in reply to Re: Re: Dynamically allocating variables
in thread Dynamically allocating variables

does it push the value, or just a pointer?

When you're passing around objects, really you're passing around special references. So even if you have two scalars pointing to one struct, it's that same struct you're modifying, regardless of how it's named.

It might be easier to illustrate with an example:

use Class::Struct; use strict; struct Test => [ xyz => '$' ]; our $T = new Test(xyz => 123); print "\$T->xyz starts as: ", $T->xyz, "\n"; no strict 'refs'; my $symbolic = "T"; ${$symbolic}->xyz(456); print "\$T->xyz is now: ", $T->xyz, "\n"; my %A $A{t} = $T; $A{t}->xyz(789); print "\$T->xyz is now: ", $T->xyz, "\n"; __END__ $T->xyz starts as: 123 $T->xyz is now: 456 $T->xyz is now: 789
To get "full" copy effects, you need to make a copy of each of the fields:
my $a = new Test ( xyz => 123 ); # original my $b = new Test ( xyz => $a->xyz ); # copy of a $b->xyz(456); printf "a=%d b=%d\n", $a->xyz, $b->xyz; # a=123 b=456

I don't know of an easy way to "deep copy" struct objects, though the Clone module (or Storable's 'dclone' function) may help.

Replies are listed 'Best First'.
Re: Re: Re: Re: Dynamically allocating variables
by newatperl (Acolyte) on Dec 02, 2001 at 06:18 UTC
    Thanks for clarifying that Fastolfe. But, I guess my original question still stands. Without turing strict off, I can't really load a Hash with unique structs that are created on the fly as I read each line in. true or not true?
      I have a feeling that you and Fastolfe are talking past each other.

      The answer is that you can. He is using anonymous hashes as structs (which is usually the right native Perl data structure to use for that, even though it is not perfect). What strict.pm stops you from doing is making the assumptions that these anonymous things have concrete names in your main namespace - names which you might accidentally use to have one of them override another. (This would be bad.)

      And I guess I just don't understand your question. Do you have Perl constructs in your input that you're trying to place into a hash? Something like this might work in that situation:
      my %data; while (<DATA>) { my ($key, $value) = eval $_ or die; $data{$key} = $value; } print $data{other_key}->[1]; # data __DATA__ some_key => { data => 'structure', number => 1 } other_key => [ qw/ other data structure / ] third_key => "and a simple third"
      Is this what you're getting at? If not, I need to see something more specific.