byuhobbes85
06-20-2008, 07:26 PM
I'm somewhat new to Perl, and very new to object-oriented programming in Perl. I've used OO programming extensively in Java and PHP, but I just have a question of how you would do something in Perl.
I have an object that I want to store, among other things, a list of filenames. The object is implemented as a hash, though, and I can see that you cannot store arrays in hashes. I tried storing a reference to the array in a scalar, but I am not sure what to do from here. Here is the code.
package BlastFactory;
# Constructor
sub new {
# Get parameters, set to an anonymous hash
my ($class, $filename) = @_;
return bless {
_query_files => load_query_files($filename)
}, $class;
}
# Load the query files into an array
sub load_query_files {
# Grab parameters
my ($file_to_load) = @_;
# Create an array to store query filenames
my @q_files = ();
# If the input file exists
if(-e $file_to_load) {
# ...load it
open INPUT, $file_to_load;
} else {
# Otherwise, die trying
die("ERROR: file '$file_to_load' does not exist");
}
# For each line of the input file...
while(<INPUT>) {
# ...grab the line
my $query_file = $_;
# Trim it
chomp($query_file);
# Add it to the array
push(@q_files, $query_file);
}
# Close the input streamm
close(INPUT);
# I have tested it here and the array is correct
return @q_files
}
# Count method...get number of query files
sub get_query_file_count {
my $count = @_[0]->{_query_files};
# When I call this method, however, it gives me the first value in the original array.
return $count;
}
# Success
1;
I've done a bit of testing so that I know the array is created correctly, but only the first value of the array is being stored in the object's hash. Does anyone have any tips? Thanks!
I have an object that I want to store, among other things, a list of filenames. The object is implemented as a hash, though, and I can see that you cannot store arrays in hashes. I tried storing a reference to the array in a scalar, but I am not sure what to do from here. Here is the code.
package BlastFactory;
# Constructor
sub new {
# Get parameters, set to an anonymous hash
my ($class, $filename) = @_;
return bless {
_query_files => load_query_files($filename)
}, $class;
}
# Load the query files into an array
sub load_query_files {
# Grab parameters
my ($file_to_load) = @_;
# Create an array to store query filenames
my @q_files = ();
# If the input file exists
if(-e $file_to_load) {
# ...load it
open INPUT, $file_to_load;
} else {
# Otherwise, die trying
die("ERROR: file '$file_to_load' does not exist");
}
# For each line of the input file...
while(<INPUT>) {
# ...grab the line
my $query_file = $_;
# Trim it
chomp($query_file);
# Add it to the array
push(@q_files, $query_file);
}
# Close the input streamm
close(INPUT);
# I have tested it here and the array is correct
return @q_files
}
# Count method...get number of query files
sub get_query_file_count {
my $count = @_[0]->{_query_files};
# When I call this method, however, it gives me the first value in the original array.
return $count;
}
# Success
1;
I've done a bit of testing so that I know the array is created correctly, but only the first value of the array is being stored in the object's hash. Does anyone have any tips? Thanks!