{ # beginning of lexical scope
my $foo;
} # end of lexical scope
####
{ # begin lexical scope
my $foo = "a string";
print " \$foo is: ", (defined $foo ? $foo : "undefined"), $/;
} # end lexical scope
print "\$foo is: ", (defined $foo ? $foo : "undefined"), $/;
__output__
$foo is: a string
$foo is: undefined
##
##
my $ref;
{ # begin lexical scope
my $foo = "something in a lexical scope";
$ref = \$foo;
} # end lexical scope
print "\$ref refers to: $$ref", $/;
print "\$foo is: ", (defined $foo ? $foo : "undefined"), $/;
__output__
$ref refers to: something in a lexical scope
$foo is: undefined
##
##
{
my $foo = "a lexical variable";
$bar = "a package variable";
print " \$foo is: ", (defined $foo ? $foo : "undefined"), $/;
print " \$bar is: ", (defined $bar ? $bar : "undefined"), $/;
}
print "\$foo is: ", (defined $foo ? $foo : "undefined"), $/;
print "\$bar is: ", (defined $bar ? $bar : "undefined"), $/;
__output__
$foo is: a lexical variable
$bar is: a package variable
$foo is: undefined
$bar is: a package variable
##
##
use strict;
my $foo = "defined";
BEGIN {
print "foo is ", defined($foo) ? $foo : 'undef',
" during BEGIN phase\n";
};
print "foo is ", defined($foo) ? $foo : undef, " at runtime\n";
__output__
foo is undef during BEGIN phase
foo is defined at runtime
##
##
## lextut1.pl
my $foo = "in lextut1.pl's lexical file scope";
print "\$foo is: ", (defined $foo ? $foo : "undefined"), $/;
##
##
perl -e 'require "lextut1.pl"; \
print "\$foo is: ", (defined $foo ? $foo : "undefined"), $/;'
$foo is: in lextut1.pl's lexical file scope
$foo is: undefined
##
##
sub foo
{ # begin lexical scope
my $x = "a string";
print "\$x in foo() is: ", (defined $x ? $x : "undefined"), $/;
bar();
} # end lexical scope
sub bar
{ # begin lexical scope
print "\$x in bar() is: ", (defined $x ? $x : "undefined"), $/;
} # end lexical scope
foo();
__output__
$x in foo() is: a string
$x in bar() is: undefined
##
##
open(SRC, $0) or die("ack: $!");
my @lines = ;
## $line is declared *before* the braces
foreach my $line (@lines) {
## $w is declared within the condition, which is
## also before the braces
if(my($w) = $line =~ /\b(\w+)\b/) {
print "bareword found: $w\n";
}
print "\$w is: ", (defined $w ? $w : "undefined"), $/
if $line eq $lines[$#lines];
}
print "\$line is: ", (defined $line ? $line : "undefined"), $/;
__output__
bareword found: open
bareword found: my
bareword found: foreach
bareword found: if
bareword found: print
bareword found: print
bareword found: if
bareword found: print
$w is: undefined
$line is: undefined
##
##
## otherwise $r would be auto-vifified as a package global
use strict;
print $r,$/ if my $r = 10 % 5;
__output__
Global symbol "$r" requires explicit package name at - line 1.
Execution of - aborted due to compilation errors.
##
##
my $foo = "file scope";
{
my $foo = "outer scope";
{
my $foo = "inner scope";
print " \$foo is: $foo\n";
}
print " \$foo is: $foo\n";
}
print "\$foo is: $foo\n";
__output__
$foo is: inner scope
$foo is: outer scope
$foo is: file scope
##
##
my @list = qw(a list of words);
for my $w (@list) {
if($w =~ /^[aeiou]/) {
$w = "$w: begins with a vowel";
} else {
$w = "$w: begins with a consonant";
}
print $w, $/;
}
__output__
a: begins with a vowel
list: begins with a consonant
of: begins with a vowel
words: begins with a consonant
##
##
sub foo {
print " \$x is: $x\n";
}
$x = "original state";
{ # beginning of lexical scope
local $x = "altered state";
foo();
} # end of lexical scope
print "\$x is: $x\n";
__output__
$x is: altered state
$x is: original state
##
##
{ # begin lexical scope
local $x = "auto-vivified";
print " \$x is: ", (defined $x ? $x : "undefined"), $/;
} # end lexical scope
print "\$x is: ", (defined $x ? $x : "undefined"), $/;
print "*x is: ", (exists $main::{x} ? $main::{x} : "undefined"), $/;
__output__
$x is: auto-vivified
$x is: undefined
$main::{x} is: *main::x
##
##
use IO::File;
my $file;
{ ## this trick is known as file slurping
local $/;
my $fh = IO::File->new("lextut1.pl") or die("ack: $!");
$file = <$fh>;
}
print $file;
my @foo = qw( a comma separated list of words );
{
local $" = ', ';
print "@foo\n"
}
__output__
my $foo = "in the lextut1.pl's lexical file scope";
print "\$foo is: ", (defined $foo ? $foo : "undefined"), $/;
a, comma, separated, list, of, words
##
##
{
package foo;
our $x = "in foo";
package bar;
## $x can still be seen as it is still in scope
print " \$x is: $x\n";
}
print "\$foo::x is: $foo::x\n";
__output__
$x is: in foo
$foo::x is: in foo
##
##
## set stricture checking for the rest of the file scope
use strict;
## see. man perllexwarn for why this is double-plus good
use warnings;
## ah, heaven-sent
use File::Find::Rule;
## for lexically scoped file-handles
use IO::File;
## prevent subroutines from being able access program level variables
{
## naked block's lexical scope
my @files = File::Find::Rule->file()
->name("*.{pl,pm}")
->in( shift @ARGV );
## $fl is in the foreach lexical scope
foreach my $fl (@files) {
## ditto with $lc
my $lc = count_lines($fl);
print "$fl: $lc line".($lc != 1 && 's')." of code\n";
}
}
sub count_lines {
## will be closed when we exit the current scope
my $fh = IO::File->new(shift) or die("ack: $!");
## scoped (and therefore private) to count_lines()
my $count = 0;
$count++ while <$fh>;
return $count;
}