Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

perl can find the file but can not open the file

by ShermW0829 (Acolyte)
on Mar 09, 2013 at 18:46 UTC ( #1022605=perlquestion: print w/ replies, xml ) Need Help??
ShermW0829 has asked for the wisdom of the Perl Monks concerning the following question:

I have opened, read from, and written to files using PERL in the past. Now I think I am doing the same thing but the open states the below answers.

Found "linux_complete_command_set.txt" Unable to open linux_complete_command_set.txt: No such file or directo +ry

Below is the code. The file exists within the same directory where I am running the PERL file hence the "./". I tried running the file without the ./ with the same results.

#! /usr/bin/perl -w use v5.14.2; use strict; my $file = "linux_complete_command_set.txt"; if ( -e "$file" ) { print "\"Found $file\"\n"; } open ( FH0, '<./$file' ) or die "Unable to open $file: $!\n"; while ( <FH0> ) { print "$_\n"; } 1;

Comment on perl can find the file but can not open the file
Select or Download Code
Re: perl can find the file but can not open the file
by choroba (Abbot) on Mar 09, 2013 at 18:49 UTC
    Please, show the actual error message. There should be a colon and an additional text. Also, the name of the file probably has not lost the "complete_" part.

    Update: Single quotes do not interpolate. Use double quotes if you want a variable to be expanded.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Thank you. I have updated the messages to reflect the correct output. I have tried without success to copy from the terminal to this web page so I have just been typing in the code information and I do make mistakes when I do that. I did that in my last post as well. With my apologies; Sherman

Re: perl can find the file but can not open the file
by McA (Deacon) on Mar 09, 2013 at 19:37 UTC

    Hi,

    I'm not sure whether your problem is solved, the right hint was given by choroba. With the code line

    open ( FH0, '<./$file' ) or die "Unable to open $file: $!\n";
    you try to open a file with the name './$file' and not with the filename './linux_complete_command_set.txt' as you like. The single quotes suppress variable expansion.

    Another reason to use the three parameters form of open

    open (FH0, '<', $file) or die ...

    Best regards
    McA

Re: perl can find the file but can not open the file
by graff (Chancellor) on Mar 09, 2013 at 19:42 UTC
    You're using quotes where you don't need to, and as indicated above, the single-quotes in the open statement are not only unnecessary, but cause the "file not found" error. Try it like this:
    die "$file not found\n" unless ( -e $file ); open( FH0, $file ) or die "Unable to open $file: $!\n"; ...
    In that version, I took advantage of the fact that "open" assumes "for reading" as a default; if the file name returns "true" with '-e', then it exists. But there might still be issues with being able to read it. If '-r' returns true for the file, then it's virtually certain that "open" for reading will succeed.
Re: perl can find the file but can not open the file
by karlgoethebier (Curate) on Mar 09, 2013 at 20:30 UTC

    ...like this:

    #! /usr/bin/perl + # use v5.14.2; # don't have this + use strict; use warnings; my $file = shift || die $!; print qq("Got that $file!"\n) if -e $file; open ( my $fh, "<", $file) || die qq(Couldn't open $file: $!\n); print while <$fh>; close $fh; __END__ Karls-Mac-mini:monks karl$ ./ShermW0829.pl linux_complete_command_set. +txt "Got that linux_complete_command_set.txt!" Reboot that box! Go away, you don't exist!

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: perl can find the file but can not open the file
by toolic (Chancellor) on Mar 10, 2013 at 02:30 UTC
    perlcritic can identify such errors:
    [ValuesAndExpressions::RequireInterpolationOfMetachars] String *may* require interpolation at line 12, near \'FH0, \'<./$file\ +'\'. (Severity: 1)'

      Thank you to everyone. This has been an informative experience. It all started because I ran the script I created and somehow it took the text file I was reading from and emptied it which I didn't notice. So I ran the script again without any errors but without any returns either. So I thought the script was bad which it was but I have since fixed. But since I didn't know exactly what the script was doing I wrote a smaller try script which is the one we are talking about. It returned no errors but no results either since I was still working with the null text file. So I searched the internet and found the site that suggested the single quotes and so here we now are.

      Installing Perl::Critic was fun since I had to find and install numerous modules to get it past the make test. Is there a way to find all the dependencies before I try to install a module from CPAN?

      Again thank you to all. Sherman

        So I searched the internet and found the site that suggested the single quotes

        So your question boils down to: "In the following code, I used the exact same string for a file test and in open(), yet my code doesn't work as expected:

        if ( -e "$file" ) open ( FH0, '<./$file' )

        Stripping away the code, one can only wonder why you think the two strings:

        "$file"
         '<./$file' 
        

        ...are the same. There are several differences.

        So I searched the internet and found the site that suggested the single quotes

        You can learn about the difference between double quotes and single quotes, as well as many other useful things, like how to properly open() a file, which you aren't doing, in "Learning Perl 6th".

        Is there a way to find all the dependencies before I try to install a module from CPAN?

        Well, most people don't install from source. Instead, they just type:

        $ cpan Some::Module

        and the cpan command handles downloading and installing all the dependencies. However, the first time you try to use the cpan command, you have to do some setup, which may or may not go smoothly.

        More recent versions of perl come with the cpanm command, which stands for cpan minus, which is like cpan, but with no setup required:

        $ cpanm Some::Module

        And if you don't have cpan minus, you can download the App::cpanminus module from cpan.

        There are two kinds of perl, modern perl and the shite that existed before that. You seem to be learning the shite. I suggest you alter course.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1022605]
Approved by graff
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (16)
As of 2014-07-23 20:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (152 votes), past polls