Dispatch Tables Further
Here is a snipit of code that I used to test the idea, the actual script is a little more complicated.
#!/usr/bin/perl -w
use strict;
use IO::File;
my $fh = new IO::File;
my $file = "text.txt";
my $newString;
my $opers = {
'TX:' => \&text,
'END:' => \&end,
};
if ($fh->open($file)) {
my @file = < $fh>;
foreach my $string(@file) {
foreach my $key (keys %$opers){
if ($string =~ /^$key/) {
chomp($string);
($newString = $string) =~ s/^$key//;
$opers->{$key}->($newString);
}
}
}
}
sub text {
my $in = shift;
print $in
}
sub end {
my $in = shift;
print $in
}
It opens the file, reads it in and if the line begins with TX: or END: it passes the line to the approriate subroutine. I like it!