I'm taking a Perl class and and I'm working on comprehending this script
in one of the lessons. This script has you count words in a file. The
line where one is supposed to read in the file being counted is like so:
while (defined($line = <>))
However, when I type this in: perl countwords.pl history.txt
Nothing happens. This lesson that I'm working on is working from the
premise that people are using a Windows Perl installation. So it looks
like I need to change the line above so that it will work in my setup.
What would be a good way to change this line?

Signature
Lola - mailto:lola@his.com
http://www.lolajl.net | Blog at http://www.lolajl.net/blog/
Terrorism delenda est! (Terrorism must be destroyed utterly!)
I'm in Bowie, MD, USA, halfway between DC and Annapolis.
Marco Baroni - 26 Dec 2004 09:20 GMT
> while (defined($line = <>))
This line looks good in any standard setup (e.g., using perl on the
terminal in osx). What is your setup? Are you sure that the problem
is not elsewhere?
Regards,
Marco
Lola Lee - 26 Dec 2004 12:31 GMT
Marco Baroni said the following on 12/26/04 4:20 AM:
> This line looks good in any standard setup (e.g., using perl on the
> terminal in osx). What is your setup? Are you sure that the problem
> is not elsewhere?
Hmm . . . looks like the script that John Delacour provided works. So
something's gotta be wrong with the one I typed in. Going to have to go
back and compare it with my printout of the lesson to make sure I didn't
leavy anything out.

Signature
Lola - mailto:lola@his.com
http://www.lolajl.net | Blog at http://www.lolajl.net/blog/
Terrorism delenda est! (Terrorism must be destroyed utterly!)
I'm in Bowie, MD, USA, halfway between DC and Annapolis.
John Delacour - 26 Dec 2004 10:34 GMT
>This script has you count words in a file. The line where one is
>supposed to read in the file being counted is like so:
[quoted text clipped - 4 lines]
>
>Nothing happens.
The line you quote simply puts each line of a putative file into the
scalar variable $line. What "happens" will depend on how you deal
with $line and where you print your results.
Supposing that countwords.pl is a script something like the one below
and that history.txt is a file in the same directory (say your home
directory), then you will get output in the terminal as below when
you run your command. I personally would substitute for your line:
foreach $line (<>)
which does the same thing and, to me at least, is simpler and clearer.
###################
#!/usr/bin/perl
foreach (@ARGV) {
while (defined ($line = <>)) {
@words = split /\s+/, $line;
$wordcount = @words;
print "$wordcount, ";
$total += $wordcount;
}
print "\n$_....Total: $total \"words\"\n";
}
###################
_TERMINAL_
eremita:~ jd$ cd
eremita:~ jd$ perl countwords.pl histoire.html
7, 2, 4, 2, 4, 6, 5, 4, 2, 6, 2, 0, 1, 1, 0, 3, 1, 1,
histoire.html....Total: 51 words
eremita:~ jd$
.
Ken Williams - 26 Dec 2004 17:32 GMT
>> This script has you count words in a file. The line where one is
>> supposed to read in the file being counted is like so:
[quoted text clipped - 17 lines]
>
> which does the same thing and, to me at least, is simpler and clearer.
It doesn't quite do the same thing. The 'while' loop will read one
line, then process it, then read the next line, then process it, and so
on. The 'foreach' loop will read all the lines into memory, then
process each of them one by one. So if it's a large file, you'll have
the whole thing in memory at once.
The simplest, if you like using the default variable $_, is this:
while (<>) {
...
}
which is shorthand for:
while (defined($_ = <>)) {
...
}
-Ken
Doug McNutt - 26 Dec 2004 13:12 GMT
>Nothing happens. This lesson that I'm working on is working from the premise that people are using a Windows Perl installation.
Watch out for line ends in the source file that is being counted. Perl probably doesn't care because the return-linefeed pair from Windoze still contains the single linefeed that UNIX expects. But I have been fooled, especially with Unicode's two new code points representing line ends.

Signature
--> Halloween == Oct 31 == Dec 25 == Christmas <--
Conrad Schilbe - 26 Dec 2004 17:51 GMT
>
>
[quoted text clipped - 4 lines]
>
>
Also, I believe that the snippet suggested could stop if there is a
blank line in the file! Although, it would still contain a linefeed, I
rarely trust interpretation of such.
while (defined($line = <>))
I prefer this method:
while ($line = <>) {
next if ($line =~ /^\s*$/);
}
-- cs
John Horner - 27 Dec 2004 00:46 GMT
Short answer: show us the whole script, and tell us about the file
"history.txt".
The most likely explanation for why "do something for each line of file
x" would do nothing is that there are no lines in file x, right? That
is, it will do something three times for a three-line file, and zero
times for a zero-line file, or a file that doesn't exist. It's not
failing, it's doing what it's told to do.
So where is history.txt, and what does it contain? Is it in the same
folder as countwords.pl for a start?
jh
> I'm taking a Perl class and and I'm working on comprehending this
> script in one of the lessons. This script has you count words in a
[quoted text clipped - 9 lines]
> looks like I need to change the line above so that it will work in my
> setup. What would be a good way to change this line?