in reply to Re: Going Backwards In Reference
in thread Going Backwards In Reference

Clint, thanks for answering. I am actually going to do exactly what you said, however, I am trying to find where it points to a value so I can reinit it into a hash. I ended up doing it in strings, and its working fine now, I take it step by step and if at any point it points to a value it simply gets replaced by a hash as needed. I am little stuck on the following code perhaps you could help me out.
sub addTo() { my $location = $_[0]; my $data = $_[1]; my @keys = split(/\//, $location); my $key; my $current = $database; while($key = shift(@keys)) { $current->{$key} = {} unless (exists $current->{$key} && ref($curr +ent->{$key}) eq "HASH"); $current = $current->{$key}; } #I AM STUCK HERE ----- $current = $data; }


Since $current is a pointer, I understand that by setting it to $data it doesn't change the value of what its referencing but changes $current to equal the scalar value $data.

I tried the following snippet
my $pointer = \$database->{"2002"}->{"Gustav"}->{"General"}->{"Overvie +w"}; ${$pointer} = "nasdfaull";
This worked well, and actually changed the value it was referencing. However, when I tried to do the same referencing technique below with $current = $database, I would get stuck on the unless line, telling me that I didn't have a hash reference. The addTo method definitely works if I keep it as it now but switch the $current = $data code with
$current->{$key} = $data;
I would really like to make it work without having to switch that though.

-Constantin

Replies are listed 'Best First'.
Re^3: Going Backwards In Reference
by GrandFather (Saint) on Jul 12, 2007 at 22:13 UTC

    There is a bit of nomenclature here that is not working in your favor. Perl does not have a concept of "pointers", although it does use "references" a lot (there is a not so subtle distinction - in Perl objects are reference counted). Perl allows references to pretty much anything, including references so you can have what may be considered indirect references, but they still ain't pointers.

    I suspect a good dose of reading is what you most need at this point. Take a look at perldata and perllol. You may find the Tutorials section Data Types and Variables helps a lot too.

    Assignment always replaces whatever was previously contained by a variable. It doesn't matter what "type" of thing the variable previously contained, it gets replaced.


    DWIM is Perl's answer to Gödel
      I ended up doing $current->{$key} = $data, and after reading your post I think I see how I confused myself with the pointer/references issue, Perl not having pointer, only references. Thanks for the help guys.