Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: my $x or my ($x)

by johngg (Abbot)
on Apr 04, 2006 at 12:47 UTC ( #541140=note: print w/ replies, xml ) Need Help??


in reply to my $x or my ($x)

Another place where the effect of my $x; and my ($x); differs is in matching regular expressions. Assigning a match in scalar context just records whether the match was successful, like this

perl -e 'my $x = "abcdefg" =~ /(cd)/; print "$x\n";'

prints

1

whereas matching in list context assigns the captures in the match

perl -e 'my ($x) = "abcdefg" =~ /(cd)/; print "$x\n";'

prints

cd

Normally, or rather probably, you would be making more than one capture in the regular expression so you would do something like

my($this, $that) = $string =~ /abc(def).+?(pq)$/;

Cheers,

JohnGG


Comment on Re: my $x or my ($x)
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://541140]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2015-07-07 07:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (87 votes), past polls