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

Re: [FWP] thoughts on while(defined(EXP warnings



>>>>> "DLN" == David L Nicol <david@kasey.umkc.edu> writes:

  DLN> perl -w warns me that is is safer to write constructions
  DLN> of the form

  DLN> while($line=<HANDLE>){

  DLN> as

  DLN> while(defined($line=<HANDLE>)){

  DLN> and I surely agree.

RTFM

	If and ONLY if the input symbol is the only thing
	inside the conditional of a while or for(;;) loop, the value
	is automatically assigned to the variable $_.  In these loop
	constructs, the assigned value (whether assignment is
	automatic or explicit) is then tested to see if it is
	defined.  The defined test avoids problems where line has a
	string value that would be treated as false by perl e.g. ""
	or "0" with no trailing newline. 

	Anyway, the following lines are
	equivalent to each other:

	    while (<STDIN>) { print; }
	    for (;<STDIN>;) { print; }
	    print while defined($_ = <STDIN>);
	    print while ($_ = <STDIN>);
	    print while <STDIN>;

	and this also behaves similarly, but avoids the use of $_ :

	    while (my $line = <STDIN>) { print $line }

NOTE the above line.

	In other boolean contexts, <filehandle> without explicit
	defined test or comparison will solicit a warning if -w is
	in effect.

so with 5.005_03, you don't need defined when doing a while and
assigning <> to a var. you must be using an older perl

perl -we 'while( $l = <> ) {print "[$l]\n"}'
jdje
[jdje
]
0[0]

i typed ^D after the 0 with no newline. no warnings. 5.004_04 gives out
nasty warnings.

uri

-- 
Uri Guttman  ---------  uri@sysarch.com  ----------  http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page  -----------  http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net  ----------  http://www.northernlight.com

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe