Re: WHILE and DEFINED problem
by valdez (Monsignor) on Mar 05, 2004 at 15:41 UTC
|
| [reply] |
|
|
Oopss... Seems that defined is not really needed. Thankie very muchie, Valerio! That is quite an article. I'll try to absorb it anyway. Ciao! :)
| [reply] |
|
|
| [reply] [d/l] |
Re: WHILE and DEFINED problem
by demerphq (Chancellor) on Mar 05, 2004 at 16:03 UTC
|
local @ARGV=qw(c:/tmp.txt);
while (<>) {
chomp;
print "\$_: '$_'\n";
}
And its easier to get right too. ;-)
Update after a moments reflection: My point being that no its not better to use defined, or any of the other common pieces of advice about avoiding perl idiom. The idiom exists to make your life easy and to reduce trivial and common coding errors. If you want to have to hassle with stuff like that then do it in C.
Update 2: Read the link valdez posted. ++ to him. Obviously its clear which camp im in, but I can see there are good points raised in that thread about using defined(). I still wouldnt though.
---
demerphq
First they ignore you, then they laugh at you, then they fight you, then you win.
-- Gandhi
| [reply] [d/l] [select] |
Re: WHILE and DEFINED problem
by Thelonius (Priest) on Mar 05, 2004 at 15:45 UTC
|
chomp returns "the total number of characters removed from all its arguments." In $qline is undef, chomp returns 0, which is defined. | [reply] |
|
|
OIC, now it's clear. Yes, you're right, Have just verified everything out. chomp does return a value of 1 by default for every <> read-in. It actually ties with the contents $/.
ICIC! Thanks again, Thelonius!
| [reply] |
|
|
| [reply] |
Re: WHILE and DEFINED problem
by kutsu (Priest) on Mar 05, 2004 at 15:58 UTC
|
$qfile = "c:/tmp.txt";
if (-s $qfile) # man perlfunc has more -s = if exist with size > 0
{
open QINPUT...
}
"Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce
| [reply] [d/l] |
Re: WHILE and DEFINED problem
by dragonchild (Archbishop) on Mar 05, 2004 at 18:41 UTC
|
Both valdez and demerphq have excellent (and overlapping) points. I would like to point out that the proximate issue is the use of defined() and chomp() in the while statement. chomp() never returns an undefined value. Better (though I personally write it as demerphq does) would have been:
while (chomp(my $qline = <QINPUT>))
{
# yadda, yadda, yadda
}
Though, this will fail to read the last line if it doesn't contain a newline at the end of it. (Similar to the "Incomplete last line" error vi gives you in the same situation.)
Just do what demerphq or valdez (through the link provided) suggest.
------
We are the carpenters and bricklayers of the Information Age.
Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.
| [reply] [d/l] |
Re: WHILE and DEFINED problem
by zakzebrowski (Curate) on Mar 05, 2004 at 19:15 UTC
|
Aside, I've tried to re-write your code in a way I like it (which is slightly different than other ppl...
open (IN,"<" . "c:/tmp.txt") or die "Doh: $!"; # Use ()'s, and added o
+r die... sometimes that comes in hand when debugging.
while(<IN>){ # $_ is set to each line for each file.
my $line = $_; # Take raw form of line
$line =~ chomp $line; # chomp the line.
if (length($line)){ # does line have non zero length? (Sorta what y
+ou're doing.)
print "$line\n";
}
}
close IN; # Clean up.
$.02
| [reply] [d/l] |
|
|
while (<$in>) {
chomp(my $line=$_);
next unless length $line;
print;
}
__END__
# please excuse this stuff here, just doing some pmdev testing with th
+is node :-)
# it will be removed soon.
# No indent
# single space
# two spaces
# three spaces
# four spaces
# No indent
# single space
# two spaces
# three spaces
# four spaces
:-)
---
demerphq
First they ignore you, then they laugh at you, then they fight you, then you win.
-- Gandhi
| [reply] [d/l] [select] |