Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Variable Scoping in Perl: the basics

by marcelpaulo (Novice)
on May 27, 2018 at 00:19 UTC ( [id://1215262]=note: print w/replies, xml ) Need Help??


in reply to Variable Scoping in Perl: the basics

A really enlightening article: it threw light on what was a mysterious theme for me. I'd offer a suggestion, for the 4th code snippet, which bit me when I tried out with use strict. If we run it like this:
use strict; use warnings; package Szyewicki; our ($Robert); $Robert = "the boss"; package PoolHall; our ($Robert); $Robert = "the darts expert"; package Szyewicki; print "Here at work, 'Robert' is $Robert, but over at the pool hall, ' +Robert' is $PoolHall::Robert\n";
The result, which puzzled me at first, will be:
Here at work, 'Robert' is the darts expert, but over at the pool hall, + 'Robert' is the darts expert
It took me quite a while to decypher why: the lexical scope of the 2nd our, under package PoolHall, is the file, so, when we switch to package Szyewicki, it's still in effect, making $Robert a lexical alias to package variable $Szyewicki::Robert. To work as expected and intended, the code must be:
use strict; use warnings; package Szyewicki; our ($Robert); $Robert = "the boss"; package PoolHall; our ($Robert); $Robert = "the darts expert"; package Szyewicki; our ($Robert); print "Here at work, 'Robert' is $Robert, but over at the pool hall, ' +Robert' is $PoolHall::Robert\n";
The 3rd our alias $Robert to $Szyewicki::Robert.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2026-01-22 07:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (125 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.