CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Reading a File


New User

Jun 4, 2013, 3:32 PM

Post #1 of 2 (1037 views)
Reading a File Can't Post


I have a .txt file I'm trying to open with Perl, but whenever I write a program to open it, nothing happens (or at least, I don't see anything).

The name of the file is prot.txt. It contains protein sequences. Anyhew, this is how the program I've written looks like:

$proteinfilename = 'prot.txt';
open(PROTEINFILE, $proteinfilename);
@protein = <PROTEINFILE>;
print @protein;

However, when I run the program using the command prompt, there are no visible results. Any thoughts about what might be going on? Thanks.

Veteran / Moderator

Jun 4, 2013, 5:16 PM

Post #2 of 2 (1029 views)
Re: [Eukaryoticus] Reading a File [In reply to] Can't Post

You need to check the return code of the open call to make sure it was successful and take proper action if it wasn't.

You should also:
1) load the warnings and strict pragmas and declare your vars.
2) use a lexical var for the filehandle instead of the bareword.
3) use the 3 arg form of open.

use strict; 
use warnings;

my $protein_filename = 'prot.txt';

open my $protein_fh, '<', $protein_filename or die "failed to open '$protein_filename' $!";

my @protein = <$protein_fh>;
close $protein_fh;

print @protein;


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives