Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
P is for Practical
 
PerlMonks  

Re: Help needed understanding global variables in Perl

by broquaint (Abbot)
on Mar 05, 2002 at 10:44 UTC ( #149325=note: print w/ replies, xml ) Need Help??


in reply to Help needed understanding global variables in Perl

> how exactly do I define (rather than declare) a global variable in Perl
The closest thing to a global variable are either "magical" variables or punctuation variables. Check out this node for more info on globals.

> is there any kind of "super" use strict, which helps me catch typos that my programs may have?
One of the reasons for using strict is to encourage the use of lexical variables and to move away from globals, which are can be a large cause of confusion to the unwary.

> is there any way of defining a variable like C's static (i.e., a global variable with local scope)
Not really. You can use a global variable in a local scope using the local() function.

# use strict; $foo = "a string"; sub bar { local $foo; print qq(foo is now localized and empty "$foo"\n); }
Or you could use a closure to create the imitation of a static variable
{ # creates a new lexical scope my $foo = "add text here - "; sub add_text { my $args = join '', @_; $foo .= $args; return $foo; } } # $foo now only referenced by &add_text
> In fact, I have trouble understanding the difference between our ($i); and use vars qw($i);
The difference is that our() declares the variable into the current package and is visible for the rest of the lexical scope (in my opinion this is very icky and kinda anti-DWIM). Whereas use vars qw($x @y %z) just declares it's arguments into the current package, and doesn't mess with the lexical scope.
HTH

broquaint

Update: changed explanation of our() vs use vars qw($x @y %z) per rob_au's note. Also realised why tilly has been knocking our() for so long ;-)
Update 2: also changed explanation of global variables per shotgunefx's note. /me thinks more research before posting might be an idea ...


Comment on Re: Help needed understanding global variables in Perl
Select or Download Code
Re: Re: Help needed understanding global variables in Perl
by rob_au (Abbot) on Mar 05, 2002 at 10:55 UTC
    There is no difference between the two really. One is a language construct (our()) and the other is a pragma. They both declare package variables (as opposed to lexical variables, which are declared with my(), and don't touch packages) into the current package.

    There is a difference between the scoping between use vars qw/ $var / and our $var - This is described in perldoc -f our and described by Sinister previously in this thread.

     

    perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Re: Re: Help needed understanding global variables in Perl
by shotgunefx (Parson) on Mar 05, 2002 at 11:05 UTC
    A clarification on the following point.
    The closest thing to a global variable in perl is a variable that lives in main::'s symbol table. This is because whenever a variable is referred it is always looked for in main::. But this is only a last resort,

    This is not true. If you are not in main:: it will not look in main for a variable unless you use our $var and then it's still only I belive in the current lexical scope.
    # Prints nothing. perl -e '$var = 'value';package FOO; print $var' #prints "value" perl -e 'our $var = 'value';package FOO; print $var' #prints "value" perl -e '$var = 'value';package FOO; print $main::var'


    -Lee

    "To be civilized is to deny one's nature."
Re: Re: Help needed understanding global variables in Perl
by Juerd (Abbot) on Mar 05, 2002 at 12:18 UTC

    The closest thing to a global variable are either "magical" variables or punctuation variables.

    No. All punctuation variables are global, and some are magical. Not every global is a punctuation variable or magical. $foo is a valid global scalar that goes in foo's stash, just like @foo.

    ++ vs lbh qrpbqrq guvf hfvat n ge va Crey :)
    Nabgure bar vs lbh qvq fb jvgubhg ernqvat n znahny svefg.
    -- vs lbh hfrq OFQ pnrfne ;)
        - Whreq
    

      Are punctuation variables truly global or are they forced to be in main:: when unqualified?

      -Lee

      "To be civilized is to deny one's nature."
        They are forced into main::, but they are global. You can abuse this by starting your string with a control character: ${^Foo} is also forced into main::. (it's actually ${"\cFoo"})

        ++ vs lbh qrpbqrq guvf hfvat n ge va Crey :)
        Nabgure bar vs lbh qvq fb jvgubhg ernqvat n znahny svefg.
        -- vs lbh hfrq OFQ pnrfne ;)
            - Whreq
        

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://149325]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2014-04-19 20:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls