lets say i have a subroutine i want to pass a variable, and the sub should modify it, this would normally require passing a refreence:
#define the sub
sub strip_space
{
# get our parameter
my $val = $_[0];
# if not given a reference, return false
return false if ref($val) ne "SCALAR";
# remove whitespace and retrn true on success
$$val =~ s/\s+//g;
return 1;
}
# and call said sub
$test = "this will have no spaces!";
print $test if strip_space($test);
print $test if strip_space(\$test);
the first print would never take place because a reference isnt passed to the sub, so it returns false, whereas the second print would work as expected. i remember to pass it a
reference to a variable rather than just the variable itself.
but is there a way to not have to explicitly pass a reference to the sub? isnt there a way i can just use strip_space($test) every time and from within the sub grab a reference to $test?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.