First a few notes about the code.
On Sun, Jan 16, 2011 at 12:18 PM, galeb abu-ali <[email protected]> wrote:
> #!/usr/bin/perl
> # create_gb_library.pl
> use strict; use warnings;
>
> open IDFILE, shift or die "can't read idfile!\n";
You should probably add some error checking here to make sure that you
get the right arguments passed. It will also make the code easier to
follow. You should also use lexical file handles and the 3-argument
open: see "perldoc -f open" for details. The two arguent version that
you're using interprets the second argument in special ways that could
result in executing a process instead of opening a file.
For example:
# Assuming the program should always have exactly 1 argument:
die "Usage: $0 id_file" unless @ARGV == 1;
# Store the file name in a variable for clarity.
my $id_file_name = shift @ARGV;
# Use 3-argument open, with a lexical file handle.
# Also output $! in die message so the user knows
# what's wrong.
open my $id_fh, '<', $id_file_name or
die "Failed to open id file '$id_file_name': $!";
> my @ids = '';
>
> while( <IDFILE> ) {
> my $filename = $_;
This would be cleaner if the file name was declared on the while line:
while(my $filename = <$id_fh>)
{
> push( @ids, $filename );
> }
>
> my @library;
>
> for my $id( @ids ) {
If $id is a file name then a better name would probably be
$id_file_name, but then we already have said variable from above. Are
these the same types of files or perhaps the names are ambiguous? For
now, we might as well us $filename, as we did reading them in.
for my $filename (@ids)
{
> chomp $id;
> open FILE, $id or die "can't open file $id!\n";
Again, better with the 3-argument open, a lexical file handle, and
outputting $!:
open my $fh, '<', $filename or
die "Failed to open file '$filename': $!";
> $/ = "//\n"; # input-record separator is genbank end-of-record
> separator
I think that you should limit the scope of that so that its affects
are temporary.
local $/ = "//\n";
> my $record = <FILE>;
my $record = <$fh>;
> push( @library, $record)
> }
> close FILE;
If you're going to explicitly close this file (IIRC, a lexical file
handle will close it automatically when it goes out of scope) then you
should probably do it at the end of the loop where it was opened.
Also, close can fail (though I think unlikely here) so you might as
well check for success and warn.
close $fh or warn "Failed to close file '$filename': $!";
}
> open OUTLIBRARY, ">gblibrary.txt" or die "can't open gblibrary.txt!\n";
The 3-arg open might be preferred here too and for certain a lexical
file handle would still be preferred. Again, you should output the $!
variable if it fails:
my $out_file_name = 'gblibrary.txt';
my open $out_fh, '>', $out_file_name or
die "Failed to open output file '$out_file_name': $!";
> print OUTLIBRARY @library, "\n";
I'm not sure if it's necessary, but with lexical file handles I always
enclose the handle in a block to be sure Perl knows what I mean:
print { $out_fh } @library, "\n";
> close OUTLIBRARY;
Again, I think the lexical file handle should automatically close this
file, but if we're going to close it we might as well be explicit
about it.
close $out_fh or
die "Failed to close output file '$out_file_name': $!";
Now on to why it isn't working.
> opening individual files. The code is below. I successfully populate
> @ids but cannot manage to loop through @ids, open files and
> populate @library as I get "can't open file !\n"; messages. Please
> advise at your convenience,
How do you know that you successfully populated @ids with the correct
data? Have you output it to see? To be certain, you can use
Data::Dumper to output the raw array.
use Data::Dumper;
print Dumper \@ids;
The message "can't open file !\n" suggests that your file name
variable is empty, which would explain why opening fails. So confirm
what is in the array. You can also dump the value of the file name
variable:
print Dumper \$filename;
Look up Data::Dumper on CPAN for detailed usage (you could try perldoc
too: perldoc Data::Dumper). I would guess that the problem is the @ids
array not containing what you think it does. Outputting the $!
variable in your die calls should also help to identify problems,
though it looks like it won't add anything extra this time.
--
Brandon McCaig <http://www.bamccaig.com> <[email protected]>
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software <http://www.castopulence.org/> <[email protected]>
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/