R. Joseph Newton wrote: > "... > Unlike 'my', however, it is a permanent > package variable and its value will be retained across calls to a > subroutine ..." --Rob > > Hi Rob, > > So does this make it equivalent to a C-style static variable?
Sort of, but beware that any occurrence of 'our $var' in the same package is the same variable. It's more like Fortran COMMON blocks if that means anything to you? > I've been trying to test it, and it seems to be impossible to even > declare it using strict. The main point of 'our' is that it lets you use package variable with 'use strict'. Any variable used without first declaring it as 'my' is a package variable which won't go away. 'use strict' stops you from using variables without first declaring them, and 'our' lets you declare them after all. > When I try to declare a variable as local, > I get an error telling me that the global symbol needs an explicit > package name. That's 'use strict' helping you out! Forget about 'local' for now. All it does is create a temporary copy of an existing global variable for the duration of the current scope. The variable has to be there first or 'use strict' will complain. > When I try to modify an existing variable, I get an > error saying that I can't modify a lexical variable. > You can't apply 'local' to anything but a global variable. A 'my' variable is a local variable anyway, so there's no point in making a temporary local copy of it. > > Without strict, I have yet to find any configuration that would > actually retain value between calls to a function. > Easy. Try this: #!perl use strict; use warnings; sub p { our $val; print ++$val, "\n"; } p;p;p;p;p;p; __END__ > > Can you show any examples of local at work--preferably using strict? > Like I said, forget about it. I've never used it except for temporarily changing the value of a Perl predefined variable. For instance, enable 'slurp' mode like this: my $buffer = do { local $/; <FH> }; you can put local $var = 'value' but without the assignment it leaves the variable as 'undef'. The new value only exists until the end of the scope (i.e. for the duration of the <FH>) after which Perl puts the old value back for you. Don't use local apart from for this sort of thing. > > I do have a little demo for the our and my scoping, about the way I > would use them. I actually couldn't detect any difference when > declared in the global scope, so I take the our declaration as a > comment, to remind me that I've done something dumb by declaring in > the global context. Essentially, I would say that visibility > protection comes down to the location of the declaration. > > #!/usr/bin/perl -w > > #use strict; > > our $FirstName = "Robert"; That's global variable $main::FirstName, but since you've declared it at file level it's in scope all the way through. > > FeedSub(); > sub FeedSub { > $MiddleName = "Joseph"; $MiddleName is undeclared, so 'use strict' will barf on it. > my $LastName = "Newton"; A local variable. It will be reallocated and reassigned here every time the sub is called. > for (1...4) { > TestIt($LastName, $MiddleName); > print "$LastName\n"; > print "$FirstName\n"; > print "$MiddleName\n"; > } > } > > sub TestIt { > my ($LastName, $MiddleName) = @_; These two parameters passed by value from FeedSub() and copied into local (non-persistent) variables. > print "$LastName\n"; > $MiddleName .= '_'; > $FirstName .= '_'; This is the only package variable that you're changing. It will grow by one underscore per call if you look at your output carefully. > print "$FirstName\n"; > print "$MiddleName\n"; > print "$LastName\n"; > } I hope this helps. Cheers Joseph, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]