Unfortunately is seems like George didn't get that joke :-(
jue
What joke? Mirco made the perl syntax match my previous notions, and when
you don't transpose letters in the filename, it works:
C:\MinGW\source>perl faulk4.pl
Sun 18h 41m 55s -23â–‘ 5.4' 0.983 10.215 52.155 Up
Mercury 20h 2m 16s -22â–‘ 12.5' 1.102 22.537 37.668 Up
Venus 21h 55m 33s -14â–‘ 16.3' 0.795 39.872 11.703 Up
Moon 21h 17m 19s -15â–‘ 2.4' 62.4 ER 36.796 22.871 Up
Mars 18h 11m 59s -24â–‘ 6.1' 2.431 4.552 56.184 Up
Jupiter 20h 3m 35s -20â–‘ 49.4' 6.034 23.867 38.203 Up
Saturn 11h 32m 59s +5â–‘ 8.6' 9.018 -47.333 157.471 Set
Uranus 23h 21m 30s -4â–‘ 57.9' 20.421 48.328 -18.527 Up
Neptune 21h 39m 30s -14â–‘ 22.8' 30.748 38.963 16.599 Up
C:\MinGW\source>type faulk4.pl
open(50, '<', 'eph4.txt') or die "cannot open";
DO:
{ my $line = readline(*50);
if(eof != 0) { exit }
print $line; # no 'write' here
redo DO } # no 'end' possible
close(50)
# perl faulk4.pl
C:\MinGW\source>
The thing I needed to do to make this work was step away from the machine.
You saw the transposition, and now I see that the io difficulties I was
having have disappeared.
For the sake of thoroughness, let me just list the scripts that I now have
for the identical purpose:
C:\MinGW\source>type faulk1.pl
#The manuals don't really say it but files have their own data type.
#This line creates a variable called file or exits saying that it can't
open
#the file with a possible explanation (the $! variable).
open( FILE, "eph4.txt") || die "Could not open ehp3.txt $!";
#read out one line at a time until the file ends
while( <FILE> ){
print $_; #If you don't specify a file it goes to STDOUT (STandard
OUTpu
t)
#The $_ variable is created by the while statement
}
close(FILE); #This might not really be needed
# perl faulk1.pl
C:\MinGW\source>type faulk2.pl
#!/usr/bin/perl
use strict;
use warnings;
my $filename = 'eph3.txt';
open my $handle, '<', $filename
or die "unable to open '$filename' because $!";
while (<$handle>) {
s/%%/%\n/;
print;
}
close $handle;
# perl faulk2.pl
C:\MinGW\source>type faulk7.pl
open(my $fh, '<', 'eph3.txt');
while (my $line = <$fh>) {
print $line;
}
close($fh)
# perl faulk7.pl
C:\MinGW\source>type faulk8.pl
open(my $fh, '<', 'eph3.txt');
while (<$fh>) {
print $_;
}
close($fh)
# perl faulk8.pl
C:\MinGW\source>type faulk9.pl
open(my $fh, '<', 'eph3.txt') or die "cannot open eph3.txt: $!";
while (<$fh>) {
print $_;
}
close($fh)
# perl faulk9.pl
C:\MinGW\source>type faulk10.pl
my $filename = 'eph3.txt';
open(my $fh, '<', $filename) or die "cannot open $filename: $!";
while (<$fh>) {
print $_;
}
close($fh)
# perl faulk10.pl
C:\MinGW\source>
Page 21 of the camel book is the reference a person needs for this. The
'<' means explicitly to read from an existing file.
Peter also gave two counterexamples to help illustrate what goes wrong:
C:\MinGW\source>perl faulk5.pl
C:\MinGW\source>type faulk5.pl
open(my $fh, '<eph3.txt>');
DO:
{ my $line = readline($fh);
if(eof != 0) { exit }
print $line;
redo DO }
close($fh)
# perl faulk5.pl
C:\MinGW\source> perl faulk6.pl
C:\MinGW\source>type faulk6.pl
open(my $fh, '<', 'eph3.txt>');
DO:
{ my $line = readline($fh);
if(eof != 0) { exit }
print $line;
redo DO }
close($fh)
# perl faulk6.pl
C:\MinGW\source>
Does someone have a reference for the three-argument form for open?
I'd like to thank everyone who responded to this thread; it was a learning
experience for me. But now that I have achieved this great thing, I'll
hope to process the file that gets opened.
Maybe I should start a new thread for that.
--
George
I think we ought to raise the age at which juveniles can have a gun.
George W. Bush
Picture of the Day
http://apod.nasa.gov/apod/