Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Update references

by Khen1950fx (Canon)
on May 13, 2012 at 15:43 UTC ( #970295=note: print w/ replies, xml ) Need Help??


in reply to Update references

This might make is clearer:

#!/usr/bin/perl -l use strict; no strict 'refs'; use warnings; use Devel::SimpleTrace; use Data::Dumper::Concise; print "With Arrays: "; my(@a) = qw(0 1 2 3); my $ar = \@a; my $air = \$a[0]; print "Define array >@a<\nCreate a reference to \$a[0]"; print "array ref: >@$ar<"; print "array index ref: >$$air"; @a = qw(a b c); $air = \$a[0]; print "Change array to >@a<"; print "array ref: >@$ar<"; print "array index ref: >${$air}<";


Comment on Re: Update references
Download Code
Re^2: Update references
by Anonymous Monk on May 13, 2012 at 17:46 UTC

    The point is, that I don't want to renew the reference. I'm using this kind of reference for Tk-Checkbuttons and I'd have to recreate all of them (and there a quite a few).

    I thought, that a reference points to a place in your memory and doesn't care what's written in there. But it looks like I can't replace that piece of data by modifying the original array. Too me it looks a bit odd.
      But it looks like I can't replace that piece of data by modifying the original array.

      Of course you can, but you must not destroy the internal reference of the array slot [0] to the scalar originally stored in it.  If you do so, you have no access to that scalar any longer via $a[0].

      Change @a = ('a','b','c') to  pop @a; @a[0..2] = qw(a b c); and things will work fine.

      In the former case, 3 new scalars are being placed into the array slots, while in the latter case, the old scalars are being modified.

      #!/usr/bin/perl -w use strict; print "--------Trying with arrays-------\n"; my @a = (0, 1, 2, 3); my $ar = \@a; # Array reference my $air = \$a[0]; # Reference to member of an array print "Define array >@a<\nCreate a reference to \$a[0]\n"; print "array ref: >@$ar< \n"; print "array index ref: >$$air<\n"; pop @a; @a[0..2] = qw(a b c); print "Change array to >@a<\n"; print "array ref: >@$ar< \n"; print "array index ref: >$$air<\n"; # <--- I'd like to get "a" here __END__ --------Trying with arrays------- Define array >0 1 2 3< Create a reference to $a[0] array ref: >0 1 2 3< array index ref: >0< Change array to >a b c< array ref: >a b c< array index ref: >a<

      Your misunderstanding is that you think Perl works like C, i.e. that references directly point to the memory locations where the actual data is stored.  This is not how Perl works.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2014-08-20 16:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (118 votes), past polls