[Pdx-pm] OOP help

matthew wickline m_pm_pdx at wickline.org
Tue Jan 28 07:29:05 CST 2003


20030127 kellert at ohsu.edu wrote:
 > Is there an easy way to fool the subroutine
 > (actually an OOP method) into thinking that
 > it's getting a set of files instead of an array ?


Looking at your code, it looks like you have an array of strings, and 
each string is something you'd like to treat as a filehandle.

http://search.cpan.org/author/ERYQ/IO-stringy-2.108/lib/IO/Scalar.pm

IO::Scalar will likely do what you need. It lets you pass a reference to 
a scalar and get something back that will act like a filehandle.

Pulling code from your post, code from the Boulder::Blast docs, and 
using IO::Scalar, I came up with the code at the end of this email. I 
haven't tested or even syntax-checked it, so it may not work at all.

If you need to do this sort of thing in the future, you can turn an 
array of strings into an array of filehandles with

use IO::Scalar;
my @filehandles = map { IO::Scalar->new(\$_) } @strings;

-matt

  PS: I haven't actually looked at your data either. I'm
      taking on faith that the strings in your array are
      what Boulder::Blast expects to find in files.

PSS: everybody out there please get yourselves employed
      so you'll feel comfortable passing on perl job tips
      when my wife and I move to PDX this May/June. :)
      She'll be in an OHSU nursing program, and I'll be
      hitting the streets trying to find a job that lets
      me play with perl most or all of the day.
      Gracias



use Boulder::Blast;
use IO::Scalar;
# :
# :
# skipping the part where you read in a file, break it into
# parts that Boulder::Blast expects individual files to look
# like. We'll start from where you already have the 'files'
# as separate strings in the @queries array...
# :
# :

parse_blast_results( \@queries );
exit;

sub parse_blast_results {
     # takes an arrayref of strings as input,
     # each string the contents of a blast file;
     # calls report_on_blast_file to print a report on each
     my $queries = shift; # the arrayref we passed
     foreach my $result ( @$queries ) {
         # $result is a string with contents that
         # Boulder::Blast would expect in a file
         # We're going to make a filehandle out
         # of that string:
         my $fh = IO::Scalar->new( \$result );
         # and then do some normal Boulder::Blast stuff:
         report_on_blast_file( $fh );
     }
}
sub report_on_blast_file {
     # takes a filehandle to a blast output file
     # or the path of a file (any of the args that
     # Boulder::Blast::parse() is willing to accept)
     # and prints to STDOUT a report on that file
     my $blast = Boulder::Blast->parse( shift );
     my $query = $blast->Blast_query;
     foreach $hit ( $blast->Blast_hits ) {
         print join( ', ',
             "Query: $blast_query",
             'Hit: '    . $hit->Name,
             'Signif: ' . $hit->Signif,
         ),"\n";
         foreach my $hsp ( $hit->Hsps ) {
             print join( "\n",
                 $hsp->Query_start,
                 $hsp->Query_end,
                 $hsp->Name,
                 $hsp->Signif,
                 "\n",
             );
         }
     }
}




More information about the Pdx-pm-list mailing list