Why does this not work? (package var aliasing)

L

Lucas Van Hieng

This works:
my $a = 5;
*b = \$a;

$b is an alias to $a.

ok.

but why dont this work?

my @a = (1, 2, 3);
*b = \@a;

I get an error when I try to use @b i nany way:

Global symbol "@b" requires explicit package name

What gives?

I also tried *b{ARRAY} instead of *b but no change.

5.6.1
 
B

Brian McCauley

Lucas Van Hieng said:
This works:
my $a = 5;
*b = \$a;

$b is an alias to $a.

ok.

but why dont this work?

my @a = (1, 2, 3);
*b = \@a;

It does work.

After the execution of the above the package varialble @b is an alias
for the lexical @a.
I get an error when I try to use @b i nany way:

Global symbol "@b" requires explicit package name

What gives?

The two package variables $a and $b are exempted from the need to be
delared under 'use strict' because of their special meaning in sort().
@a and @b are not.

The error you are getting is compile time.

The aliasing operation is run-time.

The effect of run-time aliasing does not propagate bechwards through
time and cause the variable to become declared at compile time.

Note: compile-time aliasing (in a BEGIN{} block) effectively _does_ count
as a declaration, and is more-or-less what 'usr vars' does.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
G

gnari

Lucas Van Hieng said:
This works:
my $a = 5;
*b = \$a;

$b is an alias to $a.

ok.

but why dont this work?

my @a = (1, 2, 3);
*b = \@a;

I get an error when I try to use @b i nany way:

Global symbol "@b" requires explicit package name

What gives?
use strict;
my $x=5;
my @x=(1,2,3);
*y=\@x;
print "\$y=$x\n";
print "\@y=@x\n";

output:
perl p.pl
$y=5
@y=1 2 3

gnari
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top