#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    11
    Rep Power
    0

    Question File open operation


    Hi,

    I am new to Perl and started learning recently.

    I tried the following perl program to read the contents of the file :

    #!/usr/local/bin/perl

    if (open(MYFILE, "file1")) {
    print ("file opened success \n");
    $line = <MYFILE>;
    while ($line ne "")
    {
    print ($line);
    $line = <MYFILE>;
    }
    }

    The result is blank when I run this program even though "file1" exists in correct directory and is not blank.

    Please hep ..
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    That works for me. Not sure why it doesn't work for you. Anyway, try the below alternative:

    Code:
    #! /usr/bin/local/perl
    
    my ($fh, $line);
    open ($fh, "< file1");
    if ($fh) { print "file opened success\n" }
    while ($line = <$fh>) {
        print $line;
    }
    close $fh;
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,923
    Rep Power
    1225
    The most likely reason for no output would be that the first line in the file is blank which would cause the first iteration of the while loop evaluate to false and terminate without printing anything.

    1) You should ALWAYS use the strict and warnings pragmas.

    2) You should be using a lexical var for the filehandle instead of a bareword.

    3) You should use the 3 arg form of open.

    4) The while loop should loop over the filehandle.

    Code:
    #!/usr/local/bin/perl
    
    use strict;
    use warnings;
    
    open my $fh, '<', 'file1' or die "failed to open 'file1' $!";
    
    while (my $line = <$fh>) {
        print $line;
    }
    
    close $fh;
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    11
    Rep Power
    0
    Hi,

    I tried this but again it dint work for me


    Originally Posted by noobie1000
    That works for me. Not sure why it doesn't work for you. Anyway, try the below alternative:

    Code:
    #! /usr/bin/local/perl
    
    my ($fh, $line);
    open ($fh, "< file1");
    if ($fh) { print "file opened success\n" }
    while ($line = <$fh>) {
        print $line;
    }
    close $fh;
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    11
    Rep Power
    0

    Unhappy


    Hi,

    I have checked that the first line is not blank and tried the program you have given ..

    It says "Failed to open file1 . No such file or directory "

    Now what to do ...

    Originally Posted by FishMonger
    The most likely reason for no output would be that the first line in the file is blank which would cause the first iteration of the while loop evaluate to false and terminate without printing anything.

    1) You should ALWAYS use the strict and warnings pragmas.

    2) You should be using a lexical var for the filehandle instead of a bareword.

    3) You should use the 3 arg form of open.

    4) The while loop should loop over the filehandle.

    Code:
    #!/usr/local/bin/perl
    
    use strict;
    use warnings;
    
    open my $fh, '<', 'file1' or die "failed to open 'file1' $!";
    
    while (my $line = <$fh>) {
        print $line;
    }
    
    close $fh;
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,923
    Rep Power
    1225
    Where is the file located?

    Is it in the same directory as the script? If not, you'll need to specify the path.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    11
    Rep Power
    0
    Yes, its located in the same path as the script ..


    Originally Posted by FishMonger
    Where is the file located?

    Is it in the same directory as the script? If not, you'll need to specify the path.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,923
    Rep Power
    1225
    Check the spelling and and case. *nix systems are case sensitive, so if the filename is File1 instead of file1, then the open call will fail.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    11
    Rep Power
    0
    Hmm.. yes.. those were the first things I checked when the program dint execute ...but alas ! ! !

    Originally Posted by FishMonger
    Check the spelling and and case. *nix systems are case sensitive, so if the filename is File1 instead of file1, then the open call will fail.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    Are you executing the perl script directly? Or from a wrapper script located elsewhere? Can you try providing the absolute path in the open routine?

    Code:
    open ($fh, "< /absolute/path/to/file1");
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    11
    Rep Power
    0
    Phew ! ! Finally absolute path worked ... OMG and I dint realise this. .. Thank you sooooo soo much

    Originally Posted by noobie1000
    Are you executing the perl script directly? Or from a wrapper script located elsewhere? Can you try providing the absolute path in the open routine?

    Code:
    open ($fh, "< /absolute/path/to/file1");
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    Originally Posted by NancyAnna
    Phew ! ! Finally absolute path worked ... OMG and I dint realise this. .. Thank you sooooo soo much
    No, not providing the absolute path is not the problem. Remember FishMonger had asked a question if the perl script and "file1" are located in the same path. If it actually did, then you need not provide the absolute path. Please re-check.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    11
    Rep Power
    0
    But actually, both the scripts are in the same directory only ...


    Originally Posted by noobie1000
    No, not providing the absolute path is not the problem. Remember FishMonger had asked a question if the perl script and "file1" are located in the same path. If it actually did, then you need not provide the absolute path. Please re-check.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,923
    Rep Power
    1225
    Were you executing the script from the same directory, or were you in some other directory?

    If you were in a different directory than the script and file you were opening and didn't specify the path in the open call, perl will look for the file in the current working directory and won't find it.
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    11
    Rep Power
    0
    Yes FishMonger ... I was executing script from the same directory only ..
    anyways thanks so much for ur help .. i was breaking my head for one whole day on this.



    Originally Posted by FishMonger
    Were you executing the script from the same directory, or were you in some other directory?

    If you were in a different directory than the script and file you were opening and didn't specify the path in the open call, perl will look for the file in the current working directory and won't find it.

IMN logo majestic logo threadwatch logo seochat tools logo