Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

readline() on unopened filehandle

by invaderzard (Acolyte)
on Sep 09, 2012 at 11:19 UTC ( #992577=perlquestion: print w/replies, xml ) Need Help??
invaderzard has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl use Modern::Perl; use File::Slurp qw/read_file write_file/; my $pfaminput ='D://ARP//Downloads//Trembl//toxinpospf.txt'; my $seedinput ='D://ARP//Downloads//PFACFULL.txt'; #open OUTPUT, ">", 'D://ARP//Downloads//trembl//Toxin-NegativeACClist. +txt'; #select OUTPUT; while (<$pfaminput>){ my $pfam = $_; print $pfam; #while (<$seedinput>) { # my $blah = substr $_, 0, 5; # if ($blah=<$pfaminput>) { #print $_; # } # #} }

Hi all. When I execute this code, I get: readline() on unopened filehandle at <insertprogramnamehere> line 9

This is my line 9

while (<$pfaminput>){

Why is this? Please help and many thanks!

Replies are listed 'Best First'.
Re: readline() on unopened filehandle
by Corion (Pope) on Sep 09, 2012 at 11:23 UTC

    So, Perl tells you that the filehandle is not opened.

    Where did you open the filehandle to your file?

    See open or maybe use File::Slurp?

Re: readline() on unopened filehandle
by NetWallah (Abbot) on Sep 09, 2012 at 14:11 UTC
    Since you are just a few months into perl, here is the canonical way to do this (Note - I changed the name and meaning of your variable for clarity):
    my $pfaminput_filename ='D:/ARP/Downloads/Trembl/toxinpospf.txt'; # Double fwd-slash is not necessary open (my $pfaminput_filehandle , "<" , $pfaminput_filename ) or die "Can't open '$pfaminput_filename' : Error is : $!"; while (my $pfam = <$pfaminput_filehandle>){ print $pfam; } close $pfaminput_filehandle;
    Update: Corrected variable name inside error message.

                 I hope life isn't a big joke, because I don't get it.

Re: readline() on unopened filehandle
by aitap (Deacon) on Sep 09, 2012 at 15:13 UTC
    $pfaminput is just a scalar containing file path, not a filehandle. You need to open a filehandle in order to read from it.
    Sorry if my advice was wrong.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://992577]
Approved by moritz
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2017-09-22 21:48 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (269 votes). Check out past polls.