in reply to Re: Re: Re: Re: Re: Re: Scope Between global and my
in thread Scope Between global and my

You have to declare (using our) before you can localize. Example follows.

# in main file sub main { # declare, then localize: our ( $ItemNumber, %CFL, @CFD, #Customer %DFL, @DFD, #Destination %IFL, @IFD, #Item %MFL, @MFD, #Master %SFL, @SFD, #Description %UFL, @UFD #User ); local ( $ItemNumber, %CFL, @CFD, #Customer %DFL, @DFD, #Destination %IFL, @IFD, #Item %MFL, @MFD, #Master %SFL, @SFD, #Description %UFL, @UFD #User ); # set some values: $ItemNumber = 6; # call other subs. Can be in other lexical locations, but MUST # be in the same namespace for this 'our' to work! child_sub1(); }
# in child file 1: sub child_sub1 { # declare ONLY: our ( $ItemNumber, %CFL, @CFD, #Customer %DFL, @DFD, #Destination %IFL, @IFD, #Item %MFL, @MFD, #Master %SFL, @SFD, #Description %UFL, @UFD #User ); print "Item: $ItemNumber\n"; }
Since our has lexically-scoped effect, you would put that declaration at the inner-most necessary lexical scope, i.e. inside each of the relevant subs. But only put the local at the point where you want to "protect", i.e. "save", any current values of these variables, i.e. in your "main" sub -- probably just below the our declaration.

jdporter
The 6th Rule of Perl Club is -- There is no Rule #6.

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: Re: Re: Scope Between global and my
by SkipHuffman (Monk) on Apr 02, 2004 at 19:42 UTC

    Thanks, that does answer the question. I still think I must be missing something though. But, I have the code working, and I only wince a little when I see that section.

    Again, thank you so much for your help. You and the Monestary are really helping my birth into the world of perl.

    Skip