http://www.perlmonks.org?node_id=992577

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 (Patriarch) 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 (Canon) 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.
                       -SNL

Re: readline() on unopened filehandle
by aitap (Curate) 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.