Re: [SPOILER] Re: New Quiz: "What does this code do?" (1-December-2006)

[email protected] (Randal L. Schwartz) 14 Dec 2006 16:18:50 -0800
Newsgroups gmane.comp.lang.perl.qotw.discuss
Message-ID <[email protected]>
>>>>> "Jay" == Jay Savage <[email protected]> writes:
Jay> $1 is implicitly localized to the enclosing block, not to the operator
Jay> itself. That means that something like the following would be
Jay> dangerous if $1 weren't read-only:

Jay> {
Jay>     m/(.)/;
Jay>     &sub;
Jay>     print $1;
Jay> }

It's dangerous anyway.  Never use $1 unless you've tested the match!
$1 is *set* on a match, but not *reset* on a fail.

Consider:

        "xyz" =~ /(x..)/; # $1 is now xyz
        "xyz" =~ /(y..)/; # $1 is *still* xyz

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[email protected]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!