Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Unusual variable declaration

by choroba (Cardinal)
on Nov 02, 2019 at 21:46 UTC ( [id://11108263]=note: print w/replies, xml ) Need Help??


in reply to Unusual variable declaration

The first line declares an alias $var for the global variable $var in the current package. The second line makes it an alias of the $var variable in the main package.

Therefore, it only makes sense to use this in a package different to main. For example:

#!/usr/bin/perl use warnings; use strict; our $var; $var = 42; { package My; our $var; *var = \$main::var; print $var, "\n"; $var = 84; } print $var, "\n";

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Unusual variable declaration
by LanX (Saint) on Nov 03, 2019 at 12:12 UTC
    Addendum:

    • local

    I almost always use this construct with local except if I want to alias globally.

     local *var = \$main::var;

    Otherwise any later use of $My::var in another scope will also be aliased.

    • importing

    Should also be mentioned that typeglob aliasing is the essential underlying mechanism Exporter is using.

    Though mostly for subs not vars and always globally.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re^2: Unusual variable declaration
by haukex (Archbishop) on Nov 02, 2019 at 21:49 UTC

    Exactly what I was going to say :-) The most relevant docs are probably perlmod.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-25 15:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found