Q
QoS
Is there any other way to load a subroutine into an array?
#!/usr/bin/perl
use strict;
use warnings;
my @array;
@array = loadSub('example');
if (defined $array[0]) {
foreach my $l (@array) {
print $l;
}
}
exit;
sub loadSub #-----------------------------------------------------------
{
my $sub = quotemeta ($_[0]) || return (0);
my $this_perl = $0;
if (open (IN, '<', $this_perl)) {
my ($out, $found,);
while (my $line = (<IN>)) {
if (! $found && ! $line =~ m/^\s*sub\s+$sub/) {
next;
}
else {
$found = 1;
if ($line =~ m/^\s*}/) { last; }
$out .= $line;
}
}
if (! $found) {
warn 'Unable to find subroutine: ' . $sub . "\n";
$out = 0;
}
close IN
|| warn "Unable to close input file in loadSub\n";
return ($out);
}
else {
warn "Unable to read file: $this_perl\n$!\n";
}
return (0);
}
sub example #-----------------------------------------------------------
{
my ($a, $b,);
foreach my $n (1..5) { $a++; $b += $a + $n; }
return ($b);
}
#!/usr/bin/perl
use strict;
use warnings;
my @array;
@array = loadSub('example');
if (defined $array[0]) {
foreach my $l (@array) {
print $l;
}
}
exit;
sub loadSub #-----------------------------------------------------------
{
my $sub = quotemeta ($_[0]) || return (0);
my $this_perl = $0;
if (open (IN, '<', $this_perl)) {
my ($out, $found,);
while (my $line = (<IN>)) {
if (! $found && ! $line =~ m/^\s*sub\s+$sub/) {
next;
}
else {
$found = 1;
if ($line =~ m/^\s*}/) { last; }
$out .= $line;
}
}
if (! $found) {
warn 'Unable to find subroutine: ' . $sub . "\n";
$out = 0;
}
close IN
|| warn "Unable to close input file in loadSub\n";
return ($out);
}
else {
warn "Unable to read file: $this_perl\n$!\n";
}
return (0);
}
sub example #-----------------------------------------------------------
{
my ($a, $b,);
foreach my $n (1..5) { $a++; $b += $a + $n; }
return ($b);
}