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

VB-ish behaviour byVar or byRef of objects in Perl

by jbrugger (Parson)
on Dec 11, 2007 at 14:39 UTC ( #656410=perlquestion: print w/ replies, xml ) Need Help??
jbrugger has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks, After being away from Perl for a year, i'm back, and i seek new wisdom :)

In the .Net platform an object is stored in the heap. When passing the object 'as value' and not as a reference, the object itself isn't copied, you still get a reference to it (instead of a fresh copy).

If you want a copy (like items on the stack), you need to deep copy (clone) the object by hand.

I wonder how this is done in Perl, since i could not find any info on it.

Thanks again :)

edit:
as just stated by jkva (and the others below (thanks) ).... an object in Perl already IS a reference... So it stays a reference as it's passed :).

Comment on VB-ish behaviour byVar or byRef of objects in Perl
Replies are listed 'Best First'.
Re: VB-ish behaviour byVar or byRef of objects in Perl
by moritz (Cardinal) on Dec 11, 2007 at 14:54 UTC
    Since an "object" in Perl 5 is a blessed reference, you get the same behaviour.

    You can deep clone an object with Storable::dclone.

Re: VB-ish behaviour byVar or byRef of objects in Perl
by holli (Monsignor) on Dec 11, 2007 at 14:47 UTC
    Clone


    holli, /regexed monk/
Re: VB-ish behaviour byVar or byRef of objects in Perl
by sundialsvc4 (Abbot) on Dec 11, 2007 at 15:37 UTC

    This behavior is quite common and it's implemented and used quite heavily in (current versions of...) Perl. See perldoc perlref et al.

    One of the nice concepts that's used a lot in these languages is this one:   the thing that is passed “is a” reference. In other words, it no longer matters how you pass it... rather, the thing that is passed is a magical thing that will give access to whatever it is “referencing.” It's a subtle distinction but very useful.

    Like the other languages, Perl automagically provides you with reference-counted objects that are garbage collected for you.

Re: VB-ish behaviour byVar or byRef of objects in Perl
by ikegami (Pope) on Dec 11, 2007 at 18:56 UTC

    The same applies to non-objects as well. Everything is passed by reference in Perl.

    sub func { $_[0] = "bar"; } my $s = "foo"; func($s); print("$s\n"); # bar
      Everything is passed by reference in Perl.

      I know it's true, but the common ways to handle subroutine arguments sort of make it untrue.

      sub fiddle { my ($arg) = @_; $arg = "waffle"; } sub faddle { my $arg = shift; $arg = "waffle"; } sub muddle { $_[0] = "waffle"; } my $value = "pancake"; print "$value\n"; # Prints "pancake" fiddle($value); print "$value\n"; # Prints "pancake" faddle($value); print "$value\n"; # Prints "pancake" muddle($value); print "$value\n"; # Prints "waffle"

      Shifting and slicing @_ into subroutine lexicals will make copies (which only matters for simple non-reference values). I know, I know. This is obvious stuff I'm talking about. But when somebody hears that everything is passed by reference in Perl, they also need to hear that it's a behavior that often gets bypassed in idiomatic code.

        Consider that it's because of the passed-by-aliasing behavior that those are the prevailing idoms . . . :)

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://656410]
Approved by jkva
Front-paged by jkva
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2015-07-08 07:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (96 votes), past polls