-
Perl open function
've just started doin Perl on my own...
I have a doubt.
the following program named openpage.pl is not working and shows the error below
open (page,"<text.txt");
while<page>{print;}
D:\SCRIPTS>perl openpage.pl
syntax error at openpage.pl line 2, near "while<page>"
Execution of openpage.pl aborted due to compilation errors.
-
try:
Code:
open(PAGE, "text.txt") or die("could not open text.txt\n");
while(<PAGE>){
print;
}
The reason I made it capitalized is because that's a convention in perl. Otherwise, the parenthesis should make it work for you. It may have worked if you had put spaces between while and <page> also, not sure. Hope this helps.
-
There's a ton of ways to skin a cat in perl, here's another:
Another note on the open, input redirection is not needed if you are just reading a file, it is only needed if you are writing to it (> file). Also, don't forget to close the file once you are done.
Code:
open(PAGE, "text.txt") || die "Open text.txt failed ($!)\n";
my @lines = <PAGE>;
close(PAGE);
print @lines;
-
Thanx for the response..
I'll try them and do let you know.
-
Thanx a lot!
It worked very well..
Can i request a little more info on the writing and just opening files with the open function..
I also wanna know the conventions.
-
perl.com
perl.org
perlmonks.org
cpan.org
should give you all the info you need, and for a book that should help pick up the perl Cookbook. Gives plenty of examples and should help you.