Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what's the scope of $_ array? 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

Can someone confirm what scope $_ is.

i.e.

if I call a sub with
Code:
mySub1(a,b,c);

the sub will have access to $_ array with elements indexed at 0,1,2 equaling a,b,c respectively.

but if in mySub1 I call another sub
Code:
mySub2(1,2,3);

Have I just trashed the $_ array for mySub1 as now indexes 0,1,2 equal 1,2,3 respectively?

thanks

1DMF

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
I'm not sure what kind of confirmation you are looking for, maybe this will help?
Code:
my $pattern = "%20s\t%s\n";
&mySub1 qw/a b c/;
&mySub2 qw/1 2 3/;


sub mySub1 {
	printf $pattern, "mySub1 before:", "@_";
	print "w/args -\n";
	&mySub2 qw/d e f/;
	print "w/o args -\n";
	&mySub2;
	printf $pattern, "mySub1 after:", "@_";
}

sub mySub2 {
	printf $pattern, "mySub2:", "@_";
}
 
is $_ a global so when ever you call another sub, that array is overwritten with the new set of arguments being passed to the subroutine?

or is $_ local to each subroutine block?


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
opps just realise my mistake I mean @_ !!!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
@_ is local to every subroutine block.

The only exception is if you call a subroutine without parenthesis from within another sub, that sub gets the same @_ as the calling sub had. But the inner sub can't modify @_ for the calling sub even in this case.

Code:
$ perl
outer("hello", "world");

sub outer {
   # we get @_ = hello, world
   print "sub outer got: @_\n";

   inner(); # send an empty @_
   inner("goodbye", "mars");
   inner; # send it the same @_ we have

   print "at the end, sub outer has: @_\n";
}

sub inner {
   print "sub inner got: @_\n";

   # we might not be able to modify @_ in the case
   # of the second sub call where it is read-only
   eval {
      $_[0] = "bonjour";
   };
   if ($@) {
      warn $@;
   }

   print "end of sub inner, has: @_\n";
}
__END__
sub outer got: hello world
sub inner got: 
end of sub inner, has: bonjour
sub inner got: goodbye mars
Modification of a read-only value attempted at - line 19.
end of sub inner, has: goodbye mars
at the end, sub outer has: hello world

Kirsle.net | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Actually, the last code isn't quite right.

Code:
-- inner; # send it the same @_ we have
++ &inner; # send it the same @_ we have

Perl was confused about the bareword "inner;" and didn't call it as a sub. Using the & disambiguates it and Perl calls the sub correctly.

New output:

Code:
sub outer got: hello world
sub inner got: 
end of sub inner, has: bonjour
sub inner got: goodbye mars
Modification of a read-only value attempted at - line 20.
end of sub inner, has: goodbye mars
sub inner got: hello world
Modification of a read-only value attempted at - line 20.
end of sub inner, has: hello world
at the end, sub outer has: hello world

Usually when you see a sub called with no parenthesis it's on a method of a module, like

Code:
print $obj->data;

so Perl isn't confused by it.

Kirsle.net | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Thanks Kirsle,

I'm glad that there is a local @_ for each subroutine block!

I have a sub which receives 3 arguments, I then call another sub passing it $_[0] as an argument, I was concerend when the sub returns back, that the @_ would have been trashed.

As @_ is local within the sub block, it isn't going to be a problem!



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top