in reply to what is the difference between 'my' and 'local'?
Local comes in to play when you have a global/package variable defined, and you want to temporarily use that variable for something else. It often comes up with the special variables, such as $/, which is the variable which defines the input seperator. By default, it is the newline. But what if you would like to suck in an entire file, and not do it line by line? You could do this, not using local:# Define a global variable, the compatible way use vars qw($foo); # Define a global variable, the Perl 5.6.x way our $foo; # Define a "lexical" variable my $foo;
That works. However, it's not considered "safe". What is considered more safe would be to use local, which would limit the scode of $/ when we undefine it like so:undef $/; # Enables whole file mode open (FH, $filename); # Opens the file while(<FH>) { # Contains the whole file blah }
When the last closing bracket is reached, the original value of $/ (newline by default) is restored, as the "local" definition goes out of scope.{ undef local $/; open (FH, $filename); while(<FH>) { blah } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: what is the difference between 'my' and 'local'?
by Hofmator (Curate) on Jun 22, 2001 at 13:04 UTC |