in reply to Declareing something with a *

1. *varAlias is a typeglob which is sort of a window into the symbol table where all the uses of varAlias are kept. For example, *varAlias{SCALAR} points to the scalar value referenced by $varAlias while *varAlias{ARRAY} points to the array referenced by @varAlias. (It's a bit more complicated than that but I think this covers the main point.) Since $globvalue is a scalar, *varAlias refers to a scalar value.

2. It depends on whether there was a reference in %thepackage:: . The code $reftype = ref $varAlias is checking whether the value in $varAlias is a reference to something. The function ref returns true (actually the data type the reference refers to) or false ("") if $varAlias is not a reference. Thus, if there is no value assigned to $reftype, then it is likely you did not have any references in the %thepackage:: symbol table.

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.

Try perldoc perldata for info on typeglobs and perldoc -f ref for info on the function ref.

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

Replies are listed 'Best First'.
Re: Declareing something with a *
by Abigail-II (Bishop) on May 18, 2004 at 13:16 UTC
    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

      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

      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

Re: Re: Declareing something with a *
by Scarborough (Hermit) on May 18, 2004 at 15:35 UTC
    Thank you and also to Abigail for all the help on this. I'm starting to get to grips now with some of the more powerful features of this great language. A language which a lot of my old college lecturers said was very dangerous and only gudgingly showed us for CGI scripting, then a very new field. I only wish I'd been using it full time since then, and I might be able to answer more then I ask.
    Thanks again