Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: pass by value vs reference and return by value

by ww (Bishop)
on Oct 27, 2013 at 16:07 UTC ( #1059916=note: print w/ replies, xml ) Need Help??


in reply to pass by value vs reference and return by value

As to return by value, an example:

#!/usr/bin/perl use 5.016; use warnings; use Data::Dumper; # pass by ref; return by val my $foo = "abc"; sub subr { my $local_foo = shift; # see Note1, below my $local_deref = $$local_foo; say " In sub, \$local_deref: $local_deref"; $local_deref .= " def"; say "\t And now, after cat, \$local_deref: $local_deref"; return $local_deref; } my $fooref = \$foo; my $result = subr($fooref); say "\t\t Back in main, after subr has returned."; say "\t\t $result";

Produces output:

In sub, $local_deref: abc And now, after cat, $local_deref: abc def Back in main, after subr has returned. abc def

Note1: Had you attempted to -- for example --  print "DEBUG: \$local_foo: $local_foo"; before the deref you would be advised:

 DEBUG: $local_foo: SCALAR(0x169ef64)
... where the hex value (address) will vary with machine, OS or, possibly, whether the butterfly's wings are flapping upward or downward in the jungles or Borneo.


If you didn't program your executable by toggling in binary with a bat-handle switch, it wasn't really programming!


Comment on Re: pass by value vs reference and return by value
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (13)
As of 2014-09-30 15:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (377 votes), past polls