Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: Re: Re: Dynamically allocating variables by Fastolfe
in thread Dynamically allocating variables by newatperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-03-28 20:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found