tom arnall wrote:
> this works:
BEGIN blocks are compiled and run first before any code in main and they have
lexical scope because of the {} brackets.
> #!/usr/bin/perl -w
> use strict;
> print "b in main: $b";
The variables $a and $b are special in perl which means that they are global
by default. If you had used a different name you would have had to use the
package name with it or declare it with our() or my().
> a();
> #----------------------
> BEGIN{
> our ($b);
our() says that you can now use the variable in the current scope without
prepending the package name but that is superfluous because $b is special and
never requires the package name.
> my ($fn) = @ARGV;
my() creates a lexical variable that only exists within the current scope.
> $b = `cat $fn`;
>
> sub a {
> print "b in a: $b";
> }
> }
>
> this doesn't:
>
> #!/usr/bin/perl -w
> use strict;
> my $b = `cat $fn`;
The variable $fn is local to the BEGIN block because our() declaration only
applies the the enclosing scope. If you want to use $fn here you have to
either define it with our() or my() or prepend the package name.
> print "b in main: $b";
> a();
> #----------------
> BEGIN{
> our ($fn) = @ARGV;
> my $b = `cat $fn`;
>
> sub a {
> print "b in a: $b";
> }
> }
>
> getting:
>
> Global symbol "$fn" requires explicit package name at ./v line 3
You would get the same message for the other program if you had not used the
special $b variable.
This may help: http://perl.plover.com/FAQs/Namespaces.html
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>