Hi Adrian,
> Victor Churchill wrote:
> > ( for comparison Perl can be a minefield, where any of the null
> > string '', the integer 0, and an undefined value undef are all
> > false.)
>
> It hardly ever causes problems in Perl land though since all the
> 'false' values correspond to 'empty' in one way or another (empty
> string, zero number, zero length array, empty hash, etc.) Once you're
> used to the convention it reads quite naturally IMHO.
I agree in general, empty/zero values are false, but Perl is
inconsistent as it treats the non-empty string "0" as false. That's one
of the things I think Python `fixed' over Perl.
$ for s in 0 1 '""' '"0"' '"00"' '"0e42"' '"1"'; do
> printf '%8s %1d %1d %1d\n' $s \
> "$(awk "BEGIN {if ($s) print 1}")" \
> "$(perl -wle "$s and print 1")" \
> "$(python -c "if $s: print 1")"
> done
0 0 0 0
1 1 1 1
"" 0 0 0
--> "0" 1 0 1
"00" 1 1 1
"0e42" 1 1 1
"1" 1 1 1
$
It comes about because of Perl's polymorphism with scalars; they're
both strings and numbers at the same time. Perl took this from awk
where it is very handy in the typically short awk programs of the time
but it seems detrimental to me once programs grow longer than a few
lines.
Note, the disagreement with awk and perl above for "0" despite my saying
Perl was inspired by awk stems from awk being more subtle about its
treatment of the string "0" being false; it is false when it comes from
user input, e.g. the environment or read from a file, but as a constant
in source code it's true!
$ for s in '' 0 1 bar; do FOO=$s awk 'ENVIRON["FOO"]' <<<$s; done
1
bar
$
$ for s in 0 1 bar; do awk "\"$s\"" <<<$s; done
0
1
bar
$
Cheers, Ralph.
--
Next meeting: ???, ???day, 2012-11-?? 20:00
Meets, Mailing list, IRC, LinkedIn, ... http://dorset.lug.org.uk/
New thread on mailing list: mailto:[email protected]
How to Report Bugs Effectively: http://goo.gl/4Xue