Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: VB-ish behaviour byVar or byRef of objects in Perl

by webfiend (Vicar)
on Dec 11, 2007 at 20:54 UTC ( #656481=note: print w/replies, xml ) Need Help??


in reply to Re: VB-ish behaviour byVar or byRef of objects in Perl
in thread VB-ish behaviour byVar or byRef of objects in Perl

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.

Replies are listed 'Best First'.
Re^3: VB-ish behaviour byVar or byRef of objects in Perl
by Fletch (Chancellor) on Dec 12, 2007 at 17:49 UTC

    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: note [id://656481]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2019-07-18 19:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?