P
Phil Jacobson
Good afternoon, I was fooling around with the following test code:
1. sub pageprint
2. {
3. foreach my $one (@pages)
4. {
5. my @subpage = @{$one};
6. foreach my $two (@subpage)
7. { print "$two\n"; }
8. }
9. }
10.
11. my @page1 = ("1", "2", "3");
12. my @page2 = ("one", "two", "three");
13. my @page3 = ("uno", "dos", "tres");
14. my @pages = (\@page1, \@page2, \@page3,);
15. &pageprint;
The above code doesn't work unless I remove the "my" declarations. It
brought up a few questions: Does the interpreter create a @pages when
it sees line 3 and then creates another separate one when it sees line
14? I understand if I put the subroutine at the bottom of the code it
will work with all the arrays my'd. The code also works having the
subroutine first if I "local" the page arrays. Are there any benefits
I'm overlooking of having Perl operate this way and in your opinion,
what are some of the best ways to deal with a situation where you want
to handle global variables inside a subroutine?
Thank you for your time,
Phil Jacobson
1. sub pageprint
2. {
3. foreach my $one (@pages)
4. {
5. my @subpage = @{$one};
6. foreach my $two (@subpage)
7. { print "$two\n"; }
8. }
9. }
10.
11. my @page1 = ("1", "2", "3");
12. my @page2 = ("one", "two", "three");
13. my @page3 = ("uno", "dos", "tres");
14. my @pages = (\@page1, \@page2, \@page3,);
15. &pageprint;
The above code doesn't work unless I remove the "my" declarations. It
brought up a few questions: Does the interpreter create a @pages when
it sees line 3 and then creates another separate one when it sees line
14? I understand if I put the subroutine at the bottom of the code it
will work with all the arrays my'd. The code also works having the
subroutine first if I "local" the page arrays. Are there any benefits
I'm overlooking of having Perl operate this way and in your opinion,
what are some of the best ways to deal with a situation where you want
to handle global variables inside a subroutine?
Thank you for your time,
Phil Jacobson