[sf-perl] Plucking from a string

Penkethman, John A john.a.penkethman at lmco.com
Fri May 23 14:20:13 PDT 2008


As a beginner in Perl, there have been three areas that have been most
troublesome to learn (aside from general cryticity): 
1) scalar vs. list context, as in the thing below
2) variable scoping, as in name space, packages etc. I understand
lexical now, I guess.
3) variable lifetime, with respect to when Perl destroys or keeps data
depending on whether or not a reference still exists to it.

Jack

-----Original Message-----
From: sanfrancisco-pm-bounces+john.a.penkethman=lmco.com at pm.org
[mailto:sanfrancisco-pm-bounces+john.a.penkethman=lmco.com at pm.org] On
Behalf Of Garth Webb
Sent: Friday, May 23, 2008 11:33 AM
To: San Francisco Perl Mongers User Group
Subject: Re: [sf-perl] Plucking from a string

On Fri, May 23, 2008 at 10:36 AM, Penkethman, John A
<john.a.penkethman at lmco.com> wrote:
> Good stuff.
>
> But what's happening internally with
>
> while ($theline =~ /($TARGETSTR\d+)/gi) {  print "$1\n"; }
>
>
> Within the while loop, is the matching expression "$theline =~
> /($TARGETSTR\d+)/gi" returning one match at a time for each loop
> iteration? It would seem so since it then assigns the result to $1
each
> time.
>
> On the other hand, in frosty's
>
> my $TARGET = qr/(?:bcd=\d+)/;
>
> print "$_\n" for ( $theline =~ /$TARGET/g );
>
> the "$theline =~ /$TARGET/g" statement seems to be returning a whole
> array, which then gets assigned to $_ one at a time. Why the
difference?
> Is there a difference? I think this is great, but I would not have
> expected this behavior in the while loop.

The regex responds to scalar vs. array context.  In an array context,
all the matches are returned.  In a scalar context, just one at a time
is returned.

Garth

> Thanks,
> jack
>
>
> -----Original Message-----
> From: sanfrancisco-pm-bounces+john.a.penkethman=lmco.com at pm.org
> [mailto:sanfrancisco-pm-bounces+john.a.penkethman=lmco.com at pm.org] On
> Behalf Of yary
> Sent: Friday, May 23, 2008 7:47 AM
> To: San Francisco Perl Mongers User Group
> Subject: Re: [sf-perl] Plucking from a string
>
> On Fri, May 23, 2008 at 7:30 AM, Neil Heller <nheller at silcon.com>
wrote:
>> Just one more question...
>>
>>
>>
>> In the line:
>>
>> while ($theline =~ /($TARGETSTR\d+)/gi) {  print "$1\n"; }
>>
>>
>>
>> why is the d+ escaped?
>
> Try it without the backslash, and then try replacing the numbers in
> the input with the letter "d". It's an eye opener!
>
> Perhaps the command "perldoc perlre" isn't working on your system (bad
> install?)- so try this page-
> http://www.perl.com/doc/manual/html/pod/perlre.html
> and search for "\d"
>
> -y
> _______________________________________________
> SanFrancisco-pm mailing list
> SanFrancisco-pm at pm.org
> http://mail.pm.org/mailman/listinfo/sanfrancisco-pm
> _______________________________________________
> SanFrancisco-pm mailing list
> SanFrancisco-pm at pm.org
> http://mail.pm.org/mailman/listinfo/sanfrancisco-pm
>
_______________________________________________
SanFrancisco-pm mailing list
SanFrancisco-pm at pm.org
http://mail.pm.org/mailman/listinfo/sanfrancisco-pm


More information about the SanFrancisco-pm mailing list