From 2d7429ae14b32fe8717cf19a695f451a1e85ef7c Mon Sep 17 00:00:00 2001 From: "Francisco J. Ossandon" Date: Fri, 28 Feb 2014 13:22:49 -0300 Subject: [PATCH] Massive check of file open lines. Changed bareword filehandles to lexical with three-argument opens calls, added die/throw and reason of failure $! where missing, and made error messages more uniform and consistent with Florent Angly messages for Root (like quoting of filenames). Also deleted a few unused open filehandles and some unnecessary parenthesis. Tests pass. --- Bio/Align/Graphics.pm | 22 +-- Bio/AlignIO.pm | 4 +- Bio/Assembly/IO/bowtie.pm | 6 +- Bio/Assembly/Tools/ContigSpectrum.pm | 2 +- Bio/DB/BioFetch.pm | 6 +- Bio/DB/Flat.pm | 4 +- Bio/DB/Flat/BinarySearch.pm | 39 ++-- Bio/DB/GFF.pm | 7 +- Bio/DB/GFF/Adaptor/berkeleydb.pm | 10 +- Bio/DB/HIV.pm | 28 +-- Bio/DB/Query/HIVQuery.pm | 10 +- Bio/DB/Registry.pm | 5 +- Bio/DB/SeqFeature/Store/DBI/Pg.pm | 6 +- Bio/DB/SeqFeature/Store/DBI/SQLite.pm | 3 +- Bio/DB/SeqFeature/Store/berkeleydb.pm | 13 +- Bio/DB/SeqHound.pm | 20 ++- Bio/DB/TFBS/transfac_pro.pm | 236 ++++++++++++++++--------- Bio/DB/Taxonomy/flatfile.pm | 16 +- Bio/DB/Taxonomy/greengenes.pm | 2 +- Bio/DB/Taxonomy/silva.pm | 2 +- Bio/DB/WebDBSeqI.pm | 3 +- Bio/Index/Abstract.pm | 4 +- Bio/Index/Blast.pm | 2 +- Bio/Index/BlastTable.pm | 2 +- Bio/Index/EMBL.pm | 2 +- Bio/Index/Fasta.pm | 2 +- Bio/Index/Fastq.pm | 2 +- Bio/Index/GenBank.pm | 3 +- Bio/Index/Hmmer.pm | 2 +- Bio/Index/Qual.pm | 2 +- Bio/Index/Stockholm.pm | 3 +- Bio/Index/SwissPfam.pm | 2 +- Bio/Index/Swissprot.pm | 2 +- Bio/LiveSeq/IO/Loader.pm | 4 +- Bio/MapIO/fpc.pm | 8 +- Bio/PhyloNetwork/GraphViz.pm | 6 +- Bio/PullParserI.pm | 2 +- Bio/SimpleAlign.pm | 2 +- Bio/Tools/Alignment/Consed.pm | 31 ++-- Bio/Tools/Lucy.pm | 22 +-- Bio/Tools/Run/RemoteBlast.pm | 44 ++--- Bio/Tools/Run/WrapperBase/CommandExts.pm | 6 +- Bio/Tools/ipcress.pm | 6 +- Bio/Tree/Draw/Cladogram.pm | 2 +- Bio/Tree/Tree.pm | 2 +- Bio/Tree/TreeFunctionsI.pm | 2 +- Build.PL | 4 +- doc/Deobfuscator/bin/deob_index.pl | 2 +- doc/Deobfuscator/cgi-bin/deob_interface.cgi | 8 +- examples/contributed/rebase2list.pl | 7 +- examples/db/rfetch.pl | 5 +- examples/make_primers.pl | 6 +- examples/subsequence.cgi | 6 +- examples/tools/extract_genes.pl | 8 +- maintenance/authors.pl | 55 +++--- maintenance/check_URLs.pl | 2 +- maintenance/cvs2cl_by_file.pl | 53 +++--- maintenance/dependencies.pl | 4 +- maintenance/deprecated.pl | 7 +- maintenance/find_mod_deps.pl | 4 +- maintenance/modules.pl | 15 +- scripts/Bio-DB-GFF/bp_genbank2gff.pl | 11 +- scripts/Bio-DB-GFF/bp_genbank2gff3.pl | 4 +- scripts/Bio-DB-GFF/bp_process_gadfly.pl | 18 +- scripts/das/bp_das_server.pl | 6 +- scripts/popgen/bp_composite_LD.pl | 5 +- scripts/searchio/bp_fastam9_to_table.pl | 2 +- scripts/searchio/bp_hmmer_to_table.pl | 2 +- scripts/searchio/bp_parse_hmmsearch.pl | 6 +- scripts/searchio/bp_search2table.pl | 2 +- scripts/seq/bp_split_seq.pl | 7 +- scripts/seqstats/bp_chaos_plot.pl | 8 +- scripts/seqstats/bp_oligo_count.pl | 14 +- scripts/tree/bp_blast2tree.pl | 12 +- scripts/utilities/bp_download_query_genbank.pl | 8 +- scripts/utilities/bp_netinstall.pl | 38 ++-- scripts/utilities/bp_revtrans-motif.pl | 2 +- scripts/utilities/bp_search2tribe.pl | 2 +- t/RemoteDB/HIV/HIVAnnotProcessor.t | 9 +- t/RemoteDB/HIV/HIVQuery.t | 10 +- t/SearchIO/blast.t | 4 +- t/Seq/MetaSeq.t | 13 +- t/SeqFeature/Collection.t | 6 +- t/SeqIO/Handler.t | 12 +- t/SeqIO/SeqIO.t | 9 +- t/SeqIO/ace.t | 14 +- t/SeqIO/embl.t | 8 +- t/SeqIO/fasta.t | 6 +- t/SeqIO/gcg.t | 8 +- t/SeqIO/genbank.t | 15 +- t/SeqIO/largefasta.t | 2 +- t/SeqIO/mbsout.t | 29 ++- t/SeqIO/msout.t | 8 +- t/SeqIO/raw.t | 7 +- t/SeqIO/swiss.t | 3 +- t/Tools/Genewise.t | 7 +- t/Tools/Genomewise.t | 4 +- t/Tools/Run/WrapperBase.t | 2 +- t/Tree/TreeIO/newick.t | 6 +- t/Tree/TreeIO/nhx.t | 8 +- t/Variation/Variation_IO.t | 14 +- 101 files changed, 615 insertions(+), 531 deletions(-) diff --git a/Bio/Align/Graphics.pm b/Bio/Align/Graphics.pm index f348160ac..2eb93deed 100644 --- a/Bio/Align/Graphics.pm +++ b/Bio/Align/Graphics.pm @@ -278,24 +278,24 @@ $self->_domain_label(); if($self->{output}) { - open(OUTPUT, ">$self->{output}"); - binmode OUTPUT; + open my $OUTPUT, '>', $self->{output} or die "Could not read file '$self->{output}': $!\n"; + binmode $OUTPUT; if(defined($self->{out_format})) { SWITCH: { - if($self->{out_format} eq "png") {print OUTPUT $self->{image}->png; last SWITCH;} - if($self->{out_format} eq "jpeg") {print OUTPUT $self->{image}->jpeg; last SWITCH;} - if($self->{out_format} eq "gif") {print OUTPUT $self->{image}->gif; last SWITCH;} - if($self->{out_format} eq "gd") {print OUTPUT $self->{image}->gd; last SWITCH;} + if($self->{out_format} eq "png") {print $OUTPUT $self->{image}->png; last SWITCH;} + if($self->{out_format} eq "jpeg") {print $OUTPUT $self->{image}->jpeg; last SWITCH;} + if($self->{out_format} eq "gif") {print $OUTPUT $self->{image}->gif; last SWITCH;} + if($self->{out_format} eq "gd") {print $OUTPUT $self->{image}->gd; last SWITCH;} } }else { - print OUTPUT $self->{image}->png; + print $OUTPUT $self->{image}->png; } - close OUTPUT; + close $OUTPUT; }else { binmode STDOUT; @@ -303,10 +303,10 @@ if($self->{output}) if(defined($self->{out_format})) { SWITCH: { - if($self->{out_format} eq "png") {print STDOUT $self->{image}->png; last SWITCH;} + if($self->{out_format} eq "png") {print STDOUT $self->{image}->png; last SWITCH;} if($self->{out_format} eq "jpeg") {print STDOUT $self->{image}->jpeg; last SWITCH;} - if($self->{out_format} eq "gif") {print STDOUT $self->{image}->gif; last SWITCH;} - if($self->{out_format} eq "gd") {print STDOUT $self->{image}->gd; last SWITCH;} + if($self->{out_format} eq "gif") {print STDOUT $self->{image}->gif; last SWITCH;} + if($self->{out_format} eq "gd") {print STDOUT $self->{image}->gd; last SWITCH;} } }else diff --git a/Bio/AlignIO.pm b/Bio/AlignIO.pm index cdf4cf4a1..9086d925c 100644 --- a/Bio/AlignIO.pm +++ b/Bio/AlignIO.pm @@ -36,10 +36,10 @@ Bio::AlignIO - Handler for AlignIO Formats use Bio::AlignIO; - open MYIN,"testaln.fasta"; + open MYIN, '<', 'testaln.fasta' or die "Could not read file 'testaln.fasta': $!\n"; $in = Bio::AlignIO->newFh(-fh => \*MYIN, -format => 'fasta'); - open my $MYOUT, '>', 'testaln.pfam'; + open my $MYOUT, '>', 'testaln.pfam' or die "Could not write file 'testaln.pfam': $!\n"; $out = Bio::AlignIO->newFh(-fh => $MYOUT, -format => 'pfam'); diff --git a/Bio/Assembly/IO/bowtie.pm b/Bio/Assembly/IO/bowtie.pm index 04d814014..3a1b9c428 100755 --- a/Bio/Assembly/IO/bowtie.pm +++ b/Bio/Assembly/IO/bowtie.pm @@ -185,7 +185,7 @@ sub _bowtie_to_sam { if ($file =~ m/\.gz[^.]*$/) { $file = $self->_uncompress($file); $self->close; - open (my $fh,$file); + open my $fh, '<', $file or $self->throw("Could not read file '$file': $!"); $self->file($file); $self->_fh($fh); } @@ -279,8 +279,8 @@ sub _bowtie_to_sam { print $samh $PG; # print alignments - open($sam_tmp_h, $sam_tmp_f) or - $self->throw("Can not open '$sam_tmp_f' for reading: $!"); + open $sam_tmp_h, '<', $sam_tmp_f or + $self->throw("Could not read file '$sam_tmp_f': $!"); print $samh $_ while (<$sam_tmp_h>); diff --git a/Bio/Assembly/Tools/ContigSpectrum.pm b/Bio/Assembly/Tools/ContigSpectrum.pm index 94cea71ab..d53d373cd 100644 --- a/Bio/Assembly/Tools/ContigSpectrum.pm +++ b/Bio/Assembly/Tools/ContigSpectrum.pm @@ -2050,7 +2050,7 @@ sub _draw_graph { } $viz->add_edge($id1 => $id2, label => $edge_val); } - open my $fh, '>', $outfile or die "Error: Could not write file '$outfile'\n$!\n"; + open my $fh, '>', $outfile or $self->throw("Error: Could not write file '$outfile': $!"); print $fh $viz->as_png; close $fh; return 1; diff --git a/Bio/DB/BioFetch.pm b/Bio/DB/BioFetch.pm index 4fb421f16..240546314 100644 --- a/Bio/DB/BioFetch.pm +++ b/Bio/DB/BioFetch.pm @@ -431,11 +431,11 @@ sub postprocess_data { } elsif ($args{'type'} eq 'file') { - open (F,$args{'location'}) or $self->throw("Couldn't open $args{location}: $!"); + open my $F, '<', $args{'location'} or $self->throw("Could not read file '$args{location}': $!"); # this is dumb, but the error may be anywhere on the first three lines because the # CGI headers are sometimes printed out by the server... - my @data = grep {defined $_} (scalar ,scalar ,scalar ); - close F; + my @data = grep {defined $_} (scalar <$F>, scalar <$F>, scalar <$F>); + close $F; if (join('',@data) =~ /^ERROR (\d+) (.+)/m) { $self->throw("BioFetch Error $1: $2"); } diff --git a/Bio/DB/Flat.pm b/Bio/DB/Flat.pm index 769088ff4..2ad75fe58 100644 --- a/Bio/DB/Flat.pm +++ b/Bio/DB/Flat.pm @@ -387,7 +387,7 @@ sub write_config { $self->write_flag or $self->throw("cannot write configuration file because write_flag is not set"); my $path = $self->_config_path; - open (my $F,">$path") or $self->throw("open error on $path: $!"); + open my $F, '>', $path or $self->throw("Could not write file '$path': $!"); my $index_type = $self->indexing_scheme; print $F "index\t$index_type\n"; @@ -466,7 +466,7 @@ sub _read_config { my $path = $self->_config_path; return unless -e $path; - open (my $F,$path) or $self->throw("open error on $path: $!"); + open my $F, '<', $path or $self->throw("Could not read file '$path': $!"); my %config; while (<$F>) { chomp; diff --git a/Bio/DB/Flat/BinarySearch.pm b/Bio/DB/Flat/BinarySearch.pm index dc12cfdb7..78fb3dac4 100644 --- a/Bio/DB/Flat/BinarySearch.pm +++ b/Bio/DB/Flat/BinarySearch.pm @@ -436,7 +436,7 @@ sub get_stream_by_id { my $file = $self->{_file}{$fileid}; - open( my $IN, "<$file" ); + open my $IN, '<', $file or $self->throw("Could not read file '$file': $!"); my $entry; @@ -815,7 +815,7 @@ sub build_index { sub _index_file { my ( $self, $file ) = @_; my $v = $self->verbose; - open( my $FILE, "<", $file ) || $self->throw("Can't open file [$file]"); + open my $FILE, '<', $file or $self->throw("Could not read file '$file': $!"); my $recstart = 0; my $fileid = $self->get_fileid_by_filename($file); @@ -931,7 +931,7 @@ sub _index_file { $self->_add_id_position( $id, $pos, $fileid, $length, \%secondary_id ); $count++; - close($FILE); + close $FILE; $count; } @@ -954,9 +954,9 @@ sub write_primary_index { @ids = sort { $a cmp $b } @ids; - open( my $INDEX, ">" . $self->primary_index_file ) - || $self->throw( - "Can't open primary index file [" . $self->primary_index_file . "]" ); + open my $INDEX, '>', $self->primary_index_file + or $self->throw( + "Could not write primary index file '" . $self->primary_index_file . "': $!" ); my $recordlength = $self->{_maxidlength} + @@ -1075,7 +1075,7 @@ sub new_secondary_filehandle { my $secindex = File::Spec->catfile( $indexdir, "id_$name.index" ); - open( my $fh, ">", $secindex ) || $self->throw($!); + open my $fh, '>', $secindex or $self->throw("Could not write file '$secindex': $!"); return $fh; } @@ -1103,7 +1103,7 @@ sub open_secondary_index { $self->throw("Index is not present for namespace [$name]\n"); } - open( my $newfh, "<", $secindex ) || $self->throw($!); + open my $newfh, '<', $secindex or $self->throw("Could not read file '$secindex': $!"); my $reclen = $self->read_header($newfh); $self->{_secondary_filehandle}{$name} = $newfh; @@ -1189,8 +1189,8 @@ sub make_config_file { my $configfile = $self->_config_file; - open( my $CON, ">", $configfile ) - || $self->throw("Can't create config file [$configfile]"); + open my $CON, '>', $configfile + or $self->throw("Could not write config file '$configfile': $!"); # First line must be the type of index - in this case flat print $CON "index\tflat/1\n"; @@ -1205,8 +1205,6 @@ sub make_config_file { print $CON "fileid_$count\t$file\t$size\n"; - my $fh; - open( $fh, "<", $file ) || $self->throw($!); $self->{_file}{$count} = $file; $self->{_dbfile}{$file} = $count; $self->{_size}{$count} = $size; @@ -1244,7 +1242,7 @@ sub make_config_file { my $alpha = $alphabet ? "/$alphabet" : ''; print $CON "format\t" . "$format\n"; } - close($CON); + close $CON; } =head2 read_config_file @@ -1263,8 +1261,8 @@ sub read_config_file { my $configfile = $self->_config_file; return unless -e $configfile; - open( my $CON, "<", $configfile ) - || $self->throw("Can't open configfile [$configfile]"); + open my $CON, '<', $configfile + or $self->throw("Could not read config file '$configfile': $!"); # First line must be type my $line = <$CON>; @@ -1305,8 +1303,6 @@ sub read_config_file { . "]" ); } - my $fh; - open( $fh, "<", $filename ) || $self->throw($!); $self->{_file}{$fileid} = $filename; $self->{_dbfile}{$filename} = $fileid; $self->{_size}{$fileid} = $filesize; @@ -1408,8 +1404,7 @@ sub get_filehandle_by_fileid { $self->throw("ERROR: undefined fileid in index [$fileid]"); } - my $fh; - open( $fh, "<", $self->{_file}{$fileid} ) || $self->throw($!); + open my $fh, '<', $self->{_file}{$fileid} or $self->throw("Could not read file '$self->{_file}{$fileid}': $!"); return $fh; } @@ -1448,9 +1443,9 @@ sub primary_index_filehandle { my ($self) = @_; unless ( defined( $self->{'_primary_index_handle'} ) ) { - open( $self->{'_primary_index_handle'}, - "<" . $self->primary_index_file ) - || self->throw($@); + my $primary_file = $self->primary_index_file; + open $self->{'_primary_index_handle'}, '<', $primary_file + or self->throw("Could not read file '$primary_file': $!\n"); } return $self->{'_primary_index_handle'}; } diff --git a/Bio/DB/GFF.pm b/Bio/DB/GFF.pm index 8458b899a..eb3751e99 100644 --- a/Bio/DB/GFF.pm +++ b/Bio/DB/GFF.pm @@ -1838,10 +1838,10 @@ sub load_gff { && tied *$file_or_directory; my $tied_stdin = tied(*STDIN); - open my $SAVEIN,"<&STDIN" unless $tied_stdin; + open my $SAVEIN, "<&STDIN" unless $tied_stdin; local @ARGV = $self->setup_argv($file_or_directory,'gff','gff3') or return; # to play tricks with reader my $result = $self->do_load_gff('ARGV'); - open STDIN,"<", $SAVEIN unless $tied_stdin; # restore STDIN + open STDIN, '<', $SAVEIN unless $tied_stdin; # restore STDIN return $result; } @@ -1937,7 +1937,7 @@ sub load_fasta { open my $SAVEIN, "<&STDIN" unless $tied; local @ARGV = $self->setup_argv($file_or_directory,'fa','dna','fasta') or return; # to play tricks with reader my $result = $self->load_sequence('ARGV'); - open STDIN,"<", $SAVEIN unless $tied; # restore STDIN + open STDIN, '<', $SAVEIN unless $tied; # restore STDIN return $result; } @@ -3894,4 +3894,3 @@ This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut - diff --git a/Bio/DB/GFF/Adaptor/berkeleydb.pm b/Bio/DB/GFF/Adaptor/berkeleydb.pm index 7a67d8d55..fdf86d8ac 100644 --- a/Bio/DB/GFF/Adaptor/berkeleydb.pm +++ b/Bio/DB/GFF/Adaptor/berkeleydb.pm @@ -246,7 +246,8 @@ sub _open_databases { : $create ? "+>" : "<"; - open (my $F,$mode,$self->_notes_file) or $self->throw($self->_notes_file.": $!"); + my $notes_file = $self->_notes_file; + open my $F, $mode, $notes_file or $self->throw("Could not open file '$notes_file': $!"); $self->{notes} = $F; } @@ -364,7 +365,7 @@ sub load_sequence { my $file = $self->_fasta_file; my $loaded = 0; - open (my $F,">>$file") or $self->throw("Couldn't open $file for writing: $!"); + open my $F, '>>', $file or $self->throw("Could not append file '$file': $!"); if (defined $id) { print $F ">$id\n"; @@ -490,8 +491,9 @@ sub finish_load { sub _touch_timestamp { my $self = shift; my $tsf = $self->_timestamp_file; - open (my $F,">$tsf") or $self->throw("Couldn't open $tsf: $!"); + open my $F, '>', $tsf or $self->throw("Could not write file '$tsf': $!"); print $F scalar(localtime); + close $F; } @@ -1020,7 +1022,7 @@ sub new { : $write ? "+>>" : "<"; - open (my $F,$mode,$dbname) or $class->throw("$dbname: $!"); + open my $F, $mode, $dbname or $class->throw("Could not open file '$dbname': $!"); my $self = bless { fh => $F, next_idx => 0, diff --git a/Bio/DB/HIV.pm b/Bio/DB/HIV.pm index f762f43b6..02fd41910 100755 --- a/Bio/DB/HIV.pm +++ b/Bio/DB/HIV.pm @@ -364,16 +364,15 @@ sub postprocess_data { last; }; m/file/ && do { - local $/; - undef $/; - open (F, "<", $loc) or + local $/ = undef; + open my $F, '<', $loc or $self->throw( - -class=>'Bio::Root::FileOpenException', - -text=>"Error opening tempfile \"$loc\" for reading", - -value=>$loc + -class => 'Bio::Root::FileOpenException', + -text => "Error opening tempfile '$loc' for reading", + -value => $! ); - @data = split( /\n|\r/, ); - close(F); + @data = split( /\n|\r/, <$F>); + close $F; last; }; do { @@ -407,11 +406,11 @@ sub postprocess_data { last; }; m/file/ && do { - open(F, ">", $loc) or $self->throw(-class=>'Bio::Root::FileOpenException', - -text=>'Error opening tempfile \"$loc\" for writing', - -value=>$loc); - print F join("", @flines); - close(F); + open my $F, '>', $loc or $self->throw(-class=>'Bio::Root::FileOpenException', + -text=>"Error opening tempfile '$loc' for writing", + -value=>$!); + print $F join("", @flines); + close $F; last; }; do { @@ -494,8 +493,9 @@ sub get_seq_stream { # this may get reset when requesting batch mode ($rformat,$ioformat) = $self->request_format(); if( $self->verbose > 0 ) { - open(my $ERR, "<", $tmpfile); + open my $ERR, '<', $tmpfile or $self->throw("Could not read file '$tmpfile': $!"); while(<$ERR>) { $self->debug($_);} + close $ERR; } return Bio::SeqIO->new('-verbose' => $self->verbose, diff --git a/Bio/DB/Query/HIVQuery.pm b/Bio/DB/Query/HIVQuery.pm index 99fa5d02a..081acf50f 100755 --- a/Bio/DB/Query/HIVQuery.pm +++ b/Bio/DB/Query/HIVQuery.pm @@ -405,12 +405,12 @@ sub help{ my (@tbls, @flds, @als, @opts, $fh); if ($fname) { - open ($fh, ">", $fname) or $self->throw(-class=>'Bio::Root::IOException', - -text=>"Error opening help html file $fname for writing", - -value=>$!); + open $fh, '>', $fname or $self->throw(-class => 'Bio::Root::IOException', + -text => "Error opening help html file $fname for writing", + -value => $!); } else { - open($fh, ">&1"); + open $fh, ">&1"; } @tbls = $schema->tables; @tbls = ('COMMAND', grep !/COMMAND/,@tbls); @@ -1544,5 +1544,3 @@ sub _sorry{ } 1; - - diff --git a/Bio/DB/Registry.pm b/Bio/DB/Registry.pm index 3078909e3..c5b635dca 100644 --- a/Bio/DB/Registry.pm +++ b/Bio/DB/Registry.pm @@ -130,7 +130,7 @@ sub _load_registry { my ($db,$hash) = (); for my $file (@ini_files) { - open my $FH,"$file"; + open my $FH, '<', $file or $self->throw("Could not read file '$file': $!"); while( <$FH> ) { if (/^VERSION=([\d\.]+)/) { if ($1 > $OBDA_SPEC_VERSION or !$1) { @@ -284,7 +284,8 @@ sub _make_private_registry { $self->throw("Could not make directory $HOME/$PRIVATE_DIR, " . "no $REGISTRY file available") if $@; - open(my $F,">$HOME/$PRIVATE_DIR/$REGISTRY"); + open my $F, '>', "$HOME/$PRIVATE_DIR/$REGISTRY" + or $self->throw("Could not write file '$HOME/$PRIVATE_DIR/$REGISTRY': $!"); print $F while (<$F>); close $F; diff --git a/Bio/DB/SeqFeature/Store/DBI/Pg.pm b/Bio/DB/SeqFeature/Store/DBI/Pg.pm index a3da80fc1..18f3042e9 100644 --- a/Bio/DB/SeqFeature/Store/DBI/Pg.pm +++ b/Bio/DB/SeqFeature/Store/DBI/Pg.pm @@ -454,14 +454,14 @@ sub _finish_bulk_update { my $qualified_table = $self->_qualify($table); system "cp $path $path.bak"; # Get stuff from file into STDIN so we don't have to be superuser - open FH, $path; + open my $FH, '<', $path or $self->throw("Could not read file '$path': $!"); print STDERR "Loading file $path\n"; $dbh->do("COPY $qualified_table FROM STDIN CSV QUOTE '''' DELIMITER '\t'") or $self->throw($dbh->errstr); - while (my $line = ) { + while (my $line = <$FH>) { $dbh->pg_putline($line); } $dbh->pg_endcopy() or $self->throw($dbh->errstr); - close FH; + close $FH; #unlink $path; } delete $self->{bulk_update_in_progress}; diff --git a/Bio/DB/SeqFeature/Store/DBI/SQLite.pm b/Bio/DB/SeqFeature/Store/DBI/SQLite.pm index 213d623d4..5da7e3b1f 100644 --- a/Bio/DB/SeqFeature/Store/DBI/SQLite.pm +++ b/Bio/DB/SeqFeature/Store/DBI/SQLite.pm @@ -427,7 +427,8 @@ sub _finish_bulk_update { my $fh = $self->dump_filehandle($table); my $path = $self->dump_path($table); $fh->close; - open($fh, $path); + + open $fh, '<', $path or $self->throw("Could not read file '$path': $!"); my $qualified_table = $self->_qualify($table); my $sth; diff --git a/Bio/DB/SeqFeature/Store/berkeleydb.pm b/Bio/DB/SeqFeature/Store/berkeleydb.pm index 53f34b8c5..1d8a9e341 100644 --- a/Bio/DB/SeqFeature/Store/berkeleydb.pm +++ b/Bio/DB/SeqFeature/Store/berkeleydb.pm @@ -369,7 +369,7 @@ sub auto_index_in_process { return unless -e $flag_file; # if flagfile exists, then check that PID still exists - open my $fh,$flag_file or die "Couldn't open $flag_file: $!"; + open my $fh, '<', $flag_file or $self->throw("Could not read file '$flag_file': $!"); my $pid = <$fh>; close $fh; return 1 if kill 0=>$pid; @@ -393,7 +393,7 @@ sub flag_autoindexing { my $doit = shift; my $flag_file = $self->autoindex_flagfile; if ($doit) { - open my $fh,'>',$flag_file or die "Couldn't open $flag_file: $!"; + open my $fh, '>', $flag_file or $self->throw("Could not write file '$flag_file': $!"); print $fh $$; close $fh; } else { @@ -673,7 +673,8 @@ sub open_notes_db { : $create ? "+>" : "<"; - open (my $F,$mode,$self->_notes_path) or $self->throw($self->_notes_path.": $!"); + my $notes_file = $self->_notes_file; + open my $F, $mode, $notes_file or $self->throw("Could not open file '$notes_file': $!"); $self->notes_db($F); } @@ -724,9 +725,9 @@ sub _delete_databases { sub _touch_timestamp { my $self = shift; my $tsf = $self->_mtime_path; - open (F,">$tsf") or $self->throw("Couldn't open $tsf: $!"); - print F scalar(localtime); - close F; + open my $F, '>', $tsf or $self->throw("Could not write file '$tsf': $!"); + print $F scalar(localtime); + close $F; } sub _store { diff --git a/Bio/DB/SeqHound.pm b/Bio/DB/SeqHound.pm index 92046c871..d2e831188 100644 --- a/Bio/DB/SeqHound.pm +++ b/Bio/DB/SeqHound.pm @@ -519,31 +519,35 @@ sub postprocess_data $self->throw("Please provide the function name"); } - #set up verbosity level if need record in the log file + #set up verbosity level if need record in the log file my $log_msg = "Writing into '$LOGFILENAME' log file.\n"; my $now = strftime("%a %b %e %H:%M:%S %Y", localtime); if ($lcontent eq "") { $self->debug($log_msg); - open (my $LOG, '>>', $LOGFILENAME); + open my $LOG, '>>', $LOGFILENAME or $self->throw("Could not append file '$LOGFILENAME': $!"); print $LOG "$now $funcname. No reply.\n"; + close $LOG; return; } elsif ($lcontent =~ /HTTP::Request error/) { $self->debug($log_msg); - open (my $LOG, '>>', $LOGFILENAME); + open my $LOG, '>>', $LOGFILENAME or $self->throw("Could not append file '$LOGFILENAME': $!"); print $LOG "$now $funcname. Http::Request error problem.\n"; + close $LOG; return; } elsif ($lcontent =~ /SEQHOUND_ERROR/) { $self->debug($log_msg); - open (my $LOG, '>>', $LOGFILENAME); + open my $LOG, '>>', $LOGFILENAME or $self->throw("Could not append file '$LOGFILENAME': $!"); print $LOG "$now $funcname error. SEQHOUND_ERROR found.\n"; + close $LOG; return; } elsif ($lcontent =~ /SEQHOUND_NULL/) { $self->debug($log_msg); - open (my $LOG, '>>', $LOGFILENAME); + open my $LOG, '>>', $LOGFILENAME or $self->throw("Could not append file '$LOGFILENAME': $!"); print $LOG "$now $funcname Value not found in the database. SEQHOUND_NULL found.\n"; + close $LOG; return; } else { @@ -551,14 +555,16 @@ sub postprocess_data my @lines = split(/\n/, $lcontent, 2); if ($lines[1] =~ /^-1/) { $self->debug($log_msg); - open (my $LOG, '>>', $LOGFILENAME); + open my $LOG, '>>', $LOGFILENAME or $self->throw("Could not append file '$LOGFILENAME': $!"); print $LOG "$now $funcname Value not found in the database. -1 found.\n"; + close $LOG; return; } elsif ($lines[1] =~ /^0/) { $self->debug($log_msg); - open (my $LOG, '>>', $LOGFILENAME); + open my $LOG, '>>', $LOGFILENAME or $self->throw("Could not append file '$LOGFILENAME': $!"); print $LOG "$now $funcname failed.\n"; + close $LOG; return; } else { diff --git a/Bio/DB/TFBS/transfac_pro.pm b/Bio/DB/TFBS/transfac_pro.pm index fbbce58b4..2b7ded80e 100644 --- a/Bio/DB/TFBS/transfac_pro.pm +++ b/Bio/DB/TFBS/transfac_pro.pm @@ -785,56 +785,63 @@ sub _build_index { # don't strictly need it my $index_dir = $self->index_directory; - my $gene_index = "$index_dir/gene.dat.index"; + my $gene_index = "$index_dir/gene.dat.index"; my $reference_index = "$index_dir/reference.dat.index"; - my $matrix_index = "$index_dir/matrix.dat.index"; - my $factor_index = "$index_dir/factor.dat.index"; - my $fragment_index = "$index_dir/fragment.dat.index"; - my $site_index = "$index_dir/site.dat.index"; + my $matrix_index = "$index_dir/matrix.dat.index"; + my $factor_index = "$index_dir/factor.dat.index"; + my $fragment_index = "$index_dir/fragment.dat.index"; + my $site_index = "$index_dir/site.dat.index"; my $reference_dat = "$dat_dir/reference.dat"; if (! -e $reference_index || $force) { - open(REF, $reference_dat) || $self->throw("Cannot open reference file '$reference_dat' for reading"); + open my $REF, '<', $reference_dat or $self->throw("Could not read reference file '$reference_dat': $!"); my %references; unlink $reference_index; - my $ref = tie(%references, 'DB_File', $reference_index, O_RDWR|O_CREAT, 0644, $DB_HASH) || $self->throw("Cannot open file '$reference_index': $!"); + my $ref = tie(%references, 'DB_File', $reference_index, O_RDWR|O_CREAT, 0644, $DB_HASH) + or $self->throw("CCould not open file '$reference_index': $!"); my %pubmed; my $reference_pubmed = $reference_index.'.pubmed'; unlink $reference_pubmed; - my $pub = tie(%pubmed, 'DB_File', $reference_pubmed, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$reference_pubmed': $!"); + my $pub = tie(%pubmed, 'DB_File', $reference_pubmed, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$reference_pubmed': $!"); my %gene; my $reference_gene = $gene_index.'.reference'; unlink $reference_gene; - my $gene = tie(%gene, 'DB_File', $reference_gene, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$reference_gene': $!"); + my $gene = tie(%gene, 'DB_File', $reference_gene, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$reference_gene': $!"); my %site; my $reference_site = $site_index.'.reference'; unlink $reference_site; - my $site = tie(%site, 'DB_File', $reference_site, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$reference_site': $!"); + my $site = tie(%site, 'DB_File', $reference_site, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$reference_site': $!"); my %fragment; my $reference_fragment = $fragment_index.'.reference'; unlink $reference_fragment; - my $fragment = tie(%fragment, 'DB_File', $reference_fragment, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$reference_fragment': $!"); + my $fragment = tie(%fragment, 'DB_File', $reference_fragment, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$reference_fragment': $!"); my %factor; my $reference_factor = $factor_index.'.reference'; unlink $reference_factor; - my $factor = tie(%factor, 'DB_File', $reference_factor, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$reference_factor': $!"); + my $factor = tie(%factor, 'DB_File', $reference_factor, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$reference_factor': $!"); my %matrix; my $reference_matrix = $matrix_index.'.reference'; unlink $reference_matrix; - my $matrix = tie(%matrix, 'DB_File', $reference_matrix, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$reference_matrix': $!"); + my $matrix = tie(%matrix, 'DB_File', $reference_matrix, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$reference_matrix': $!"); # skip the first three header lines - ; ; ; + <$REF>; <$REF>; <$REF>; my @data; - while () { + while (<$REF>) { if (/^AC (\S+)/) { $data[0] = $1; } @@ -870,12 +877,15 @@ sub _build_index { # end of a record, store previous data and reset # accession = pubmed authors title location - $references{$data[0]} = join(SEPARATOR, ($data[1] || '', $data[2] || '', $data[3] || '', $data[4] || '')); + $references{$data[0]} = join(SEPARATOR, ($data[1] || '', + $data[2] || '', + $data[3] || '', + $data[4] || '')); @data = (); } } - close(REF); + close $REF; $ref = $pub = $gene = $site = $fragment = $factor = $matrix = undef; untie %references; @@ -889,52 +899,60 @@ sub _build_index { my $gene_dat = "$dat_dir/gene.dat"; if (! -e $gene_index || $force) { - open(GEN, $gene_dat) || $self->throw("Cannot open gene file '$gene_dat' for reading"); + open my $GEN, '<', $gene_dat or $self->throw("Could not read gene file '$gene_dat': $!"); my %genes; unlink $gene_index; - my $gene = tie(%genes, 'DB_File', $gene_index, O_RDWR|O_CREAT, 0644, $DB_HASH) || $self->throw("Cannot open file '$gene_index': $!"); + my $gene = tie(%genes, 'DB_File', $gene_index, O_RDWR|O_CREAT, 0644, $DB_HASH) + or $self->throw("Could not open file '$gene_index': $!"); my %id; my $gene_id = $gene_index.'.id'; unlink $gene_id; - my $id = tie(%id, 'DB_File', $gene_id, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$gene_id': $!"); + my $id = tie(%id, 'DB_File', $gene_id, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$gene_id': $!"); my %name; my $gene_name = $gene_index.'.name'; unlink $gene_name; - my $name = tie(%name, 'DB_File', $gene_name, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$gene_name': $!"); + my $name = tie(%name, 'DB_File', $gene_name, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$gene_name': $!"); my %species; my $gene_species = $gene_index.'.species'; unlink $gene_species; - my $species = tie(%species, 'DB_File', $gene_species, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$gene_species': $!"); + my $species = tie(%species, 'DB_File', $gene_species, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$gene_species': $!"); my %site; my $gene_site = $site_index.'.gene'; unlink $gene_site; - my $site = tie(%site, 'DB_File', $gene_site, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$gene_site': $!"); + my $site = tie(%site, 'DB_File', $gene_site, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$gene_site': $!"); my %factor; my $gene_factor = $factor_index.'.gene'; unlink $gene_factor; - my $factor = tie(%factor, 'DB_File', $gene_factor, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$gene_factor': $!"); + my $factor = tie(%factor, 'DB_File', $gene_factor, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$gene_factor': $!"); my %fragment; my $gene_fragment = $fragment_index.'.gene'; unlink $gene_fragment; - my $fragment = tie(%fragment, 'DB_File', $gene_fragment, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$gene_fragment': $!"); + my $fragment = tie(%fragment, 'DB_File', $gene_fragment, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$gene_fragment': $!"); my %reference; my $gene_reference = $reference_index.'.gene'; unlink $gene_reference; - my $reference = tie(%reference, 'DB_File', $gene_reference, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$gene_reference': $!"); + my $reference = tie(%reference, 'DB_File', $gene_reference, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$gene_reference': $!"); # skip the first three header lines - ; ; ; + <$GEN>; <$GEN>; <$GEN>; my @data; - while () { + while (<$GEN>) { if (/^AC (\S+)/) { $data[0] = $1; } @@ -976,12 +994,15 @@ sub _build_index { # end of a record, store previous data and reset # accession = id name description species_tax_id_or_raw_string - $genes{$data[0]} = join(SEPARATOR, ($data[1] || '', $data[2] || '', $data[3] || '', $data[4] || '')); + $genes{$data[0]} = join(SEPARATOR, ($data[1] || '', + $data[2] || '', + $data[3] || '', + $data[4] || '')); @data = (); } } - close(GEN); + close $GEN; $gene = $id = $name = $species = $site = $factor = $reference = undef; untie %genes; @@ -995,52 +1016,60 @@ sub _build_index { my $site_dat = "$dat_dir/site.dat"; if (! -e $site_index || $force) { - open(SIT, $site_dat) || $self->throw("Cannot open site file '$site_dat' for reading"); + open my $SIT, '<', $site_dat or $self->throw("Could not read site file '$site_dat': $!"); my %sites; unlink $site_index; - my $site = tie(%sites, 'DB_File', $site_index, O_RDWR|O_CREAT, 0644, $DB_HASH) || $self->throw("Cannot open file '$site_index': $!"); + my $site = tie(%sites, 'DB_File', $site_index, O_RDWR|O_CREAT, 0644, $DB_HASH) + or $self->throw("Could not open file '$site_index': $!"); my %id; my $site_id = $site_index.'.id'; unlink $site_id; - my $id = tie(%id, 'DB_File', $site_id, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$site_id': $!"); + my $id = tie(%id, 'DB_File', $site_id, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$site_id': $!"); my %species; my $site_species = $site_index.'.species'; unlink $site_species; - my $species = tie(%species, 'DB_File', $site_species, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$site_species': $!"); + my $species = tie(%species, 'DB_File', $site_species, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$site_species': $!"); my %qualities; my $site_qualities = $site_index.'.qual'; unlink $site_qualities; - my $quality = tie(%qualities, 'DB_File', $site_qualities, O_RDWR|O_CREAT, 0644, $DB_HASH) || $self->throw("Cannot open file '$site_qualities': $!"); + my $quality = tie(%qualities, 'DB_File', $site_qualities, O_RDWR|O_CREAT, 0644, $DB_HASH) + or $self->throw("Could not open file '$site_qualities': $!"); my %gene; my $site_gene = $gene_index.'.site'; unlink $site_gene; - my $gene = tie(%gene, 'DB_File', $site_gene, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$site_gene': $!"); + my $gene = tie(%gene, 'DB_File', $site_gene, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$site_gene': $!"); my %matrix; my $site_matrix = $matrix_index.'.site'; unlink $site_matrix; - my $matrix = tie(%matrix, 'DB_File', $site_matrix, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$site_matrix': $!"); + my $matrix = tie(%matrix, 'DB_File', $site_matrix, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$site_matrix': $!"); my %factor; my $site_factor = $factor_index.'.site'; unlink $site_factor; - my $factor = tie(%factor, 'DB_File', $site_factor, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$site_factor': $!"); + my $factor = tie(%factor, 'DB_File', $site_factor, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$site_factor': $!"); my %reference; my $site_reference = $reference_index.'.site'; unlink $site_reference; - my $reference = tie(%reference, 'DB_File', $site_reference, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$site_reference': $!"); + my $reference = tie(%reference, 'DB_File', $site_reference, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$site_reference': $!"); # skip the first three header lines - ; ; ; + <$SIT>; <$SIT>; <$SIT>; my @data; - while () { + while (<$SIT>) { if (/^AC (\S+)/) { $data[0] = $1; } @@ -1101,12 +1130,19 @@ sub _build_index { # end of a record, store previous data and reset # accession = id gene_id sequence relative_to first_position last_position species_tax_id_or_raw_string type - $sites{$data[0]} = join(SEPARATOR, ($data[1] || '', $data[2] || '', $data[3] || '', $data[4] || 'TSS', $data[5] || '', $data[6] || '', $data[7] || '', $data[8] || '')); + $sites{$data[0]} = join(SEPARATOR, ($data[1] || '', + $data[2] || '', + $data[3] || '', + $data[4] || 'TSS', + $data[5] || '', + $data[6] || '', + $data[7] || '', + $data[8] || '')); @data = (); } } - close(SIT); + close $SIT; $site = $id = $species = $quality = $gene = $matrix = $factor = $reference = undef; untie %sites; @@ -1121,44 +1157,50 @@ sub _build_index { my $matrix_dat = "$dat_dir/matrix.dat"; if (! -e $matrix_index || $force) { - open(MAT, $matrix_dat) || $self->throw("Cannot open matrix file '$matrix_dat' for reading"); + open my $MAT, '<', $matrix_dat or $self->throw("Could not read matrix file '$matrix_dat': $!"); my %matrices; unlink $matrix_index; - my $matrix = tie(%matrices, 'DB_File', $matrix_index, O_RDWR|O_CREAT, 0644, $DB_HASH) || $self->throw("Cannot open file '$matrix_index': $!"); + my $matrix = tie(%matrices, 'DB_File', $matrix_index, O_RDWR|O_CREAT, 0644, $DB_HASH) + or $self->throw("Could not open file '$matrix_index': $!"); my %id; my $matrix_id = $matrix_index.'.id'; unlink $matrix_id; - my $id = tie(%id, 'DB_File', $matrix_id, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$matrix_id': $!"); + my $id = tie(%id, 'DB_File', $matrix_id, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$matrix_id': $!"); my %name; my $matrix_name = $matrix_index.'.name'; unlink $matrix_name; - my $name = tie(%name, 'DB_File', $matrix_name, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$matrix_name': $!"); + my $name = tie(%name, 'DB_File', $matrix_name, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$matrix_name': $!"); my %site; my $matrix_site = $site_index.'.matrix'; unlink $matrix_site; - my $site = tie(%site, 'DB_File', $matrix_site, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$matrix_site': $!"); + my $site = tie(%site, 'DB_File', $matrix_site, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$matrix_site': $!"); my %factor; my $matrix_factor = $factor_index.'.matrix'; unlink $matrix_factor; - my $factor = tie(%factor, 'DB_File', $matrix_factor, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$matrix_factor': $!"); + my $factor = tie(%factor, 'DB_File', $matrix_factor, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$matrix_factor': $!"); my %reference; my $matrix_reference = $reference_index.'.matrix'; unlink $matrix_reference; - my $reference = tie(%reference, 'DB_File', $matrix_reference, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$matrix_reference': $!"); + my $reference = tie(%reference, 'DB_File', $matrix_reference, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$matrix_reference': $!"); # skip the first three header lines - ; ; ; + <$MAT>; <$MAT>; <$MAT>; my @data; my @matrix_data; my @site_data; - while () { + while (<$MAT>) { if (/^AC (\S+)/) { $data[0] = $1; } @@ -1228,12 +1270,17 @@ sub _build_index { my $site_data = join(INTERNAL_SEPARATOR, @site_data) || ''; # accession = id name description num_of_sites matrix_data site_data - $matrices{$data[0]} = join(SEPARATOR, ($data[1] || '', $data[2] || '', $data[3] || '', $data[4], $matrix_data, $site_data)); + $matrices{$data[0]} = join(SEPARATOR, ($data[1] || '', + $data[2] || '', + $data[3] || '', + $data[4], + $matrix_data, + $site_data)); @data = @matrix_data = @site_data = (); } } - close(MAT); + close $MAT; $matrix = $id = $name = $site = $factor = $reference = undef; untie %matrices; @@ -1246,63 +1293,73 @@ sub _build_index { my $factor_dat = "$dat_dir/factor.dat"; if (! -e $factor_index || $force) { - open(FAC, $factor_dat) || $self->throw("Cannot open factor file '$factor_dat' for reading"); + open my $FAC, '<', $factor_dat or $self->throw("Could not read factor file '$factor_dat': $!"); my %factors; unlink $factor_index; - my $factor = tie(%factors, 'DB_File', $factor_index, O_RDWR|O_CREAT, 0644, $DB_HASH) || $self->throw("Cannot open file '$factor_index': $!"); + my $factor = tie(%factors, 'DB_File', $factor_index, O_RDWR|O_CREAT, 0644, $DB_HASH) + or $self->throw("Could not open file '$factor_index': $!"); my %id; my $factor_id = $factor_index.'.id'; unlink $factor_id; - my $id = tie(%id, 'DB_File', $factor_id, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file 'factor_id': $!"); + my $id = tie(%id, 'DB_File', $factor_id, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$factor_id': $!"); my %name; my $factor_name = $factor_index.'.name'; unlink $factor_name; - my $name = tie(%name, 'DB_File', $factor_name, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$factor_name': $!"); + my $name = tie(%name, 'DB_File', $factor_name, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$factor_name': $!"); my %species; my $factor_species = $factor_index.'.species'; unlink $factor_species; - my $species = tie(%species, 'DB_File', $factor_species, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$factor_species': $!"); + my $species = tie(%species, 'DB_File', $factor_species, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$factor_species': $!"); my %interactors; my $factor_interactors = $factor_index.'.interactors'; unlink $factor_interactors; - my $interact = tie(%interactors, 'DB_File', $factor_interactors, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$factor_interactors': $!"); + my $interact = tie(%interactors, 'DB_File', $factor_interactors, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$factor_interactors': $!"); my %gene; my $factor_gene = $gene_index.'.factor'; unlink $factor_gene; - my $gene = tie(%gene, 'DB_File', $factor_gene, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$factor_gene': $!"); + my $gene = tie(%gene, 'DB_File', $factor_gene, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$factor_gene': $!"); my %matrix; my $factor_matrix = $matrix_index.'.factor'; unlink $factor_matrix; - my $matrix = tie(%matrix, 'DB_File', $factor_matrix, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$factor_matrix': $!"); + my $matrix = tie(%matrix, 'DB_File', $factor_matrix, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$factor_matrix': $!"); my %site; my $factor_site = $site_index.'.factor'; unlink $factor_site; - my $site = tie(%site, 'DB_File', $factor_site, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$factor_site': $!"); + my $site = tie(%site, 'DB_File', $factor_site, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$factor_site': $!"); my %fragment; my $factor_fragment = $fragment_index.'.factor'; unlink $factor_fragment; - my $fragment = tie(%fragment, 'DB_File', $factor_fragment, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$factor_fragment': $!"); + my $fragment = tie(%fragment, 'DB_File', $factor_fragment, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$factor_fragment': $!"); my %reference; my $factor_reference = $reference_index.'.factor'; unlink $factor_reference; - my $reference = tie(%reference, 'DB_File', $factor_reference, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$factor_reference': $!"); + my $reference = tie(%reference, 'DB_File', $factor_reference, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$factor_reference': $!"); # skip the first three header lines - ; ; ; + <$FAC>; <$FAC>; <$FAC>; my @data; my $sequence = ''; - while () { + while (<$FAC>) { if (/^AC (\S+)/) { $data[0] = $1; } @@ -1350,13 +1407,16 @@ sub _build_index { # end of a record, store previous data and reset # accession = id name species sequence - $factors{$data[0]} = join(SEPARATOR, ($data[1] || '', $data[2] || '', $data[3] || '', $sequence)); + $factors{$data[0]} = join(SEPARATOR, ($data[1] || '', + $data[2] || '', + $data[3] || '', + $sequence)); @data = (); $sequence = ''; } } - close(FAC); + close $FAC; $factor = $id = $name = $species = $interact = $gene = $matrix = $site = $fragment = $reference = undef; untie %factors; @@ -1373,46 +1433,53 @@ sub _build_index { my $fragment_dat = "$dat_dir/fragment.dat"; if (! -e $fragment_index || $force) { - if (open(FRA, $fragment_dat)) { + if (open my $FRA, '<', $fragment_dat) { my %fragments; unlink $fragment_index; - my $fragment = tie(%fragments, 'DB_File', $fragment_index, O_RDWR|O_CREAT, 0644, $DB_HASH) || $self->throw("Cannot open file '$fragment_index': $!"); + my $fragment = tie(%fragments, 'DB_File', $fragment_index, O_RDWR|O_CREAT, 0644, $DB_HASH) + or $self->throw("Could not open file '$fragment_index': $!"); my %id; my $fragment_id = $fragment_index.'.id'; unlink $fragment_id; - my $id = tie(%id, 'DB_File', $fragment_id, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$fragment_id': $!"); + my $id = tie(%id, 'DB_File', $fragment_id, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$fragment_id': $!"); my %qualities; my $fragment_qualities = $fragment_index.'.qual'; unlink $fragment_qualities; - my $quality = tie(%qualities, 'DB_File', $fragment_qualities, O_RDWR|O_CREAT, 0644, $DB_HASH) || $self->throw("Cannot open file '$fragment_qualities': $!"); + my $quality = tie(%qualities, 'DB_File', $fragment_qualities, O_RDWR|O_CREAT, 0644, $DB_HASH) + or $self->throw("Could not open file '$fragment_qualities': $!"); my %species; my $fragment_species = $fragment_index.'.species'; unlink $fragment_species; - my $species = tie(%species, 'DB_File', $fragment_species, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$fragment_species': $!"); + my $species = tie(%species, 'DB_File', $fragment_species, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$fragment_species': $!"); my %gene; my $fragment_gene = $gene_index.'.fragment'; unlink $fragment_gene; - my $gene = tie(%gene, 'DB_File', $fragment_gene, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$fragment_gene': $!"); + my $gene = tie(%gene, 'DB_File', $fragment_gene, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$fragment_gene': $!"); my %factor; my $fragment_factor = $factor_index.'.fragment'; unlink $fragment_factor; - my $factor = tie(%factor, 'DB_File', $fragment_factor, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$fragment_factor': $!"); + my $factor = tie(%factor, 'DB_File', $fragment_factor, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$fragment_factor': $!"); my %reference; my $fragment_reference = $reference_index.'.fragment'; unlink $fragment_reference; - my $reference = tie(%reference, 'DB_File', $fragment_reference, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot open file '$fragment_reference': $!"); + my $reference = tie(%reference, 'DB_File', $fragment_reference, O_RDWR|O_CREAT, 0644, $DB_BTREE) + or $self->throw("Could not open file '$fragment_reference': $!"); # skip the first three header lines - ; ; ; + <$FRA>; <$FRA>; <$FRA>; my @data; - while () { + while (<$FRA>) { if (/^AC (\S+)/) { $data[0] = $1; } @@ -1457,12 +1524,17 @@ sub _build_index { # end of a record, store previous data and reset # accession = id gene_id1 gene_id2 species_tax_id_or_raw_string sequence source - $fragments{$data[0]} = join(SEPARATOR, ($data[1] || '', $data[2] || '', $data[3] || '', $data[4] || '', $data[5] || '', $data[6] || '')); + $fragments{$data[0]} = join(SEPARATOR, ($data[1] || '', + $data[2] || '', + $data[3] || '', + $data[4] || '', + $data[5] || '', + $data[6] || '')); @data = (); } } - close(FRA); + close $FRA; $fragment = $id = $species = $quality = $gene = $factor = $reference = undef; untie %fragments; @@ -1474,7 +1546,7 @@ sub _build_index { untie %reference; } else { - $self->warn("Cannot open fragment file '$fragment_dat' for reading, assuming you have an old version of Transfac Pro with no fragment.dat file."); + $self->warn("Could not read fragment file '$fragment_dat', assuming you have an old version of Transfac Pro with no fragment.dat file"); } } } diff --git a/Bio/DB/Taxonomy/flatfile.pm b/Bio/DB/Taxonomy/flatfile.pm index d2669bda1..6ba979c51 100644 --- a/Bio/DB/Taxonomy/flatfile.pm +++ b/Bio/DB/Taxonomy/flatfile.pm @@ -362,8 +362,8 @@ sub _build_index { if (! -e $nodeindex || $force) { my (%parent2children,@nodes); - open(NODES,$nodesfile) || - $self->throw("Cannot open node file '$nodesfile' for reading"); + open my $NODES, '<', $nodesfile + or $self->throw("Could not read node file '$nodesfile': $!"); unlink $nodeindex; unlink $parent2childindex; @@ -372,7 +372,7 @@ sub _build_index { my $btree = tie( %parent2children, 'DB_File', $parent2childindex, O_RDWR|O_CREAT, 0644, $DB_BTREE) || $self->throw("Cannot tie to file '$parent2childindex': $!"); - while () { + while (<$NODES>) { next if /^$/; chomp; my ($taxid,$parent,$rank,$code,$divid,undef,$gen_code,undef,$mito) = split(/\t\|\t/,$_); @@ -386,7 +386,7 @@ sub _build_index { $nodes[$taxid] = join(SEPARATOR, ($taxid,$parent,$rank,$code,$divid,$gen_code,$mito)); $btree->put($parent,$taxid); } - close(NODES); + close $NODES; $nh = $btree = undef; untie @nodes ; @@ -394,8 +394,8 @@ sub _build_index { } if ((! -e $name2idindex || -z $name2idindex) || (! -e $id2nameindex || -z $id2nameindex) || $force) { - open(NAMES,$namesfile) || - $self->throw("Cannot open names file '$namesfile' for reading"); + open my $NAMES, '<', $namesfile + or $self->throw("Could not read names file '$namesfile': $!"); unlink $name2idindex; unlink $id2nameindex; @@ -405,7 +405,7 @@ sub _build_index { my $nameh = tie ( %name2id, 'DB_File', $name2idindex, O_RDWR|O_CREAT, 0644, $DB_HASH) || $self->throw("Cannot tie to file '$name2idindex': $!"); - while () { + while (<$NAMES>) { next if /^$/; chomp; my ($taxid, $name, $unique_name, $class) = split(/\t\|\t/,$_); @@ -455,7 +455,7 @@ sub _build_index { } $id2name[$taxid] = join(SEPARATOR, @names); } - close(NAMES); + close $NAMES; $idh = $nameh = undef; untie( %name2id); diff --git a/Bio/DB/Taxonomy/greengenes.pm b/Bio/DB/Taxonomy/greengenes.pm index 8b52bc253..95bb6ad23 100644 --- a/Bio/DB/Taxonomy/greengenes.pm +++ b/Bio/DB/Taxonomy/greengenes.pm @@ -123,7 +123,7 @@ sub _build_taxonomy { my $taxonomy = Bio::DB::Taxonomy::list->new(); - open my $fh, '<', $taxofile or $self->throw("Could not read file $taxofile: $!"); + open my $fh, '<', $taxofile or $self->throw("Could not read file '$taxofile': $!"); # Will skip header line: prokMSA_id taxonomy my $prev_taxo_string = 'taxonomy'; diff --git a/Bio/DB/Taxonomy/silva.pm b/Bio/DB/Taxonomy/silva.pm index 7fa37d150..af785be34 100644 --- a/Bio/DB/Taxonomy/silva.pm +++ b/Bio/DB/Taxonomy/silva.pm @@ -124,7 +124,7 @@ sub _build_taxonomy { # One could open the file using Bio::SeqIO::fasta, but it is slower and we # only need the sequence descriptions - open my $in, '<', $taxofile or $self->throw("Could not read file '$taxofile': $!\n"); + open my $in, '<', $taxofile or $self->throw("Could not read file '$taxofile': $!"); # Populate taxonomy with taxonomy obtained from sequence description while (my $line = <$in>) { diff --git a/Bio/DB/WebDBSeqI.pm b/Bio/DB/WebDBSeqI.pm index e2bc1d34f..21fbe5865 100644 --- a/Bio/DB/WebDBSeqI.pm +++ b/Bio/DB/WebDBSeqI.pm @@ -496,8 +496,9 @@ sub get_seq_stream { # this may get reset when requesting batch mode ($rformat,$ioformat) = $self->request_format(); if( $self->verbose > 0 ) { - open(my $ERR, "<", $tmpfile); + open my $ERR, '<', $tmpfile or $self->throw("Could not read file '$tmpfile': $!"); while(<$ERR>) { $self->debug($_);} + close $ERR; } return Bio::SeqIO->new('-verbose' => $self->verbose, diff --git a/Bio/Index/Abstract.pm b/Bio/Index/Abstract.pm index ab1e5dbe2..b6e45213e 100644 --- a/Bio/Index/Abstract.pm +++ b/Bio/Index/Abstract.pm @@ -701,9 +701,7 @@ sub _file_handle { my @rec = $self->unpack_record($self->db->{"__FILE_$i"}) or $self->throw("Can't get filename for index : $i"); my $file = $rec[0]; -# my $fh = Symbol::gensym(); -# open $fh, '<', $file or $self->throw("Can't read file '$file' : $!"); - open my $fh, '<', $file or $self->throw("Can't read file '$file' : $!"); + open my $fh, '<', $file or $self->throw("Could not read file '$file': $!"); $self->{'_filehandle'}[$i] = $fh; # Cache filehandle } return $self->{'_filehandle'}[$i]; diff --git a/Bio/Index/Blast.pm b/Bio/Index/Blast.pm index 50298d0ee..b2fe3702e 100644 --- a/Bio/Index/Blast.pm +++ b/Bio/Index/Blast.pm @@ -211,7 +211,7 @@ sub _index_file { # of the last found record. ); - open(my $BLAST, '<', $file) or $self->throw("cannot open file $file\n"); + open my $BLAST, '<', $file or $self->throw("Could not read file '$file': $!"); my (@data, @records); my $indexpoint = 0; diff --git a/Bio/Index/BlastTable.pm b/Bio/Index/BlastTable.pm index 42bc62b77..d1cde0a38 100644 --- a/Bio/Index/BlastTable.pm +++ b/Bio/Index/BlastTable.pm @@ -189,7 +189,7 @@ sub _index_file { $i, # Index-number of file being indexed ) = @_; - open(my $BLAST, '<', $file) or $self->throw("cannot open file $file\n"); + open my $BLAST, '<', $file or $self->throw("Could not read file '$file': $!"); my $indexpoint = 0; my $lastline = 0; my $last_query = ''; diff --git a/Bio/Index/EMBL.pm b/Bio/Index/EMBL.pm index a73e2b124..99daab8eb 100644 --- a/Bio/Index/EMBL.pm +++ b/Bio/Index/EMBL.pm @@ -144,7 +144,7 @@ sub _index_file { $begin = 0; - open my $EMBL, '<', $file or $self->throw("Can't open file for read : $file - $!"); + open my $EMBL, '<', $file or $self->throw("Could not read file '$file': $!"); # In Windows, text files have '\r\n' as line separator, but when reading in # text mode Perl will only show the '\n'. This means that for a line "ABC\r\n", diff --git a/Bio/Index/Fasta.pm b/Bio/Index/Fasta.pm index 89525dabc..423eaaf41 100644 --- a/Bio/Index/Fasta.pm +++ b/Bio/Index/Fasta.pm @@ -178,7 +178,7 @@ sub _index_file { my $id_parser = $self->id_parser; - open my $FASTA, '<', $file or $self->throw("Can't open file for read : $file - $!"); + open my $FASTA, '<', $file or $self->throw("Could not read file '$file': $!"); # In Windows, text files have '\r\n' as line separator, but when reading in # text mode Perl will only show the '\n'. This means that for a line "ABC\r\n", diff --git a/Bio/Index/Fastq.pm b/Bio/Index/Fastq.pm index 031880437..74e3faaf8 100644 --- a/Bio/Index/Fastq.pm +++ b/Bio/Index/Fastq.pm @@ -156,7 +156,7 @@ sub _index_file { my $id_parser = $self->id_parser; my $c = 0; - open my $FASTQ, '<', $file or $self->throw("Can't open file for read : $file - $!"); + open my $FASTQ, '<', $file or $self->throw("Could not read file '$file': $!"); # In Windows, text files have '\r\n' as line separator, but when reading in # text mode Perl will only show the '\n'. This means that for a line "ABC\r\n", diff --git a/Bio/Index/GenBank.pm b/Bio/Index/GenBank.pm index 143cbc31c..c42dfbe33 100644 --- a/Bio/Index/GenBank.pm +++ b/Bio/Index/GenBank.pm @@ -149,8 +149,7 @@ sub _index_file { my $id_parser = $self->id_parser; - open my $GENBANK, '<', $file or - $self->throw("Can't open file for read : $file - $!"); + open my $GENBANK, '<', $file or $self->throw("Could not read file '$file': $!"); my %done_ids; diff --git a/Bio/Index/Hmmer.pm b/Bio/Index/Hmmer.pm index 12ab1de10..281add441 100644 --- a/Bio/Index/Hmmer.pm +++ b/Bio/Index/Hmmer.pm @@ -276,7 +276,7 @@ sub _index_file { my($self, $file, $i) = @_; my($begin); - open(my $HMMER, '<', $file) or $self->throw("cannot open file $file"); + open my $HMMER, '<', $file or $self->throw("Could not read file '$file': $!"); my $id; my $indexpoint = 0; diff --git a/Bio/Index/Qual.pm b/Bio/Index/Qual.pm index 99489f37c..e40b1dc18 100755 --- a/Bio/Index/Qual.pm +++ b/Bio/Index/Qual.pm @@ -178,7 +178,7 @@ sub _index_file { my $id_parser = $self->id_parser; - open my $QUAL, '<', $file or $self->throw("Can't open file for read : $file - $!"); + open my $QUAL, '<', $file or $self->throw("Could not read file '$file': $!"); # In Windows, text files have '\r\n' as line separator, but when reading in # text mode Perl will only show the '\n'. This means that for a line "ABC\r\n", diff --git a/Bio/Index/Stockholm.pm b/Bio/Index/Stockholm.pm index f508e4207..ec830cf1d 100644 --- a/Bio/Index/Stockholm.pm +++ b/Bio/Index/Stockholm.pm @@ -210,8 +210,7 @@ sub _index_file { my $id_parser = $self->id_parser; - open my $STOCKHOLM, '<', $file or - $self->throw("Can't open file for read : $file"); + open my $STOCKHOLM, '<', $file or $self->throw("Could not read file '$file': $!"); my %done_ids; diff --git a/Bio/Index/SwissPfam.pm b/Bio/Index/SwissPfam.pm index c306ef2cf..67ca7fd2d 100644 --- a/Bio/Index/SwissPfam.pm +++ b/Bio/Index/SwissPfam.pm @@ -136,7 +136,7 @@ sub _index_file { $begin = 0; $end = 0; - open my $SP, '<', $file or $self->throw("Can't open file for read : $file - $!"); + open my $SP, '<', $file or $self->throw("Could not read file '$file': $!"); # In Windows, text files have '\r\n' as line separator, but when reading in # text mode Perl will only show the '\n'. This means that for a line "ABC\r\n", diff --git a/Bio/Index/Swissprot.pm b/Bio/Index/Swissprot.pm index ae113ac29..48b874227 100644 --- a/Bio/Index/Swissprot.pm +++ b/Bio/Index/Swissprot.pm @@ -149,7 +149,7 @@ sub _index_file { my $id_parser = $self->id_parser; - open my $SWISSPROT,'<',$file or $self->throw("Can't read file: $file"); + open my $SWISSPROT, '<', $file or $self->throw("Could not read file '$file': $!"); my %done_ids; diff --git a/Bio/LiveSeq/IO/Loader.pm b/Bio/LiveSeq/IO/Loader.pm index 96c0c1287..ab2f4f652 100644 --- a/Bio/LiveSeq/IO/Loader.pm +++ b/Bio/LiveSeq/IO/Loader.pm @@ -879,8 +879,8 @@ sub _get_alignment { my $fastafile2="/tmp/tmpfastafile2"; my $grepcut='egrep -v "[[:digit:]]|^ *$|sequences" | cut -c8-'; # grep/cut my $alignprogram="/usr/local/etc/bioinfo/fasta2/align -s /usr/local/etc/bioinfo/fasta2/idnaa.mat $fastafile1 $fastafile2 2>$null | $grepcut"; # ALIGN - open my $TMPFASTAFILE1,">$fastafile1" || croak "Cannot write into $fastafile1 for aa alignment"; - open my $TMPFASTAFILE2,">$fastafile2" || croak "Cannot write into $fastafile1 for aa alignment"; + open my $TMPFASTAFILE1, '>', $fastafile1 or croak "Could not write file '$fastafile1' for aa alignment: $!"; + open my $TMPFASTAFILE2, '>', $fastafile2 or croak "Could not write file '$fastafile2' for aa alignment: $!"; print $TMPFASTAFILE1 ">firstseq\n$seq1\n"; print $TMPFASTAFILE2 ">secondseq\n$seq2\n"; close $TMPFASTAFILE1; diff --git a/Bio/MapIO/fpc.pm b/Bio/MapIO/fpc.pm index be55f2dbb..da54de31a 100644 --- a/Bio/MapIO/fpc.pm +++ b/Bio/MapIO/fpc.pm @@ -141,9 +141,9 @@ sub next_map{ if ($map->core_exists()) { $corfile = substr($filename,0,length($filename)-3)."cor"; - if (open(CORE,$corfile)) { - while(read(CORE,$BUFFER,2)) { - push(@cordata,unpack('n*', $BUFFER)); + if (open my $CORE, '<', $corfile) { + while( read($CORE, $BUFFER, 2) ) { + push @cordata, unpack('n*', $BUFFER); } } else { @@ -538,4 +538,4 @@ For more information on this project, please refer: The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _ -=cut \ No newline at end of file +=cut diff --git a/Bio/PhyloNetwork/GraphViz.pm b/Bio/PhyloNetwork/GraphViz.pm index 59d8e735e..212464e2c 100644 --- a/Bio/PhyloNetwork/GraphViz.pm +++ b/Bio/PhyloNetwork/GraphViz.pm @@ -32,9 +32,9 @@ Bio::PhyloNetwork::GraphViz - Interface between PhyloNetwork and GraphViz print $gv->as_text; - open PS, "> net.ps"; - print PS $gv->as_ps; - close PS; + open my $PS, '>', "net.ps" or die "Could not write file 'net.ps': $!\n"; + print $PS $gv->as_ps; + close $PS; =head1 DESCRIPTION diff --git a/Bio/PullParserI.pm b/Bio/PullParserI.pm index b802cddf3..4da9c0985 100644 --- a/Bio/PullParserI.pm +++ b/Bio/PullParserI.pm @@ -293,7 +293,7 @@ sub chunk { else { unless ($FORCE_TEMP_FILE) { # treat a string as a filehandle - open(my $fake_fh, "+<", \$thing); # requires perl 5.8 + open my $fake_fh, "+<", \$thing or $self->throw("Could not open file '$thing': $!"); # requires perl 5.8 $self->{_chunk} = Bio::Root::IO->new(-fh => $fake_fh); } else { diff --git a/Bio/SimpleAlign.pm b/Bio/SimpleAlign.pm index dd075e633..eb7e4540d 100644 --- a/Bio/SimpleAlign.pm +++ b/Bio/SimpleAlign.pm @@ -514,7 +514,7 @@ sub sort_by_list { } my $ct=1; - open(my $listfh, '<', $list) || $self->throw("can't open file for reading: $list"); + open my $listfh, '<', $list or $self->throw("Could not read file '$list': $!"); while (<$listfh>) { chomp; my $name=$_; diff --git a/Bio/Tools/Alignment/Consed.pm b/Bio/Tools/Alignment/Consed.pm index f7a390a72..acf73b978 100644 --- a/Bio/Tools/Alignment/Consed.pm +++ b/Bio/Tools/Alignment/Consed.pm @@ -237,18 +237,27 @@ sub count_sequences_with_grep { # Tom Christiansen's 'tcgrep' # http://www.cpan.org/modules/by-authors/id/TOMC/scripts/tcgrep.gz - open(my $FILE, $self->{'filename'}) or do { $self->warn("cannot open file ".$self->{'filename'}. " for grepping"); return}; + open my $FILE, '<', $self->{'filename'} or do { + $self->warn("Could not read file '$self->{'filename'}' for grepping: $!"); + return + }; my $counter =0; while(<$FILE>) { $counter++ if(/^AF/); } - close $FILE; - opendir(my $SINGLETS,$self->{'path'}); + + opendir my $SINGLETS, $self->{'path'}; foreach my $f ( readdir($SINGLETS) ) { - next unless ($f =~ /\.singlets$/); - open(my $FILE, File::Spec->catfile($self->{'path'},$f)) or do{ $self->warn("cannot open file ".File::Spec->catfile($self->{'path'},$f)); next }; - while(<$FILE>) { $counter++ if(/^>/) } - close $FILE; + next unless ($f =~ /\.singlets$/); + + my $singlet_file = File::Spec->catfile($self->{'path'}, $f); + open my $S_FILE, '<', $singlet_file or do { + $self->warn("Could not read file '$singlet_file': $!"); + next + }; + while(<$S_FILE>) { $counter++ if(/^>/) } + close $S_FILE; } + closedir $SINGLETS; return $counter; } @@ -384,10 +393,10 @@ sub freeze_hash { my %contigs = %{$self->{'contigs'}}; my $frozen = freeze(%contigs); umask 0001; - open (my $FREEZE,">$filename") or do { + open my $FREEZE, '>', $filename or do { $self->warn( "Bio::Tools::Alignment::Consed could not ". "freeze the contig hash because the file ". - "($filename) could not be opened: $!\n"); + "($filename) could not be opened: $!"); return 1; }; print $FREEZE $frozen; @@ -1332,9 +1341,9 @@ sub write_stats { my ($statsfilecontents) = $statistics_raw =~ s/.*\ \:\ //g; umask 0001; my $fh = Bio::Root::IO->new(-file=>"$stats_filename"); - # open(STATSFILE,">$stats_filename") or print("Could not open the statsfile: $!\n"); + # open my $STATSFILE, '>', $stats_filename or print "Could not write the statsfile: $!\n"); $fh->_print("$statsfilecontents"); - # close STATSFILE; + # close $STATSFILE; $fh->close(); } diff --git a/Bio/Tools/Lucy.pm b/Bio/Tools/Lucy.pm index 850be5e91..76cd72f30 100644 --- a/Bio/Tools/Lucy.pm +++ b/Bio/Tools/Lucy.pm @@ -259,7 +259,7 @@ sub _parse { my $file = $1; $self->warn("Opening $self->{seqfile} for parsing...\n") if $self->{lucy_verbose}; - open my $SEQ, $self->{seqfile} or $self->throw("Could not open sequence file: $self->{seqfile}"); + open my $SEQ, '<', $self->{seqfile} or $self->throw("Could not read sequence file '$self->{seqfile}': $!"); my ($name, $line); my $seq = ""; my @lines = <$SEQ>; @@ -293,17 +293,17 @@ sub _parse { # now parse quality values (check for presence of quality file first) if ($self->{qualfile}) { - open my $QUAL, "$self->{qualfile}" or $self->throw("Could not open quality file: $self->{qualfile}"); + open my $QUAL, '<', $self->{qualfile} or $self->throw("Could not read quality file '$self->{qualfile}': $!"); @lines = <$QUAL>; } elsif (-e "$file.qual") { $self->warn("You did not set qualfile, but I'm opening $file.qual\n") if $self->{lucy_verbose}; - $self->qualfile("$file.qual"); - open my $QUAL, "$file.qual" or $self->throw("Could not open quality file: $file.qual"); + $self->qualfile("$file.qual"); + open my $QUAL, '<', "$file.qual" or $self->throw("Could not read quality file '$file.qual': $!"); @lines = <$QUAL>; } else { - $self->warn("I did not find a quality file. You will not be able to use all of the accessor methods.\n") if $self->{lucy_verbose}; + $self->warn("I did not find a quality file. You will not be able to use all of the accessor methods.\n") if $self->{lucy_verbose}; @lines = (); } @@ -332,17 +332,17 @@ sub _parse { # determine whether reads are full length if ($self->{infofile}) { - open my $INFO, "$self->{infofile}" or $self->throw("Could not open info file: $self->{infofile}"); + open my $INFO, '<', $self->{infofile} or $self->throw("Could not read info file '$self->{infofile}': $!"); @lines = <$INFO>; } elsif (-e "$file.info") { $self->warn("You did not set infofile, but I'm opening $file.info\n") if $self->{lucy_verbose}; $self->infofile("$file.info"); - open my $INFO, "$file.info" or $self->throw("Could not open info file: $file.info"); + open my $INFO, '<', "$file.info" or $self->throw("Could not read info file '$file.info': $!"); @lines = <$INFO>; } else { - $self->warn("I did not find an info file. You will not be able to use all of the accessor methods.\n") if $self->{lucy_verbose}; + $self->warn("I did not find an info file. You will not be able to use all of the accessor methods.\n") if $self->{lucy_verbose}; @lines = (); } @@ -356,17 +356,17 @@ sub _parse { # parse rejects (and presence of poly-A if Lucy has been modified) if ($self->{stderrfile}) { - open my $STDERR_LUCY, "$self->{stderrfile}" or $self->throw("Could not open quality file: $self->{stderrfile}"); + open my $STDERR_LUCY, '<', $self->{stderrfile} or $self->throw("Could not read quality file '$self->{stderrfile}': $!"); @lines = <$STDERR_LUCY>; } elsif (-e "$file.stderr") { $self->warn("You did not set stderrfile, but I'm opening $file.stderr\n") if $self->{lucy_verbose}; $self->stderrfile("$file.stderr"); - open my $STDERR_LUCY, "$file.stderr" or $self->throw("Could not open quality file: $file.stderr"); + open my $STDERR_LUCY, '<', "$file.stderr" or $self->throw("Could not read quality file '$file.stderr': $!"); @lines = <$STDERR_LUCY>; } else { - $self->warn("I did not find a standard error file. You will not be able to use all of the accessor methods.\n") if $self->{lucy_verbose}; + $self->warn("I did not find a standard error file. You will not be able to use all of the accessor methods.\n") if $self->{lucy_verbose}; @lines = (); } diff --git a/Bio/Tools/Run/RemoteBlast.pm b/Bio/Tools/Run/RemoteBlast.pm index 92e230c64..87c7d2755 100644 --- a/Bio/Tools/Run/RemoteBlast.pm +++ b/Bio/Tools/Run/RemoteBlast.pm @@ -645,30 +645,32 @@ sub retrieve_blast { =cut sub save_output { - my ($self, $filename) = @_; - if( ! defined $filename ) { - $self->throw("Can't save blast output. You must specify a filename to save to."); + my ($self, $filename) = @_; + if( not defined $filename ) { + $self->throw("Can't save blast output. You must specify a filename to save to."); + } + my $blastfile = $self->file; + #open temp file and output file, have to filter out some HTML + open my $TMP, '<', $blastfile or $self->throw("Could not read file '$blastfile': $!"); + + open my $SAVEOUT, '>', $filename or $self->throw("Could not write file '$filename': $!"); + my $seentop = 0; + while (my $line = <$TMP>) { + next if ($line =~ /
/);
+
+        if (   $line =~ /^(?:[T]?BLAST[NPX])\s*.+$/i
+            or $line =~ /^RPS-BLAST\s*.+$/i
+            or $line =~ /<\?xml\sversion=/
+            or $line =~ /^#\s+(?:[T]?BLAST[NPX])\s*.+$/
+           ) {
+            $seentop = 1;
         }
-        my $blastfile = $self->file;
-        #open temp file and output file, have to filter out some HTML
-        open(my $TMP, $blastfile) or $self->throw("cannot open $blastfile");
-
-        open(my $SAVEOUT, ">", $filename) or $self->throw("cannot open $filename");
-        my $seentop = 0;
-        while(<$TMP>) {
-                next if (/
/);
-                if(/^(?:[T]?BLAST[NPX])\s*.+$/i ||
-           /^RPS-BLAST\s*.+$/i ||
-           /<\?xml\sversion=/ ||
-           /^#\s+(?:[T]?BLAST[NPX])\s*.+$/) {
-                        $seentop=1;
-                } 
         next if !$seentop;
-                if( $seentop ) {
-                        print $SAVEOUT $_;
-                }
+        if ( $seentop ) {
+            print $SAVEOUT $line;
         }
-        return 1;
+    }
+    return 1;
 }
 
 sub _load_input {
diff --git a/Bio/Tools/Run/WrapperBase/CommandExts.pm b/Bio/Tools/Run/WrapperBase/CommandExts.pm
index 3d7f20d35..dfb171deb 100644
--- a/Bio/Tools/Run/WrapperBase/CommandExts.pm
+++ b/Bio/Tools/Run/WrapperBase/CommandExts.pm
@@ -946,15 +946,15 @@ sub _run {
 	# set up redirects and file switches
 	for (@$filespec) {
 	    m/^1?>#?(.*)/ && do {
-		defined($args{$1}) && ( open($out,">", $args{$1}) or $self->throw("Open for write error : $!"));
+		defined($args{$1}) && ( open $out, '>', $args{$1} or $self->throw("Could not write file '$args{$1}': $!") );
 		next;
 	    };
 	    m/^2>#?(.*)/ && do {
-		defined($args{$1}) && (open($err, ">", $args{$1}) or $self->throw("Open for write error : $!"));
+		defined($args{$1}) && ( open $err, '>', $args{$1} or $self->throw("Could not write file '$args{$1}': $!") );
 		next;
 	    };
 	    m/^<#?(.*)/ && do {
-		defined($args{$1}) && (open($in, "<", $args{$1}) or $self->throw("Open for read error : $!"));
+		defined($args{$1}) && ( open $in, '<', $args{$1} or $self->throw("Could not read file '$args{$1}': $!") );
 		next;
 	    };
 	    if (m/^-(.*)\|/) {
diff --git a/Bio/Tools/ipcress.pm b/Bio/Tools/ipcress.pm
index da544b2f2..e05ef242f 100644
--- a/Bio/Tools/ipcress.pm
+++ b/Bio/Tools/ipcress.pm
@@ -147,9 +147,9 @@ sub new {
   my @result;
 
   if ($file) {
-      open FH, $file;
-      @result = ();
-      close FH;
+      open my $FH, '<', $file or $self->throw("Could not read file '$file': $!");
+      @result = (<$FH>);
+      close $FH;
   }
   elsif ($fh) {
       @result = (<$fh>);
diff --git a/Bio/Tree/Draw/Cladogram.pm b/Bio/Tree/Draw/Cladogram.pm
index f2eee8a35..5725675db 100644
--- a/Bio/Tree/Draw/Cladogram.pm
+++ b/Bio/Tree/Draw/Cladogram.pm
@@ -435,7 +435,7 @@ sub print {
   my ($file) = $self->_rearrange([qw(FILE)], @args);
   $file ||= "output.eps"; # stdout
 
-  open(my $INFO,">", $file);
+  open my $INFO, '>', $file or $self->throw("Could not write file '$file': $!");
   print $INFO "%!PS-Adobe-\n";
   print $INFO "%%BoundingBox: 0 0 ", $width, " ", $height, "\n";
   print $INFO "1 setlinewidth\n";
diff --git a/Bio/Tree/Tree.pm b/Bio/Tree/Tree.pm
index 7e8789183..89525292c 100644
--- a/Bio/Tree/Tree.pm
+++ b/Bio/Tree/Tree.pm
@@ -389,7 +389,7 @@ sub as_text {
     $self->_load_module($iomod);
 
     my $string = '';
-    open my $fh, '>', \$string or die "Couldn't open $string as file: $!\n";
+    open my $fh, '>', \$string or $self->throw("Could not write '$string' as file: $!");
     my $test = $iomod->new( -format => $format, -fh => $fh );
 
     # Get the default params for the given IO module.
diff --git a/Bio/Tree/TreeFunctionsI.pm b/Bio/Tree/TreeFunctionsI.pm
index defcf0356..93018b1c9 100644
--- a/Bio/Tree/TreeFunctionsI.pm
+++ b/Bio/Tree/TreeFunctionsI.pm
@@ -1156,7 +1156,7 @@ sub _read_trait_file {
 
     my $trait_name;
     my $trait_values;
-    open my $TRAIT, '<', $file or $self->throw("Could not open file $file: $!\n");
+    open my $TRAIT, '<', $file or $self->throw("Could not read file '$file': $!");
 
     my $first_line = 1;
     while (<$TRAIT>) {
diff --git a/Build.PL b/Build.PL
index e998805d3..c4ca1204b 100644
--- a/Build.PL
+++ b/Build.PL
@@ -322,7 +322,7 @@ sub make_bdb_test {
     my $path0 = File::Spec->catfile('t', 'LocalDB', 'SeqFeature.t');
     my $path = File::Spec->catfile('t', 'LocalDB','SeqFeature_BDB.t');
     unlink($path) if (-e $path);
-    open(my $F, ">", $path) || die "Can't create test file\n";
+    open my $F, '>', $path or die "Could not write test file '$path': $!\n";
     print $F <notes('test_db');
     my $user = $build->notes('test_user');
     my $pass = $build->notes('test_pass');
-    open my $F,">$path";
+    open my $F, '>', $path or die "Could not write file '$path' for DBI test: $!\n";
     my $str = "$path0 -adaptor DBI::$driver -create 1 -temp 1 -dsn \"$dsn\"";
     $str .= " -user $user" if $user;
     $str .= " -password $pass" if $pass;
diff --git a/doc/Deobfuscator/bin/deob_index.pl b/doc/Deobfuscator/bin/deob_index.pl
index be32c312c..ae21eb767 100755
--- a/doc/Deobfuscator/bin/deob_index.pl
+++ b/doc/Deobfuscator/bin/deob_index.pl
@@ -445,7 +445,7 @@ sub extract_pod {
 
     $stats{'files'}++;
 
-    open my $fh, $File::Find::name or die "deob_index.pl: couldn't open $file:$!\n";
+    open my $fh, '<', $File::Find::name or die "deob_index.pl: could not read file '$file': $!\n";
 
     # these have to be done in order
     my ( $pkg_name, $short_desc ) = get_pkg_name($fh);
diff --git a/doc/Deobfuscator/cgi-bin/deob_interface.cgi b/doc/Deobfuscator/cgi-bin/deob_interface.cgi
index 0efc64a59..66ee52ef3 100755
--- a/doc/Deobfuscator/cgi-bin/deob_interface.cgi
+++ b/doc/Deobfuscator/cgi-bin/deob_interface.cgi
@@ -249,8 +249,8 @@ my $style4
 my $style5 = qq{style="font-family:verdana;font-size:14px;padding:3"};
 
 # Open file containing all Bioperl package names
-open( MODS, $PERLMODULES )
-    or die "Can't open list of Perl module names $PERLMODULES: $!\n";
+open my $MODS, '<', $PERLMODULES
+    or die "Could not read list of Perl module names '$PERLMODULES': $!\n";
 
 # Open BerkeleyDB by getting hash references
 $ref_BerkeleyDB_packages = Deobfuscator::open_db($BerkeleyDB_packages);
@@ -261,7 +261,7 @@ my $pattern = param('search_string') ? param('search_string') : ' ';
 $pattern =~ s/\s//g;
 
 # Filter file names with user search string if one has been entered
-while () {
+while (<$MODS>) {
     if (/\S+/) {    # capture list of all module names in case there are no
                     # matches found to user input string
         push @all_modules, $_;
@@ -282,7 +282,7 @@ while () {
 if ( scalar @available_modules < 1 ) {
     @available_modules = @all_modules;
 }
-close MODS or die "Can't close list of Perl module names $PERLMODULES: $!";
+close $MODS or die "Could not close list of Perl module names $PERLMODULES: $!\n";
 
 # grab BioPerl version string
 my $version_string = '__BioPerl_Version'; # specified in deob_index.pl
diff --git a/examples/contributed/rebase2list.pl b/examples/contributed/rebase2list.pl
index 178696912..34bd2f712 100755
--- a/examples/contributed/rebase2list.pl
+++ b/examples/contributed/rebase2list.pl
@@ -4,9 +4,9 @@
 
 my $strider = $ARGV[0]; #commercial_version_rebase_strider_format
 
-open (FILEIN,"$strider") or die "can't open $strider: $!\n";
+open my $FILEIN, '<', $strider or die "Could not read file '$strider': $!\n";
 
-while (){
+while (<$FILEIN>){
    chomp;
    if ( /^[A-Z]\S+,\S+/ ){
       ($enzyme,$cutsite)=split(',');
@@ -18,5 +18,4 @@ while (){
       print " \'$enzyme\'\t=> \'".$seqfixed." ".$match."\'\,\n";
    }
 }
-
-
+close $FILEIN;
diff --git a/examples/db/rfetch.pl b/examples/db/rfetch.pl
index f63be743e..4cd7614b1 100755
--- a/examples/db/rfetch.pl
+++ b/examples/db/rfetch.pl
@@ -39,11 +39,12 @@ my $seqout = Bio::SeqIO->new( '-format' => $format, '-fh' => \*STDOUT);
 my @ids;
 
 if( defined $file ) {
-  open(F,$file) || die "cannot open $file $!";
-  while(  ) {
+  open my $F, '<', $file or die "Could not read file '$file': $!\n";
+  while( <$F> ) {
     my ($id) = split;
     push(@ids,$id);
   }
+  close $F;
 } else {
   @ids = @ARGV;
 }
diff --git a/examples/make_primers.pl b/examples/make_primers.pl
index 4df6259ad..8987019ab 100644
--- a/examples/make_primers.pl
+++ b/examples/make_primers.pl
@@ -137,7 +137,7 @@ sub open_readfile {
 	chomp ($filename=);
     }
     unless (-e $filename) { die "$filename not found\n"; }
-    open($filehandle,$filename) or die "Couldn't open $filename\n";
+    open $filehandle,'<', $filename or die "Could not read file '$filename': $!\n";
     $filehandle = '';
     $filetype = '';
     $filename = '';
@@ -159,9 +159,9 @@ sub open_writefile {
 	    chomp ($_ = );
 	}
 	if (/n/i) { die "$filename not overwritten.\n"; }
-	else { open($filehandle, ">$filename") or die "Couldn't open $filename\n"; }
+	else { open $filehandle, '>', $filename or die "Could nott write file '$filename':ยท$!\n"; }
     }
-    else { open($filehandle, ">$filename") or die "Couldn't open $filename\n"; }
+    else { open $filehandle, '>', $filename or die "Could not write file '$filename': $!\n"; }
     $filehandle = '';
     $filetype = '';
     $filename = '';
diff --git a/examples/subsequence.cgi b/examples/subsequence.cgi
index 2851b5ee7..312089b8d 100644
--- a/examples/subsequence.cgi
+++ b/examples/subsequence.cgi
@@ -64,13 +64,13 @@ sub print_results {
   
   # Now we parse the FASTA file which was just generated, and perform
   # some simple conversions to HTML.   
-  open (TEMPORARY, "<$filename") or die "unable to open temporary file $filename\n";
+  open my $TEMPORARY, '<', $filename or die "Could not read temporary file '$filename': $!\n";
   print "\n";
-  while () {
+  while (<$TEMPORARY>) {
     print $_;
     print "
\n"; } - close TEMPORARY; + close $TEMPORARY; print "
\n"; unlink $filename; } diff --git a/examples/tools/extract_genes.pl b/examples/tools/extract_genes.pl index 5b09d05a2..7d6ebd7b0 100755 --- a/examples/tools/extract_genes.pl +++ b/examples/tools/extract_genes.pl @@ -86,15 +86,17 @@ my $db = Bio::DB::Fasta->new($name, -makeid => \&make_my_id); unless (-e $storedHash) { my $ref; # extract species-specific information from gene2accession - open MYIN,"gene2accession" or die "No gene2accession file\n"; - while () { - my @arr = split "\t",$_; + my $file = 'gene2accession'; + open my $MYIN, '<', $file or die "Could not read file '$file': $!\n"; + while (my $line = <$MYIN>) { + my @arr = split "\t", $line; if ($arr[0] == $species{$name} && $arr[9] =~ /\d+/ && $arr[10] =~ /\d+/) { ($ref->{$arr[1]}->{"start"}, $ref->{$arr[1]}->{"end"}, $ref->{$arr[1]}->{"strand"}, $ref->{$arr[1]}->{"id"}) = ($arr[9], $arr[10], $arr[11], $arr[7]); } } + close $MYIN; # save species-specific information using Storable store $ref, $storedHash; } diff --git a/maintenance/authors.pl b/maintenance/authors.pl index d3e66eada..37289b6a4 100755 --- a/maintenance/authors.pl +++ b/maintenance/authors.pl @@ -85,17 +85,17 @@ my @dirs = qw( ../Bio/ ../scripts . ); # -open (F, $authorsfile) || die "can't open file $authorsfile: $!"; +open my $F, '<', $authorsfile or die "Could not read file '$authorsfile': $!\n"; -while () { - my ($email) = /([\.\w_-]+ at [\.\w_-]+)/; +while (my $line = <$F>) { + my ($email) = ($line =~/([\.\w_-]+ at [\.\w_-]+)/); next unless $email; #print $email, "\n"; $email =~ s/ at /@/; $AUTHORS{$email} = 1; } -close F; +close $F; # @@ -124,22 +124,25 @@ print Dumper \%NEWAUTHORS; # this is where the action is # sub findauthors { - return unless /\.PLS$/ or /\.p[ml]$/ ; - return unless -e $_; - print "$_\n" if $verbose; - my $filename = $_; + my ($filename) = @_; + return unless ($filename =~ /\.PLS$/ or $filename =~ /\.p[ml]$/); + return unless -e $filename; + + print "$filename\n" if $verbose; #local $/=undef; - open F, $_ || die "Could not open file $_"; - while () { - #print; - last if /=head1 +AUTHOR/; + open my $F, '<', $filename or die "Could not read file '$filename': $!\n"; + while (my $line = <$F>) { + #print $line; + last if $line =~ /=head1 +AUTHOR/; } my $authorblock; - while () { - last if /=head/ and not /CONTRIBUTORS/; - $authorblock .= $_; + while (my $line = <$F>) { + last if $line =~ /=head/ and $line !~ /CONTRIBUTORS/; + $authorblock .= $line; } + close $F; return unless $authorblock; + while ( $authorblock =~ /([\.\w_-]+@[\.a-z_-]+)/g) { #my $email = $1; #$email =~ // @@ -147,7 +150,6 @@ sub findauthors { #print "$filename\t$1\n"; push @{$NEWAUTHORS{$1}}, $filename; - } } @@ -172,18 +174,19 @@ feeling too lazy to change the code. =cut sub blankline { - return unless /\.PLS$/ or /\.p[ml]$/ ; - return unless -e $_; - my $file = $_; - open (F, $_) or warn "can't open file $_: $!" && return; - local $/=""; - while () { - print "$file: +|$1|\n" if /[ \t]\n(=[a-z][^\n]+$)/m and $verbose; - print "$file: ++|$1|\n" if /\w\n(=[a-z][^\n]+$)/m and $verbose; - print "$file:|$1|+\n" if /(^=[a-z][^\n]+)\n[\t ]/m; + my ($file) = @_; + return unless ($file =~ /\.PLS$/ or $file =~ /\.p[ml]$/); + return unless -e $file; + + open my $F, '<', $file or warn "Could not read file '$file': $!\n" && return; + local $/ = ""; + while (my $line = <$F>) { + print "$file: +|$1|\n" if $line =~ /[ \t]\n(=[a-z][^\n]+$)/m and $verbose; + print "$file: ++|$1|\n" if $line =~ /\w\n(=[a-z][^\n]+$)/m and $verbose; + print "$file:|$1|+\n" if $line =~ /(^=[a-z][^\n]+)\n[\t ]/m; #print "$file:|$1|++\n" if /(^=[^\n]+)\n\w/m; } - close F; + close $F; } __END__ diff --git a/maintenance/check_URLs.pl b/maintenance/check_URLs.pl index 746b3b5d1..9e28f3f54 100755 --- a/maintenance/check_URLs.pl +++ b/maintenance/check_URLs.pl @@ -53,7 +53,7 @@ GetOptions( my $fh; if (defined $file) { - open($fh, '>', $file) || die "Can't open file : $!"; + open $fh, '>', $file or die "Could not write file '$file': $!\n"; } else { $fh = \*STDOUT; } diff --git a/maintenance/cvs2cl_by_file.pl b/maintenance/cvs2cl_by_file.pl index dfd44774b..014a1147f 100755 --- a/maintenance/cvs2cl_by_file.pl +++ b/maintenance/cvs2cl_by_file.pl @@ -1305,34 +1305,36 @@ my $self = shift; my $class = ref $self; # don't append the most recent entry, since it's already in the # new log due to CVS's idiosyncratic interpretation of "log -d". if ($Cumulative && -f $logfile_here) { - open NEW_LOG, ">>$tmpfile" - or die "trouble appending to $tmpfile ($!)"; - - open OLD_LOG, "<$logfile_here" - or die "trouble reading from $logfile_here ($!)"; - - my $started_first_entry = 0; - my $passed_first_entry = 0; - while () { - if ( ! $passed_first_entry ) { - if ( ( ! $started_first_entry ) - and /^(\d\d\d\d-\d\d-\d\d\s+(\w+\s+)?\d\d:\d\d)/ ) { - $started_first_entry = 1; - } elsif ( /^(\d\d\d\d-\d\d-\d\d\s+(\w+\s+)?\d\d:\d\d)/ ) { - $passed_first_entry = 1; - print NEW_LOG $_; - } - } else { - print NEW_LOG $_; + open $NEW_LOG, '>>', $tmpfile + or die "Could not append to file '$tmpfile': $!\n"; + + open $OLD_LOG, '<', $logfile_here + or die "Could not read file '$logfile_here': $!"; + + my $started_first_entry = 0; + my $passed_first_entry = 0; + while (<$OLD_LOG>) { + if ( ! $passed_first_entry ) { + if ( ( ! $started_first_entry ) + and /^(\d\d\d\d-\d\d-\d\d\s+(\w+\s+)?\d\d:\d\d)/ + ) { + $started_first_entry = 1; + } + elsif ( /^(\d\d\d\d-\d\d-\d\d\s+(\w+\s+)?\d\d:\d\d)/ ) { + $passed_first_entry = 1; + print $NEW_LOG $_; + } + } + else { + print $NEW_LOG $_; + } } - } - - close NEW_LOG; - close OLD_LOG; + close $NEW_LOG; + close $OLD_LOG; } if ( -f $logfile_here ) { - rename $logfile_here, $logfile_bak; + rename $logfile_here, $logfile_bak; } rename $tmpfile, $logfile_here; } @@ -1957,7 +1959,7 @@ sub read_changelog { &main::debug ("(run \"@$command\")\n"); } else { - open READER, '-' or die "unable to open stdin for reading"; + open READER, '-' or die "Unable to open stdin for reading"; } binmode READER; @@ -3232,4 +3234,3 @@ Boston, MA 02111-1307, USA. =head1 SEE ALSO cvs(1) - diff --git a/maintenance/dependencies.pl b/maintenance/dependencies.pl index f9bf24323..27f6ac2d6 100644 --- a/maintenance/dependencies.pl +++ b/maintenance/dependencies.pl @@ -99,7 +99,7 @@ for my $key (sort keys %dependencies) { } } -open (my $dfile, '>', $depfile) || die "Can't open dependency file :$!\n"; +open my $dfile, '>', $depfile or die "Could not write dependency file '$depfile': $!\n"; print $dfile $dep_header; @@ -157,7 +157,7 @@ sub parse_core { my $file = $_; return unless $file =~ /\.PLS$/ || $file =~ /\.p[ml]$/ ; return unless -e $file; - open my $F, $file || die "Could not open file $file"; + open my $F, '<', $file or die "Could not read file '$file': $!\n"; my $pod = ''; MODULE_LOOP: while (my $line = <$F>) { diff --git a/maintenance/deprecated.pl b/maintenance/deprecated.pl index 078bde2bd..57f303777 100644 --- a/maintenance/deprecated.pl +++ b/maintenance/deprecated.pl @@ -41,7 +41,7 @@ my @dep_data; # parse DEPRECATED file -open my $DFILE, '<', $depfile || die "Can't open $depfile: $!"; +open my $DFILE, '<', $depfile or die "Could not read file '$depfile': $!\n"; my $seen_top; while (my $data = <$DFILE>) { if ($data =~ /^-+$/) { @@ -65,7 +65,6 @@ while (my $data = <$DFILE>) { remove => $rem, note => $note} } - close $DFILE; for my $new (@$new) { @@ -96,7 +95,7 @@ if ($dir) { if ($write || @$new) { -open (my $NEWDEP, '>', $outfile) || croak "Can't open $outfile :$!"; +open my $NEWDEP, '>', $outfile or croak "Could not write file '$outfile': $!\n"; print $NEWDEP <) { if ($line =~ /(?:['"])?\b(use|require)\s+([A-Za-z0-9:_\.\(\)]+)\s*([^;'"]+)?(?:['"])?\s*;/) { my ($use, $mod) = ($1, $2); diff --git a/maintenance/find_mod_deps.pl b/maintenance/find_mod_deps.pl index a5448f945..721a49144 100755 --- a/maintenance/find_mod_deps.pl +++ b/maintenance/find_mod_deps.pl @@ -162,7 +162,7 @@ sub find_deps { my ( $file ) = @_; my $nopod; - { open my $p, '<', $file or die "$! reading $file\n"; + { open my $p, '<', $file or die "Could not read file '$file': $!\n"; local $/; my $code = <$p>; my $strip = Pod::Strip->new; @@ -213,7 +213,7 @@ sub find_deps { next unless $p_modfile && -f $p_modfile; - open my $p, '<', $p_modfile or die "$! opening $p_modfile\n"; + open my $p, '<', $p_modfile or die "Could not read file '$p_modfile': $!\n"; while( <$p> ) { next MODNAME if /^\s*package\s+$p\b/; } diff --git a/maintenance/modules.pl b/maintenance/modules.pl index 2cae08002..a4d5922ae 100755 --- a/maintenance/modules.pl +++ b/maintenance/modules.pl @@ -232,9 +232,9 @@ sub modules { return unless /\.pm$/ ; #return unless -e $_; #print "file: $_\n" if $verbose; - open (F, $_) or warn "can't open file $_: $!" && return; + open my $F, '<', $_ or warn "Could not read file '$_': $!\n" && return; my $class; - while () { + while (<$F>) { if (/^package\s+([\w:]+)\s*;/) { #print $1, "\n" if $verbose; $_ = $1; @@ -273,7 +273,7 @@ sub modules { } } } - close F; + close $F; } =head1 OPTIONS @@ -469,10 +469,10 @@ sub synopsis { next if $c->name eq 'Bio::Tools::HMM'; my $synopsis = ''; - open (F, $c->path) or warn "can't open file ".$c->name.": $!" && return; + open my $F, '<', $c->path or warn "Could not read file '".$c->name."': $!\n" && return; my $flag = 0; - while () { + while (<$F>) { last if $flag && /^=/; $synopsis .= $_ if $flag; $flag = 1 if /^=head1 +SYNOPSIS/; @@ -492,7 +492,7 @@ sub synopsis { print $res; print "-" x 70, "\n"; # print "SYNOPSIS not runnable\n"; - close F; + close $F; } } @@ -563,6 +563,3 @@ Heikki Lehvaslaiho, heikki-at-bioperl-dot-org Albert Vilella, avilella-AT-gmail-DOT-com =cut - - - diff --git a/scripts/Bio-DB-GFF/bp_genbank2gff.pl b/scripts/Bio-DB-GFF/bp_genbank2gff.pl index 0bd27b711..2e1144630 100644 --- a/scripts/Bio-DB-GFF/bp_genbank2gff.pl +++ b/scripts/Bio-DB-GFF/bp_genbank2gff.pl @@ -251,13 +251,14 @@ elsif ($accFILE){ my $filename = shift; die "you must supply a filename after the --accFILE command line flag\n" unless $filename; die "file $filename does not exist\n" unless (-e $filename && !(-d $filename)); - open IN, "$filename" || die "Can't open file $filename for reading accession numbers: $!\n"; - while (){ - chomp; - status(loading => $_); - my $result = $db->load_from_embl(/^NC_/?'refseq':'embl' => $_); + open my $IN, '<', $filename or die "Could not read file '$filename' for reading accession numbers: $!\n"; + while (my $line = <$IN>){ + chomp $line; + status(loading => $line); + my $result = $db->load_from_embl(/^NC_/?'refseq':'embl' => $line); status(done => $result); } + close $IN; exit 1; } diff --git a/scripts/Bio-DB-GFF/bp_genbank2gff3.pl b/scripts/Bio-DB-GFF/bp_genbank2gff3.pl index 81f3955a4..4acc7a8a2 100644 --- a/scripts/Bio-DB-GFF/bp_genbank2gff3.pl +++ b/scripts/Bio-DB-GFF/bp_genbank2gff3.pl @@ -445,12 +445,12 @@ for my $file ( @files ) { # open input file, unzip if req'd if ($stdin) { - *FH= *STDIN; + *FH = *STDIN; } elsif ( $file =~ /\.gz/ ) { open FH, "gunzip -c $file |"; } else { - open FH, "<$file"; + open FH, '<', $file; } my $in = Bio::SeqIO->new(-fh => \*FH, -format => $FORMAT, -debug=>$DEBUG); diff --git a/scripts/Bio-DB-GFF/bp_process_gadfly.pl b/scripts/Bio-DB-GFF/bp_process_gadfly.pl index b8f34e971..ab934ed46 100644 --- a/scripts/Bio-DB-GFF/bp_process_gadfly.pl +++ b/scripts/Bio-DB-GFF/bp_process_gadfly.pl @@ -102,20 +102,24 @@ sub dump_symbol { sub process_fasta { my $file = shift @ARGV; - open F,$file or die "Can't open $file: $!"; + open my $F, '<', $file or die "Could not read file '$file': $!\n"; print STDERR "Reading big FASTA file, please be patient...\n"; my ($current_id,%lengths); - while () { - if (/^>(\S+)/) { + while (my $line = <$F>) { + if ($line =~ /^>(\S+)/) { $current_id = $1; next; } die "this doesn't look like a fasta file to me" unless $current_id; - chomp; - $lengths{$current_id} += length; + chomp $line; + $lengths{$current_id} += length $line; } - foreach (sort keys %lengths) { - print join("\t",$_,'arm','Component',1,$lengths{$_},'.','+','.',qq(Sequence "$_")),"\n"; + close $F; + + foreach my $id (sort keys %lengths) { + print join("\t", $id, 'arm', 'Component', 1, $lengths{$id}, + '.', '+', '.', qq(Sequence "$id") + ), "\n"; } } diff --git a/scripts/das/bp_das_server.pl b/scripts/das/bp_das_server.pl index 86936285c..169b7df7b 100644 --- a/scripts/das/bp_das_server.pl +++ b/scripts/das/bp_das_server.pl @@ -412,11 +412,11 @@ END # directory. Used primarily for testing. sub stylesheet { ok_header(); - open(STYLE, "style.xml"); - while(