Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Open a file

by liljohn (Initiate)
on Aug 12, 2019 at 18:07 UTC ( #11104339=perlquestion: print w/replies, xml ) Need Help??

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

I am running Windows 10 and am using Perl 5.8. I can not open a file from a Perl script no matter how I try. My most recent code is as follows:

my $filename = "C/Perl64/Perl Scripts/Names.txt"; open(my $fh,'<:encoding(UTF-8)', $filename) || die "Could not open fil +e";

Replies are listed 'Best First'.
Re: Open a file
by Corion (Pope) on Aug 12, 2019 at 18:08 UTC

    Let Perl tell you the reason why it failed:

    my $filename = "C/Perl64/Perl Scripts/Names.txt"; open(my $fh,'<:encoding(UTF-8)', $filename) || die "Could not open file '$filename': $!";

      Thank you so much, Once I used your suggested code and saw the message "no such file" I realized I had omitted part of the file path.</pP

        Don't you love it when a plan comes together so easily? :)

        By the way, welcome to the Monastery, liljohn! I've just got a small suggestion here... you've done an excellent job of following this site's posting syntax, but it appears as though you've missed a couple of closing tags. The </p> closing tag in this post, and the </code> tag in your Original Post (OP) don't have the > in place. Any chance you could go back and edit them in? If not, our Editors will get notified and will have to do that for you.

        Keep up the good work here. A newcomer responding back to a helpful post goes a long way.

Re: Open a file
by dasgar (Priest) on Aug 12, 2019 at 20:15 UTC

    I think that the issue is in your first line of code. You're missing the ':' in the path. It probably should be:

    my $filename = "C:/Perl64/Perl Scripts/Names.txt";

      I know you are trying to help, but we like to teach fishing instead of simply giving away fish. Corion does an excellent job of teaching fishing (Re: Open a file) with immediate positive results (Re^2: Open a file) which will help liljohn many times in the future. This lesson is teaching the importance of writing good diagnostics into the code which can be used in many different ways and is a great lesson in Perl laziness (do it right the first time).

      Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2019-10-17 03:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?