[Bioperl-l] difference in opening file from @ARGV and STDIN?
Cook, Malcolm
MEC at stowers-institute.org
Thu Sep 11 20:19:25 UTC 2008
Note exactly the way I would put it.
Look at the difference between the first command and the second is the following transcript:
> echo -e ">asdf\natgc\n" | perl -MBio::SeqIO -e 'my $s = Bio::SeqIO->new(-format => qw{fasta}, -fh => \*ARGV); print $ARGV[0] . qq{ has } . $s->next_seq()->seq . qq{\n}' -- '-'
- has atgc
> echo -e ">asdf\natgc\n" | perl -MBio::SeqIO -e 'my $s = Bio::SeqIO->new(-format => qw{fasta}, -fh => \*ARGV); print $ARGV[0] . qq{ has } . $s->next_seq()->seq . qq{\n}' -- 'NoSuchFile'
Can't open NoSuchFile: No such file or directory at /home/mec/cvs/bioperl-live/Bio/Root/IO.pm line 458.
Can't call method "seq" on an undefined value at -e line 1.
THe only difference is that @ARG is the singleton list composed of '-' in the first call, and is the singlton list composed of 'NoSuchFile' in the second.
If you passed in a list of multiple files that actually do exist, it should work fine.
It is really a matter of ARGV processing magic.
from http://perldoc.perl.org/perlop.html
The null filehandle <> is special: it can be used to emulate the behavior of sed and awk. Input from <> comes either from standard input, or from each file listed on the command line. Here's how it works: the first time <> is evaluated, the @ARGV array is checked, and if it is empty, $ARGV[0] is set to "-", which when opened gives you standard input. The @ARGV array is then processed as a list of filenames. The loop
while (<>) {
... # code for each line
}
is equivalent to the following Perl-like pseudo code:
unshift<http://perldoc.perl.org/functions/unshift.html>(@ARGV, '-') unless @ARGV;
while ($ARGV = shift<http://perldoc.perl.org/functions/shift.html>) {
open<http://perldoc.perl.org/functions/open.html>(ARGV, $ARGV);
while (<ARGV>) {
... # code for each line
}
}
Malcolm Cook
Database Applications Manager - Bioinformatics
Stowers Institute for Medical Research - Kansas City, Missouri
________________________________
From: dave at davemessina.com [mailto:dave at davemessina.com] On Behalf Of Dave Messina
Sent: Thursday, September 11, 2008 1:47 PM
To: Cook, Malcolm
Cc: Felipe Figueiredo; bioperl-l at lists.open-bio.org
Subject: Re: [Bioperl-l] difference in opening file from @ARGV and STDIN?
Thanks, Malcolm.
So then, '-' as STDIN does work?
D
More information about the Bioperl-l
mailing list