In article <B88D6CD0.475EMon, 11 Feb 2002 12:42:56 [EMAIL PROTECTED]> wrote 
"Dr-Nikon"
<[EMAIL PROTECTED]>:

> [localhost:~/Perl] tor% cat test
> this is line nr 1
> this is line nr 2
> etc..
> ..
> this is the last line in the file
> 
> [localhost:~/Perl] tor% cat 23.pl
> #!/usr/bin/perl
> 
> if(open(FILE1, "/Users/tor/Perl/test")) {
                                  ^^^^
On a *nix system I would never give a file or a script 
the name test.
test is a system command on unix and
if the file doen't exist,
unix would call the test command and redirect it's output 
to your script.

>         $line = <FILE1>;
>            print ("This is the contents of the file\n");
>            while($line ne " ") {

Every read line ends on a line termination, \n, \r\n or \r.
The line termination is then a part of $line, too.
To avoid confusion, the command chomp removes the line termination.

Again, when there are no lines in the file any more,
<FILE1> returns the empty string "", not the one space string " ".

>            print ($line);
>            $line = <FILE1>;
>            }
> }

The standard way (avoiding many error possibilities)
of opening a file and reading it line by line is:
open FILE, "<filename" or die "Cannot open filename: $!";
while (chomp (my $line = <FILE>)) {
        # do something with $line
} 
close FILE;

or quite shorter:
open FILE, "<filename" or die "Cannot open filename: $!";
while (<FILE>) {
        chomp;
        # do something with $_
}

Best Wishes,
Andrea

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to