chunlou has asked for the wisdom of the Perl Monks concerning the following question:
Consider the following code.
print chg("word", qr/(\d)/); # print "nope" "2" =~ /(2)/; print chg("word", qr/(\d)/); # print "yes" sub chg { $_[0] =~ $_[1]; return $1 ? "yes\n" : "nope\n"; }
"2" =~ /(2)/ sets $1 to 2, which inadvertently affects chg() since chg() checks $1 for valid condition but $1 was set from outside.
If I'm using someone else's code where it might be using $1 for various things that I might not be aware of, is there anything I can do to avoid such a trap? It's kind of hard to track down such things in a large script. Thanks.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: $1 trap
by Abigail-II (Bishop) on Aug 10, 2003 at 09:10 UTC | |
by Chady (Priest) on Aug 10, 2003 at 16:07 UTC | |
by bbfu (Curate) on Aug 10, 2003 at 16:37 UTC | |
by Chady (Priest) on Aug 11, 2003 at 05:43 UTC | |
by Crackers2 (Parson) on Aug 11, 2003 at 16:46 UTC | |
|
Re: $1 trap
by pzbagel (Chaplain) on Aug 10, 2003 at 07:59 UTC | |
|
Re: $1 trap
by ihb (Deacon) on Aug 10, 2003 at 17:47 UTC | |
by diotalevi (Canon) on Aug 10, 2003 at 18:21 UTC | |
|
Re: $1 trap
by shotgunefx (Parson) on Aug 10, 2003 at 21:41 UTC | |
|
Re: $1 trap
by chunlou (Curate) on Aug 10, 2003 at 18:18 UTC | |
by pzbagel (Chaplain) on Aug 11, 2003 at 05:04 UTC |