Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
There's more than one way to do things
 
PerlMonks  

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!

Comment on readline() on unopened filehandle
Select or Download Code
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 (Monsignor) 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 (Chaplain) 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2014-04-17 23:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (458 votes), past polls