Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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.#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);
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: get a reference implicitly within a subroutine call?
by xdg (Monsignor) on Jun 23, 2006 at 15:16 UTC | |
|
Re: get a reference implicitly within a subroutine call?
by shmem (Chancellor) on Jun 23, 2006 at 15:19 UTC | |
|
Re: get a reference implicitly within a subroutine call?
by Zaxo (Archbishop) on Jun 23, 2006 at 15:20 UTC | |
|
Re: get a reference implicitly within a subroutine call?
by EvanK (Chaplain) on Jun 23, 2006 at 15:42 UTC | |
by Limbic~Region (Chancellor) on Jun 23, 2006 at 16:15 UTC |