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 (Bishop) 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.