merlyn may want to use a map() function, but a *really* good function handles scalars or arrays as inputs. To that end, here is an example that runs all the test cases I could think of.
#!/usr/local/bin/perl -w use strict; { test1 (); test2 (); test3 (); test4 (); } # # Uses a simple scalar # sub test1 { my $i = "Hello, World"; print "return - '", remove_caps ($i), "', \$i ='$i'\n"; } # # Simple scalar, result is set to a different variable # sub test2 { my $i = "Hello, World"; my $o = remove_caps ($i); print "return - \$i='$i', \$o='$o'\n"; } # # Pass a constant # sub test3 { print "constant - ", remove_caps ("Hello, World"), "\n"; } # # Operate on an array # sub test4 { my @iarray = ('Hello, World', 'Goodbye, Cruel World', 'Britney Spea +rs Sux'); my @oarray = remove_caps (@iarray); print "iarray = "; print "'$_', " foreach (@iarray); print "\n"; print "oarray = "; print "'$_', " foreach (@oarray); print "\n"; } # # Our humble little function # sub remove_caps { my @out = @_; tr/A-Z//d for (@out); return wantarray ? @out : $out[0]; }
--Chris

e-mail jcwren

In reply to (jcwren) RE: modify variable on pass by value by jcwren
in thread modify variable on pass by value by Anonymous Monk

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.