Sam Adamsh
Sam Adamsh

Reputation: 3391

How do I change a Perl variable through a reference to it?

Why does this print 8? Why doesn't $e alter $i?

my $i = 8;

sub u
{
    return \$i;
}

my $e = u();
$e = "eer";
print $i; #8

Upvotes: 1

Views: 80

Answers (1)

cjm
cjm

Reputation: 62109

References are not aliases. You must explicitly dereference them.

$$e = "eer"; # Store 'eer' into the variable referenced by $e

is not the same as

$e = 'eer'; # Store 'eer' into $e, discarding its previous content

Upvotes: 8

Related Questions