in reply to Help needed understanding global variables in Perl
> 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.
Or you could use a closure to create the imitation of a static variable# use strict; $foo = "a string"; sub bar { local $foo; print qq(foo is now localized and empty "$foo"\n); }
> In fact, I have trouble understanding the difference between our ($i); and use vars qw($i);{ # 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
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 ...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Help needed understanding global variables in Perl
by rob_au (Abbot) on Mar 05, 2002 at 10:55 UTC | |
|
Re: Re: Help needed understanding global variables in Perl
by shotgunefx (Parson) on Mar 05, 2002 at 11:05 UTC | |
|
Re: Re: Help needed understanding global variables in Perl
by Juerd (Abbot) on Mar 05, 2002 at 12:18 UTC | |
by shotgunefx (Parson) on Mar 05, 2002 at 13:28 UTC | |
by Juerd (Abbot) on Mar 05, 2002 at 14:12 UTC |