Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Variable Scoping in Perl: the basics

by arturo (Vicar)
on Mar 23, 2001 at 21:39 UTC ( #66677=perltutorial: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    #!/usr/bin/perl -w
    
    ...
    package main;
    
    # terminate("arturo"); # produces an error if you uncomment it
    
  2. or download this
    #!/usr/bin/perl -w
    
    ...
    
    $Robert = "the boss";  # error!
    print "\$Robert = $Robert\n";
    
  3. or download this
    #!/usr/bin/perl -w
    
    ...
    
    $main::Robert = "the boss";
    print "\$main::Robert = $main::Robert\n";
    
  4. or download this
    #!/usr/bin/perl -w
    
    ...
    
    print "Here at work, 'Robert' is $Robert, but over at the pool hall, '
    +Robert'
    is $PoolHall::Robert\n";
    
  5. or download this
    #!/usr/bin/perl -w
    
    ...
    print "\$foo: $foo\n"; # prints "Hey!" -- the my variable's still in s
    +cope
    # if we hadn't made that declaration above, this would be an error: th
    +e
    # interpreter would tell us that Bar::foo has not been defined.
    
  6. or download this
    #~/usr/bin/perl -w
    
    ...
    package Movie;
    
    print "Bob => $bob, Carol => $carol\n";
    
  7. or download this
    #!/usr/bin/perl -w
    
    ...
    sub showfoo {
     return $foo;
    }
    

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perltutorial [id://66677]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2016-10-01 23:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?






    Results (9 votes). Check out past polls.