Results 1 to 6 of 6

Thread: Perl open function

  1. #1
    Junior Member
    Join Date
    Jan 2006
    Location
    Cuttack, IND
    Posts
    18

    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.
    Somu

  2. #2
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    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.

  3. #3
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    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)

  4. #4
    Junior Member
    Join Date
    Jan 2006
    Location
    Cuttack, IND
    Posts
    18

    Smile

    Thanx for the response..
    I'll try them and do let you know.

  5. #5
    Junior Member
    Join Date
    Jan 2006
    Location
    Cuttack, IND
    Posts
    18

    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.
    Somu

  6. #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
  •