On 11/4/07, Mike Martin <[EMAIL PROTECTED]> wrote:
> Hi I have the following script
> our $list;
> sub print1 {
>
> my %list;
Okay, that's two variables named 'list'.
> print header();
> print start_html('Man Pages');
> my $page=param('man');
> my @array=`find /usr/share/man -name $page*`;
> my @list;
Now you have three of them....
> foreach my $man (@array){
> push (@list,$man);
>
> }
> The problem is that the our $list hash ref reference is never getting updated
>
> I would have thought that it should get updated in sub print1
Why would you think that? print1 uses a lexical array named @list and
a lexical hash named %list, but no scalar named $list.
No, wait; your mis-indentation fooled me; the sub keeps going after
you returned to the left margin:
> foreach my $list (@list){
Ah, now there's a lexical scalar named $list! But that's a lexical,
and you wanted to update a global scalar named $list. See what happens
when you give every variable the same name? Maybe you should use
descriptive names for your variables, but in any case don't name a
lexical with the same name as a global that you also wish to access in
that scope, because the lexical takes precedence.
Also, it helps to properly indent your code for readability. A good
programmers' text editor will do most of the heavy lifting for you.
Hope this helps!
--Tom Phoenix
Stonehenge Perl Training
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/