On Tue, Aug 16, 2011 at 8:27 PM, Joseph L. Casale <jcas...@activenetwerx.com
> wrote:

> What is the correct way to quickly assign the result of a regex against
> a cmdline arg into a new variable:
>
> my $var = ($ARGV[0] =~ s/(.*)foo/$1/i);
>
> Obviously that's incorrect but is there a quick way without intermediate
> assignment?
>
> Thanks!
> jlc
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>
Hi Joseph,

I would suggest naming your selectors like so: $ARGV[0] =~
s/(?'string'.*)foo/$1/i;

Now you can access this variable by simply using $+{string} as your
variable. That way you can identify your variables directly and assign them
meaningful names. Certainly in a more complex regular expressions with many
variables being retrieved this can be very useful. Of course the draw back
is that on the next regular expression this $+{<whatever>} will get reset
and you will loose what ever you retrieved similar to $1 etc...

Regards,

Rob

Reply via email to