ketaki has asked for the wisdom of the Perl Monks concerning the following question:

is there any perl module required for the code below , its a code to read and print the contents of a text file. ITs not printing anything, dont know why. it doesnt even show an error.
open(frame,'../frame.txt') or die "error: $!"; while(<frame>) { print "$_\n"; } close(frame);

Replies are listed 'Best First'.
Re: Problem printing contents of file
by Corion (Patriarch) on Jun 17, 2008 at 13:58 UTC

    If all you're telling is true, the only explanation is that ../frame.txt is an empty file. Note that it is a good practice to mention the filename as well. Also, it is a bad practice to use relative filenames unless you know which directory is the current directory when your script is run. Notably, webservers set a different current directory when they run scripts. The following code works for me, but it is not substantially different from your code:

    #!/usr/bin/perl -w use strict; #my $filename = '../frame.txt'; my $filename = $0; open my $fh, '<', $filename or die "Couldn't open '$filename': $!"; while (<$fh>) { print "$_\n"; };
      you said webservers set a diffrent current directory when they run the script.. probably this could be the reason. how am i to know which current directly are they using?
        how am i to know which current directly are they using?

        Your script can make use of the Cwd module for that. (But, as stated elsewhere, the best approach is to use a full path to frame.txt instead of the relative path that you have been using. When you use a full path it doesn't matter where the current working directory is.)

        Cheers,
        Rob

        Use the getcwd function from Cwd, or, even better, use absolute path names.

Re: Problem printing contents of file
by psini (Deacon) on Jun 17, 2008 at 13:58 UTC

    On my PC it works perfectly (indeed, I had no doubt of it).

    On which OS are you running it?

    Have you added strictures (use strict; use warnings;)?

    Does your post contain the entire file?

    How do you execute it?

    Most important: please use <code> tags when posting code here; it will be more readable and downloadable.

    Careful with that hash Eugene.

      the os on my comp is windows xp, but this script runs on the linux based server. I added the strictures you suggested and still it doesnt show any output. the file frame.txt i checked is not empty. I am executing it by viewing the page register.cgi on the browser in internet, after uploading the file on the server. Mt post does not contain the entire code in the program.... below is the whole file:
      #!/usr/bin/perl print "content-type:text/html\n\n"; use CGI; use strict; use warnings; print<<html1; <html> <head> <title>Register online</title> </head> <body style="background-color:#BC9FAC;"> html1 open(frame,'../frame.txt'); while(<frame>) { print "$_\n"; } close(frame); print<<html2; <center> <div style="margin:0 auto;width:750px;height:420px;"> html2
      and when i put this code into it:
      my $file = "../frame.txt"; if(-s ($file) >0) { # open(FILE, '<', $file) or die $!; # while(<FILE>) { # print $_, "\n"; # } # close(FILE); } else { print "$file contains no data\n"; }
      then it outputs ../frame.txt contains no data. but i checked the file frame.txt.... it does contain text.
Re: Problem printing contents of file
by kabeldag (Hermit) on Jun 17, 2008 at 14:10 UTC
    You don't need a module. Does the file contain data? Open like this:
    open(FILE, '<', '../frame.txt') or die $!; while(<FILE>) { print $_, "\n"; } close(FILE);
    To check the size of a file, you can do something like the following:
    my $file = '../frame.txt'; if(-s ($file) >0) { open(FILE, '<', $file) or die $!; while(<FILE>) { print $_, "\n"; } close(FILE); }else{ print "$file contains no data\n"; }
    7 hours sleep in 2 days is not healthy

      If you're going to tell him to use 3-arg open, you might as well recommend lexical file handles and a better error message too.

      And I find it's usually better to check what you got rather than checking what you will get.

      my $frame_qfn = 'temp'; open(my $frame_fh, '<', $frame_qfn) or die("Unable to open frame file $frame_qfn: $!\n"); while (<$frame_fh>) { print; } if (!$.) { print "$frame_fh contains no data\n"; }
      i tried ur script to check the file size... it goes to the else part and says ../frame.txt contains no data. but i checked and the file frame.txt is not empty. this code is in register.cgi file in cgi-bin directory and the file frame.txt is in the folder above cgi-bin folder.so am i writing '../frame.txt' correctly?

        Please read my reply to you. In it, I mention that, when running under a webserver, the current directory likely is not what you think. Do not use relative filenames when running under a webserver.

        You could have avoided a large part of this wild goose chase by making it clear from the start, in your top post, that you are running your script under a webserver account as a CGI script. That would've made it more clear to us where a likely cause for the error lies.

Re: Problem printing contents of file
by GrandFather (Saint) on Jun 17, 2008 at 21:15 UTC

    A whole bunch of stuff:

    1. Using a bad title for your node (see How do I compose an effective node title?).
    2. Not using any markup (see Perl Monks Approved HTML tags).
    3. Not previewing your node or, having previewed your node, not bothering to tidy it up.
    4. Not using correct capitalization and other grammar errors.
    5. Not putting code in code tags

    Perl is environmentally friendly - it saves trees
Re: Problem printing contents of file
by kabeldag (Hermit) on Jun 17, 2008 at 13:58 UTC
    The file exists? The file contains data..? Open like this:
    open(FILE, '<', '/frame.txt') or die $!; while(<FILE>) { print $_, "\n"; } close(FILE);
Re: Problem printing contents of file
by Anonymous Monk on Jun 17, 2008 at 13:58 UTC
    You've got the wrong pants :) and the file is probably empty :) no modules required