[comp.lang.perl] strings in boolean context

tneff@bfmny0.UU.NET (Tom Neff) (03/29/90)

I wonder if it would be inviting disaster to suggest that "quoting"
an expression always force string context rather than integer.  So

	$x = 0;

	print "Just another " unless $x;
	print "Perl hacker," if "$x";

would yield output that makes Randal happy. :-)

Right now 0 and "0" both evaluate false, while 00 is false and "00" true.
Of course you can 'say what you mean' and use ($x ne "") instead, which
might even be better programming practice in many cases...
-- 
If the human mind were simple enough to understand,  =))  Tom Neff
we'd be too simple to understand it. -- Pat Bahn     ((=  tneff@bfmny0.UU.NET

schwartz@psuvax1.cs.psu.edu (Scott Schwartz) (03/29/90)

Is it really a good idea to allow strings automagically be coerced to
integers in boolean context?  I think changing that behavior is nicer
than some of the fixed to Felix's program that have floated by.   Would
that break any old code?

By the way, in the newly distributed lib/syslog.pl there is a hardcoded
reference to /usr/local/lib/perl/syslog.h.  Shouldn't Configure fix
that up?


-- 
Scott Schwartz		schwartz@cs.psu.edu

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (03/30/90)

In article <Ex6f-04@cs.psu.edu> schwartz@psuvax1.cs.psu.edu (Scott Schwartz) writes:
: Is it really a good idea to allow strings automagically be coerced to
: integers in boolean context?  I think changing that behavior is nicer
: than some of the fixed to Felix's program that have floated by.   Would
: that break any old code?

First of all, no coercion happens in a boolean context.  If it has a valid
string value, it's false if it's specifically "0" or "".  Otherwise, if
it has a numeric value, it's false if it equals 0.0.  Otherwise, it's undefined
and therefore false.

Suppose we change "0" to be true.  What do you do if it's both a number and
a string?  The fact is, when you say literal 0, it is store both ways
(currently, anyway).  I can't really make people keep track of what's
been evaluated in which context.  It's bad enough to describe magical
increment in those terms.  I think you really want to say ne '' when
you mean that, like Tom says.  It's just as fast as the numeric comparison,
anyway.

: By the way, in the newly distributed lib/syslog.pl there is a hardcoded
: reference to /usr/local/lib/perl/syslog.h.  Shouldn't Configure fix
: that up?

Not directly.  But it should just do 'syslog.h', and rely on @INC (which
is, in fact, set up by Configure).

Larry

flee@shire.cs.psu.edu (Felix Lee) (03/30/90)

Well, the common idiom
	while (<>)
will fail on the pathological case where a file ends with a single "0"
without a terminating newline.  Of course, that will never happen.

I don't think making "0" true in perl is a great idea.  But then, I'm
more enamored with Icon's idea of success/failure.
--
Felix Lee	flee@shire.cs.psu.edu	*!psuvax1!flee