-
December 3rd, 2006, 07:32 PM
#1
Junior Member
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.
-
December 4th, 2006, 02:51 AM
#2
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.
-
December 4th, 2006, 02:10 PM
#3
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;
There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.
(Merovingian - Matrix Reloaded)
-
December 4th, 2006, 04:26 PM
#4
Junior Member
Thanx for the response..
I'll try them and do let you know.
-
December 5th, 2006, 12:32 AM
#5
Junior Member
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.
-
December 5th, 2006, 06:03 AM
#6
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|