http://qs321.pair.com?node_id=11108263


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.