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

I am a beginner in PERL..learning the basics..using Strawberry Perl on Windows Vista

I created a text file 'fragment.txt' and saved it in the same directory..

I have used the following code to open the file

#!/usr/bin/perl -w $proteinfilename = 'fragment.txt'; open(PROTEINFILE, $proteinfilename); $protein = <PROTEINFILE>; close PROTEINFILE; print " here is the protein:\n\n"; print $protein;

but the output I get is:

readline() on closed filehandle PROTEINFILE at C:\strawberry\perl\bin\ +trial1.pl line 4 here is the protein: Use of uninitialized value $protein in print at C:\strawberry\perl\bin +\trial1.pl line 7

I face the same problem with trying to open any other file as well..

Replies are listed 'Best First'.
Re: Cannot open file in Strawberry Perl
by Corion (Patriarch) on Apr 25, 2011 at 11:46 UTC

    You don't check for errors so it's likely that opening the file failed and you didn't notice.

    Either use the autodie pragma at the top of your file:

    use autodie;

    ... or manually check for failure:

    open PROTEINFILE, $proteinfilename or die "Couldn't open '$proteinfilename': $!";
Re: Cannot open file in Strawberry Perl
by syphilis (Archbishop) on Apr 25, 2011 at 11:50 UTC
    Hi,
    I can't reproduce the problem. All works well for me:
    C:\_32\pscrpt>type fragment.txt Hello Workload !! More stuff C:\_32\pscrpt>type try.pl #!/usr/bin/perl -w $proteinfilename = 'fragment.txt'; open(PROTEINFILE, $proteinfilename); $protein = <PROTEINFILE>; close PROTEINFILE; print " here is the protein:\n\n"; print $protein; C:\_32\pscrpt>perl try.pl here is the protein: Hello Workload !! C:\_32\pscrpt>
    As you can see, your script outputs only the first line of fragment.txt.

    Cheers,
    Rob
Re: Cannot open file in Strawberry Perl
by ww (Archbishop) on Apr 25, 2011 at 12:25 UTC
    For your future reference, please use markup to make your posts more readily legible.

    <c>...</c> around code and data and <p>...</p> to mark paragraphs. THIS IS SPELLED OUT AROUND THE INPUT TEXT BOX... and explained in geater detail in Markup in the Monastery.

    And, it's "Perl" but not "PERL." Granted, this is minor, but the difference is important to a few persnickety didacts.

    More important, for you as a "beginner in PERL" is to incloude use strict in your header. It's apt to toss out notice of problems you won't even recognize without it. And use warnings is often a better choice than -w which may prompt warnings from modules (you'll want to be using some of them, soon) about coding that's perfectly alright.

    Attend also to Corion's note about error checking (and while you're at it, read about 3 arg opens.

Re: Cannot open file in Strawberry Perl
by Balaton (Initiate) on May 09, 2012 at 20:35 UTC
    I have exactly the same problem, I know that the file cannot be opened, but I cannot figure out why. I am using Active Perl on Windows7. Thanks!
Re: Cannot open file in Strawberry Perl
by Balaton (Initiate) on May 10, 2012 at 21:44 UTC
    Hi, I am also a beginner, however, I have just realized what went wrong. You must have had the same problem. Your Perl program was written correctly, but it was more likely that your file that you wanted to open was not good. The file must be saved with WordPad and NOT with NotePad because with WordPad you can choose to save the files as "Text document-MS_DOS" format and if it is a peptide you also need to give a ".pep" extension. I hope it helps, let me know if it works now.
      Thanks mate ! It works for me !

      Hey the Word Pad advise works for me. Thanks!!