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

Parsing Sequence Records

by lomSpace (Scribe)
on May 07, 2014 at 19:48 UTC ( #1085370=perlquestion: print w/replies, xml ) Need Help??

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

Hello, I'm getting the following error message, ~/Desktop/Pipelines/ readline() on closed filehandle $in. I can get the data to stdout, which is fine for my pipeline, but I would also like to have the actual file. Thank you PerlMonk for you infinite wisdom! Here's the code:

#!/usr/bin/perl -w use strict; open(my $in,"/Users/Desktop/Pipelines/fungi.1.aa.hypothetical.faa"); open(my $out,">Users/Desktop/Pipelines/fungi.1.aa.hypothetical50.faa") +; local $/ = undef; my @chunks = split(/>/, <$in>); my @hypothetical50 = @chunks[0..49]; for my $hypothetical50(@hypothetical50){ if($hypothetical50 =~ s/^gi/>gi/g){ print $out "$hypothetical50\n"; } } close($in); close($out);

Replies are listed 'Best First'.
Re: Parsing Sequence Records
by toolic (Bishop) on May 07, 2014 at 19:51 UTC
Re: Parsing Sequence Records
by thezip (Vicar) on May 07, 2014 at 20:09 UTC

    You might also consider using the three argument form of open:

    open(my $in, '<', "/Users/Desktop/Pipelines/fungi.1.aa.hypothetical.fa +a"); open(my $out, '>', "Users/Desktop/Pipelines/fungi.1.aa.hypothetical50. +faa");

    *My* tenacity goes to eleven...
      I definitely agree with the three-argument syntax you are suggesting, but I think that MidLifeXis's comment was even more important: checking whether the file could be open. I would therefore suggest this possible syntax:
      my $input_file = "/Users/Desktop/Pipelines/fungi.1.aa.hypothetical.faa +"); my $output_file = "Users/Desktop/Pipelines/fungi.1.aa.hypothetical50.f +aa"); open my $in, '<', $input_file or die "could not open $input_file $!"; open my $out, '>', $output_file or die "could not open $output_file $! +";

        I always use autodie, so its a moot point for me... :-)

        *My* tenacity goes to eleven...

      thezip: That was simple enough. One of my Duhh moments. Thanks!

Re: Parsing Sequence Records
by farang (Chaplain) on May 08, 2014 at 01:07 UTC

    I'm guessing that the reason for the open failure is that the correct path should be something like this.

    I'll assume that below. The advice other monks have given is excellent if you are using the traditional open function, but in the spirit of "there is more than one way to do it" here is another approach.

    The module Path::Tiny is great, and I tend to look to it first when opening files. After installing it, the following could be done.

    ... use Path::Tiny; my $in = path('/Users/lomSpace/Desktop/Pipelines/fungi.1.aa.hypothetic +al.faa')->slurp; my $out = path('/Users/lomSpace/Desktop/Pipelines/fungi.1.aa.hypotheti +cal50.faa')->openw; my @chunks = split(/>/, $in); ...
    The slurp method on the input file path eliminates the need for local $/ = undef; in your code and the readline, that is the angle bracket pair inside the split. It does mean that $in is no longer a filehandle while $out still is so it might be worth renaming one of the variables to make that clear.

    Path::Tiny also croaks automatically on failure with good error messages. One small downside to it may be that it is not in the core and must be installed from CPAN. Besides opening files, Path::Tiny performs other useful functions as well.

Re: Parsing Sequence Records
by MidLifeXis (Monsignor) on May 07, 2014 at 19:53 UTC

    open(...) or die "Error: $!" is a good habit to get into. See also use autodie (autodie).


      open my $fIn, '<', $filename or die "Can't open '$filename': $!";

      is an even better habit. The quoted file name in the error message lets you see what was actually being opened - unchomped input anyone?

      Perl is the programming world's equivalent of English

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1085370]
Approved by toolic
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2022-12-07 22:40 GMT
Find Nodes?
    Voting Booth?

    No recent polls found