I would like to pass the single capture of a reg-ex as a scalar to a subroutine, how do I go about doing this? Here is an example:
sub myfunc($)
{
my ($value)=@_;
# Do something with $value...
}
# This is the data we want to parse
my $some_var='value: 12345'; # For example
# We want to extract the value '12345' from $some_var
# and pass it to the myfunc subroutine as a scalar
# Attempt #1: This doesn't work
myfunc($some_var=~/value: (\d+)/);
# Attempt #2: This does work, but seems overly complicated
myfunc(join('',$some_var=~/value: (\d+)/));
Is there a better way than Attempt #2?
Update:
Oesor's answer gives exactly what I was looking for to avoid calling join
:
myfunc(($some_var=~/value: (\d+)/)[0]);