On Thu, Sep 19, 2002 at 09:39:54AM +0100, Gary Stainburn wrote:
> I've got a problem understanding a scoping issue I've got.

http://perl.plover.com/FAQs/Namespaces.html is a good online resource for
learning about scoping.

 
> I've got 2 hashes which I defined inside the sub that generates the form
> thus:
> 
> sub do_form() {
>   my ($error)=@_;
>   my %dships=('X'=>'<Any>','L'=>'Leeds','D'=>'Doncaster');
>   my %depts=('X'=>'<Any>','S'=>'Service','P'=>'Parts','T'=>'Rapid-Fit',
>              'B'=>'Bodyshop','C'=>'Commercial','N'=>'New Cars',
>              'U'=>'Used Cars');
> .....
>    print $query->popup_menu('dealer',['X','L','D'],'X',\%dships);
>   print qq{</td></tr><tr><td>Department</td><td>};
>   print $query->popup_menu('dept',['X','P','S','B','T','C','N','U'],'X',
>          \%depts);

If the ordering of your menu items doesn't matter I would suggest:

    print $query->popup_menu('dealer', [values %dships], 'X', \%dships);
    ...
    print $query->popup_menu('dept',   [values %depts ], 'X', \%depts );


> .....
> }
> 
> This worked fine until I needed the same 2 hashes in other sub's so I moved 
> them to the top of the script - outside any sub{} blocks.  I thought that 
> this would then make those hashed available to everyone.  

It should, there must be something else wrong.  If the code is long, trim it
down to the simplest test case that duplicates the problem, then post it
back to the list.  If the code is short, post it to the list so we can see
what you're doing.

I'm somewhat surprised you didn't initialize the hashes outside of the
subroutine to begin with.  As it is now, you're initializing them every time
the subroutine is called.


> What actually happened was that the hashed became unavailable and on the
> form I simply saw the single letters as defined in the array.

If you truly are having a scoping issue, and are using strict, you should
have seen a fatal error, not blank values.  You are using strict, aren't
you?


Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to