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

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



Paul M. Hoffman wrote:

>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

A cute trick you can use in conjunction with this is the following:

sample2(undef, 0, '', '0', '0e0', 1);

sub sample2 {
   for (@_) {
     if ($_) {
         $x = $_ + 0;
         print "$_ = $x\n"
     }
     else { print "<no argument passed>\n" }
   }
}

__END__

...where the output is:

<no argument passed>
<no argument passed>
<no argument passed>
<no argument passed>
0e0 = 0
1 = 1

It's a way to pass a non-false value of zero to a function.

I picked this up working with MySQL (a DBMS) which uses this trick.

-David Steffen-
David Steffen, Ph.D.
President, Biomedical Computing, Inc. <http://www.biomedcomp.com/>
Phone: (713) 610-9770 FAX: (713) 610-9769 E-mail: steffen@biomedcomp.com

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