From: "shawn wilson" <[email protected]> > On Aug 16, 2011 9:48 PM, "John W. Krahn" <[email protected]> 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 code above doesn't use s/// but just m//, so it doesn't replace, but just match (and the 'm' is missing as usually). I have shown how can s/// can be used without changing the original variable, but in this case it could be more clear and preferable to just extract the part you want from $ARGV[0] without using s///. Octavian -- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] http://learn.perl.org/
