Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Lexical scoping like a fox

by broquaint (Abbot)
on Nov 18, 2002 at 19:19 UTC ( [id://213855]=perlmeditation: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    { # beginning of lexical scope
    
      my $foo;
    
    } # end of lexical scope
    
  2. or download this
    { # begin lexical scope
    
    ...
    
     $foo is: a string
    $foo is: undefined
    
  3. or download this
    my $ref;
    { # begin lexical scope
    ...
    
    $ref refers to: something in a lexical scope
    $foo is: undefined
    
  4. or download this
    {
        my $foo = "a lexical variable";
    ...
     $bar is: a package variable
    $foo is: undefined
    $bar is: a package variable
    
  5. or download this
    use strict;
    my $foo = "defined";
    ...
    
    foo is undef during BEGIN phase
    foo is defined at runtime
    
  6. or download this
    ## lextut1.pl
    my $foo = "in lextut1.pl's lexical file scope";
    
    print "\$foo is: ", (defined $foo ? $foo : "undefined"), $/;
    
  7. or download this
    perl -e 'require "lextut1.pl"; \
             print "\$foo is: ", (defined $foo ? $foo : "undefined"), $/;'
    $foo is: in lextut1.pl's lexical file scope
    $foo is: undefined
    
  8. or download this
    sub foo
    { # begin lexical scope
    ...
    
    $x in foo() is: a string
    $x in bar() is: undefined
    
  9. or download this
    open(SRC, $0) or die("ack: $!");
    my @lines = <SRC>;
    ...
    bareword found: print
    $w is: undefined
    $line is: undefined
    
  10. or download this
    ## otherwise $r would be auto-vifified as a package global
    use strict;
    ...
    
    Global symbol "$r" requires explicit package name at - line 1.
    Execution of - aborted due to compilation errors.
    
  11. or download this
    my $foo = "file scope";
    {
    ...
      $foo is: inner scope
     $foo is: outer scope
    $foo is: file scope
    
  12. or download this
    my @list = qw(a list of words);
    for my $w (@list) {
    ...
    list: begins with a consonant
    of: begins with a vowel
    words: begins with a consonant
    
  13. or download this
    sub foo {
        print " \$x is: $x\n";
    ...
    
     $x is: altered state
    $x is: original state
    
  14. or download this
    { # begin lexical scope
      
    ...
     $x is: auto-vivified
    $x is: undefined
    $main::{x} is: *main::x
    
  15. or download this
    use IO::File;
    
    ...
    
    print "\$foo is: ", (defined $foo ? $foo : "undefined"), $/;
    a, comma, separated, list, of, words
    
  16. or download this
    {
      package foo;
    ...
    
     $x is: in foo
    $foo::x is: in foo
    
  17. or download this
    ## set stricture checking for the rest of the file scope
    use strict;
    ...
    
        return $count;
    }
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://213855]
Approved by valdez
Front-paged by krisahoch
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-19 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found