Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: perl string pass by value

by davido (Cardinal)
on Apr 02, 2018 at 14:44 UTC ( [id://1212167]=note: print w/replies, xml ) Need Help??


in reply to perl string pass by value

It's worth noting that the special variable, "@_" contains aliases to the calling args, not copies of them. Therefore, there is no copy being made here:

sub foo { return length($_[0]); }

...because we're acting only on the aliased entity. Similarly, there should be no copy here (assuming the s/// operator doesn't make one):

sub dot_to_underscore { $_[0] =~ s/\./_/g; } my $string = 'hello.world'; print "$string\n"; dot_to_underscore($string); print "$string\n";

No copy because we acted upon the alised entity, which is the same as acting upon the entity itself. On the other hand, had we unpacked our args by assigning $string to some other variable lexically scoped to the subroutine, done a substitution (probably triggering copy on write), and then returned the string, we have the opportunity to have created at very least one copy.

Copying small strings and simple values such as integers, and even floating point numbers is pretty fast, and for general cases we probably shouldn't care. But for long strings I can see where it would be useful to be aware of what will and will not trigger a buffer copy.

On the other hand, consider this:

sub dot_to_underscore { my $string = $_[0]; # No copy on 5.20+, but yes, copy under <5.20 +. $string =~ s/\./_/; # Copy made under 5.20 because of copy-on-wri +te. return $string; # No copy under 5.20+ until the *caller* modi +fies the return string. But yes, the caller will get a copy pre-5.20. }

Dave

Replies are listed 'Best First'.
Re^2: perl string pass by value
by LanX (Saint) on Apr 02, 2018 at 15:30 UTC
    >  for general cases we probably shouldn't care

    IIRC COW (Copy On Write) is also relevant for forking/threading

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Wikisyntax for the Monastery

Re^2: perl string pass by value
by pwagyi (Monk) on Apr 03, 2018 at 06:01 UTC

    Good point on @_ is alias entity. Unfortunately, when sub argument is supposed to be hash, example in a typical subroutine call like this, callee will *unpack* the arguments, and copying is un-avoidable. I have millions of such kind of calls. I've yet to benchmark perl <5.20 and 5.20+. If someone had already made a study on this, it'd be good to know if there's a big performance difference.

    Foo->new( arg1=> $a_very_long_string, arg2 => $float, arg3=> $another_ +long_string); # in new sub new { my ($class,%args) = @_; }

      Yes, that was an unfortunate design decision when this could have avoided the issue:

      sub new { my ($class, $args_href) = @_; .... }

      Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1212167]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-19 10:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found