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!


In reply to Re: pass by value vs reference and return by value by ww
in thread pass by value vs reference and return by value by ganeshPerlStarter

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.