[kw-pm] $1 matching question

Daniel R. Allen da at coder.com
Wed Jul 21 15:32:41 CDT 2004


Ah, thanks, that's the ideom I'd used before- if (/whatever/) { print }

The camel book chapter 2 says:
"the variables $1, $2, $3 ... are automatically localized, and their scope
extend to the end of the enclosing block... or to the next successful
pattern match, whichever comes first".

I guess I'm surprised that $1 didn't go out of scope the next time around
the WHILE loop (in the deparsed version); that would be the behaviour of
least surprise, as far as I can tell.

*shrug*

-Daniel

On Wed, 21 Jul 2004 Greg.Fenton at sybase.com wrote:

> You need to put an IF around your print.  $1 is assigned to for the first
> line, the second match doesn't match, but a non-match does not unset $1.
>
> bash% echo -e "9 \n \n 2" | perl -ne'if ( /(\d+)/ ) { print "$1 "; }'
>
> greg.fenton
> ----
> Greg Fenton
> Consultant, Solution Services
> iAnywhere Solutions
>
>
>
>
> "Daniel R. Allen" <da at coder.com>
> Sent by: kw-pm-bounces at mail.pm.org
> 07/21/2004 03:55 PM
>
>         To:     kw-pm at mail.pm.org
>         cc:
>         Subject:        [kw-pm] $1 matching question
>
>
>
>
> I've got a regex question that I'm probably just being stupid about:
>
> bash% echo -e "9 \n \n 2" | perl -ne'/(\d+)/; print "$1 ";'
>
> has the following result:
>
> 9 9 2
>
> Shouldn't it be '9  2' ?  And the result is the same with:
>
> bash% echo -e "9 \n \n 2" | perl -ne'$z = ""; /(\d+)/; $z = $1; print "$z
> " if $z;'
>
> Which means $1 is actively being assigned to, when there isn't a match!
>
> deparse tells me:
>
> bash% perl -MO=Deparse -ne'/(\d+)/; print "$1 ";'
> LINE: while (defined($_ = <ARGV>)) {
>     /(\d+)/;
>     print "$1 ";
> }
>
> ...which looks just like I'd expect.  Maybe I just took a stupid pill this
> morning.
>
>
> -Daniel
>
> --
> "Use a different web browser. There are a number of significant
> vulnerabilit-
> ies in technologies relating to the [Microsoft] IE domain/zone security
> model,
> the DHTML object model, MIME type determination, and ActiveX."  - US CERT
> (US
> Computer Emergency Readiness Team) - http://www.kb.cert.org/vuls/id/713878
>
> _______________________________________________
> kw-pm mailing list
> kw-pm at mail.pm.org
> http://mail.pm.org/mailman/listinfo/kw-pm
>
>





More information about the kw-pm mailing list