Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: unexpected: inside sub($1,$2) if do // then arguments will be changed

by repellent (Priest)
on Sep 11, 2011 at 08:23 UTC ( [id://925332]=note: print w/replies, xml ) Need Help??


in reply to unexpected: inside sub($1,$2) if do // then arguments will be changed

In mysub(), aliases to the global variables $1 and $2 are held in @_, but those globals don't stay set as you expected for very "long":
use warnings; use Data::Dumper; sub mysub { warn Dumper \@_; # globals still set die 'single word expected' if (my $name = shift) !~ /^\w+$/; warn Dumper \@_; # you "lost" the globals (due to matching /^ +\w+$/) die 'single word expected' if (my $value = shift) !~ /^\w+$/; } $str = "abc def"; $str =~ /(\w+)\s+(\w+)/; mysub($1, $2); __END__ $VAR1 = [ 'abc', 'def' ]; $VAR1 = [ undef ]; Use of uninitialized value $value in pattern match (m//) at ./t.pl lin +e 12. single word expected at ./t.pl line 12.

Save off global variables, or avoid using them:
my ($name, $value) = ($str =~ /(\w+)\s+(\w+)/); mysub($name, $value);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-20 05:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found