in reply to Re: Declareing something with a *
in thread Declareing something with a *

3. use strict requires variables to be predeclared before use (among other things) Declare $varAlias with my or our before use and it will compile.
Yeah, it will compile if you declare $varAlias with my, but that's as useful as removing the entire while block. Then the program will compile as well. If you declare $varAlias with my, you get a lexical $varAlias, with no relationship with the glob *varAlias. You want a package variable $varAlias, not a lexical $varAlias.

Abigail

Replies are listed 'Best First'.
Re: Re: Declareing something with a *
by El Linko (Beadle) on May 18, 2004 at 14:46 UTC
    This being because globs operate on the symbol table and variables declared with my don't end up in the symbol table.

    Declaring variables in perl really does seem quite a mess, between use vars,my,our,local,strict on/off. Doesn't matter 99% of the time but every so often something doesn't work quite the way I expect and it annoys me.
      Declaring variables in perl really does seem quite a mess, between use vars,my,our,local,strict on/off.

      Whether strict is on or off doesn't matter for the type of variable. Strictness doesn't change the meaning - it just prevents you from doing certain things. Use the following table:
       Package varLexical var
      Global scopeuse vars
      Lexical scopeourmy
      local creates dynamically scoped values - it doesn't declare variables.

      Abigail

Re: Re: Declareing something with a *
by periapt (Hermit) on May 20, 2004 at 13:42 UTC
    Yeah, I see what you mean. my doesn't make sense within the scope of the while loop. I was actually thinking about declaring $varAlias so that it would be in the same scope as *varAlias but I didn't make that clear. Keeping with the idea that variables should be declared in the smallest scope possible, our is the better bet. I'm not always clear on the use of my, our and local. Since we are trying to access a more global variable in this while loop, would local work as well? From the code, it seems that the actual value in $varAlias upon entry is not important. Of course, if that is true, then why would we care to preserve its value?


    PJ
    We are drowning in information and starving for knowledge - Rutherford D. Rogers
    What good is knowledge if you have to pull teeth to get it - anonymous
      The reason my is wrong has nothing to do with scope. It has everything to do with the kind of variable. my introduces a lexical variable - and lexical variables aren't part of a typeglob. local cannot be used to introduce variables, local acts on values. In this case, we'd either use our, or use vars.

      Abigail