At 11:43 PM -0400 8/16/11, shawn wilson wrote:
On Aug 16, 2011 9:48 PM, "John W. Krahn" <jwkr...@shaw.ca> wrote:

 Joseph L. Casale 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);


 my ( $var ) = $ARGV[0] =~ /(.*)foo/i;

IIRC, that rederines $ARGV as well. I think (my $var = $ARGV) =~ /(.+)foo/;
might be better.

The binding operator =~ does not modify the variable on its left-hand side if the expression on the right-hand side is a regular expression. The substitute operator (s///) does that. The only thing modified are the various data elements associated with evaluating a regular expression, including, in this instance, the global $1 variable.



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to