Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Re5; Is a global variable better than repetition within variables?

by danger (Priest)
on Jan 28, 2002 at 03:51 UTC ( [id://141989]=note: print w/replies, xml ) Need Help??


in reply to Re5; Is a global variable better than repetition within variables?
in thread Is a global variable better than repetition within variables?

Probably referring to the magic of: if you use an empty pattern then the last successfull pattern match is re-used. Doesn't show up in your example, but could catch someone unawares:

#!/usr/bin/perl -w use strict; # this messes up the intended empty pattern later: $_ = 'match'; /m/; my $prefix = 'dog'; my %hash = map {++$;,$_} (qw(ged house ma matic),''); print "Before:\n"; print "$_ => '$hash{$_}'\n" for keys %hash; s//$prefix/for values%hash; # Add the Prefix... print "\nAfter:\n"; print "$_ => '$hash{$_}'\n" for keys %hash; __END__ output: Before: 1 => 'ged' 2 => 'house' 3 => 'ma' 4 => 'matic' 5 => '' After: 1 => 'ged' 2 => 'house' 3 => 'doga' 4 => 'dogatic' 5 => ''

That could be easily fixed by using s/^/$prefix/ rather than the empty pattern in this case.

Update: just for reference, the relevant documentation is in perlfunc under the /PATTERN/cgimosx operator:

If the PATTERN evaluates to the empty string, the last successfully matched regular expression is used instead.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-26 00:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found