J
Josef Moellers
Hi,
I want to define a large set of almost identical functions:
sub name {
return $_[0]->{name};
}
with varying "name"s.
I thought I'd be clever and do
foreach my $f ("name") {
$PACK::{$f} = sub { return $_[0]->{$f}; }
}
As long as I do it in one source file, it works:
-8<-8<-8<-8<-8<-8<-8<-8<-8<-8<-8<-8<-
#! /usr/bin/perl
use warnings;
use strict;
# use FAT;
package FAT32;
use warnings;
use strict;
sub func1() {
print "Here is FAT32::func1\n";
}
$FAT32::{func2} = sub { print "Here is FAT32::func2\n"; };
1;
package main;
FAT32::func1();
FAT32::func2();
exit 0;
-8<-8<-8<-8<-8<-8<-8<-8<-8<-8<-8<-8<-
Output:
Here is FAT32::func1
Here is FAT32::func2
But when I put the package FAT32 into a separate file ("FAT.pm"), I get
Here is FAT32::func1
Undefined subroutine &FAT32::func2 called at test.pl line 23.
Is there any camel-dung I can put onto my hash assignment to make it
work cross-file?
Josef
I want to define a large set of almost identical functions:
sub name {
return $_[0]->{name};
}
with varying "name"s.
I thought I'd be clever and do
foreach my $f ("name") {
$PACK::{$f} = sub { return $_[0]->{$f}; }
}
As long as I do it in one source file, it works:
-8<-8<-8<-8<-8<-8<-8<-8<-8<-8<-8<-8<-
#! /usr/bin/perl
use warnings;
use strict;
# use FAT;
package FAT32;
use warnings;
use strict;
sub func1() {
print "Here is FAT32::func1\n";
}
$FAT32::{func2} = sub { print "Here is FAT32::func2\n"; };
1;
package main;
FAT32::func1();
FAT32::func2();
exit 0;
-8<-8<-8<-8<-8<-8<-8<-8<-8<-8<-8<-8<-
Output:
Here is FAT32::func1
Here is FAT32::func2
But when I put the package FAT32 into a separate file ("FAT.pm"), I get
Here is FAT32::func1
Undefined subroutine &FAT32::func2 called at test.pl line 23.
Is there any camel-dung I can put onto my hash assignment to make it
work cross-file?
Josef