[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl-AnyPerl] Re: macperl-anyperl-digest V1 #58



Richard L. Grubb wrote:

>Is there any way (or maybe I should say, How many ways are there) in
>Perl to get a subroutine to recognize a null argument?

Use undef and defined():

sample1(1, undef, 2);

sub sample1 {
  for (@_) {
    if (defined($_)) { print "$_\n" }
    else { print "<undef>\n" }
  }
}

Output:
1
<undef>
2

If you don't care to distinguish between 0 and undef (both are false), you
can do this:

sample2(undef, 0, 1);

sub sample2 {
  for (@_) {
    if ($_) { print "$_\n" }
    else { print "<undef or zero>\n" }
  }
}

Output:
<undef or 0>
<undef or 0>
1


Paul.

--
Paul Hoffman :: Taubman Medical Library :: Univ. of Michigan
nkuitse@umich.edu :: http://www-personal.umich.edu/~nkuitse/



==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-anyperl-request@macperl.org