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

Re: how do $x = \12 differ from $$y = 12 ?

by japhy (Canon)
on May 10, 2005 at 12:58 UTC ( #455547=note: print w/replies, xml ) Need Help??


in reply to how do $x = \12 differ from $$y = 12 ?

There is a world of difference. Forget that you're using a constant (12) for now. Imagine this:
my @data = ('a', 'b', 'c'); my ($x, $y); $x = \@data; @$y = @data;
$x refers to @data (that is, it holds a reference to @data). But $y doesn't; rather, the array referenced in $y holds a shallow copy of the elements in @data.

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Replies are listed 'Best First'.
Re^2: how do $x = \12 differ from $$y = 12 ?
by borisz (Canon) on May 10, 2005 at 13:12 UTC
    Thanks, that takes me closer to the answer. I wonder why $y is undefined now.
    use Scalar::Util qw/weaken/; my ( $x, $y ); $x = \12; $$y = 12; weaken($x); weaken($y); print $x; print $y; __OUTPUT__ SCALAR(0x1807f24) Use of uninitialized value in print at 5432.pl line 11.
    Boris
      Someone else showed you Devel::Peek code that explains that. $x has a refcount of 2, and $y only has a refcount of 1. It's the same as doing:
      $x = \@array; $y = [];
      There, $x has a refcount of 2 (from $x and @array), whereas $y has a refcount of only 1.

      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2020-07-07 19:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?