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

You may be right. This is actually already in a module, so I may need to split it into a sub module. Believe it or not, I am trying not to be too fancy because I am a perl novice.

local seems to be doing the trick, but I have to turn off strict to use it and that makes me uncomfortable. Thanks

  • Comment on Re: Re: Re: Re: Scope Between global and my

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Scope Between global and my
by davido (Cardinal) on Apr 01, 2004 at 18:28 UTC
    Don't turn off strict; use vars qw/variable names/;


    Dave

Re: Re: Re: Re: Re: Scope Between global and my
by jdporter (Paladin) on Apr 01, 2004 at 18:31 UTC

    You don't have to turn off strict if you either
    a) package-qualify the variable name, as in my previous example; or
    b) declare the variable using our or use vars.

    our is supposed to be a more modern replacement for use vars, but they don't do exactly the same thing, so which one is most appropriate really depends on your specific circumstances.

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

      Ok, here is what my code looks like. (Strict hates this)

      local( $ItemNumber, %CFL, @CFD, #Customer %DFL, @DFD, #Destination %IFL, @IFD, #Item %MFL, @MFD, #Master %SFL, @SFD, #Description %UFL, @UFD #User );

      How do I also declare these variables our ?

      Not like this:

      our local( $ItemNumber, %CFL, @CFD, #Customer %DFL, @DFD, #Destination %IFL, @IFD, #Item %MFL, @MFD, #Master %SFL, @SFD, #Description %UFL, @UFD #User );

      that just does not work.

      or like this:

      local our ( $ItemNumber, %CFL, @CFD, #Customer %DFL, @DFD, #Destination %IFL, @IFD, #Item %MFL, @MFD, #Master %SFL, @SFD, #Description %UFL, @UFD #User );

      that works, but strict still is not happy.

      Sorry to be dense.

      Skip

        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.