in reply to $1 trap
Thanks for the replies.
Are there modules or debugging techniques that can help track down such a logical $1 error? Consider the code below.
print chg("ctag\n", qr/t(g)/); # print "ctag" "g" =~ /(c)/; print chg("ctag\n", qr/t(g)/); # print "ctag" print chg("ctag\n", qr/t(g)/); # print "ctag" "g" =~ /(g)/; print chg("ctag\n", qr/t(g)/); # print "ctga" sub chg { my ($str, $pat) = @_; $str =~ /$pat/; eval "\$str =~ tr/a$1/$1a/" if $1; return $str; }
The code will work most of the time except when something like "g" =~ /(g)/ occurs. If I'm using someone else's code or even my own (I can't force someone or myself to write bug-free code), since the logical error occurs only once a while and even if it happens it's not necessarily noticeable, when such an error does occur, $1 is probably not the first thing coming to my mind when trying to debug it, especially when there're no warning messages pointing me to it.
__________________
Are these similar issues whenever using such global variables as $_?
Update: Maybe I should rephrase my question as not just how to avoid this $1 mistake in the first place but how to pinpoint one afterwards should an error already be happening.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: $1 trap
by pzbagel (Chaplain) on Aug 11, 2003 at 05:04 UTC |