Back to your original example, with comments:
# $formatThis is not known here
sub A { # begin of subroutine
# what is $OBJ ? is it a global variable? or is it passed into thi
+s sub?
# what is $p ? is it a global variable? or is it passed into thi
+s sub?
if ( exists $OBJ->{$p} ) {
my $q = $OBJ->{$p};
$q = &$<package_name>::formatThis($q); # wrong! <package_name>
+ means:
# read one record from filehandle 'pack
+age_name'
print FH "$p=$q ";
}
my $formatThis = sub { # $formatThis declared as my(), lexically
+scoped
my $x = @_; # wrong! evaluates @_ in scalar context!
my $y = sprintf ( "%.2f", $x);
return $y;
};
} # end of subroutine
# from here on $formatThis is not known
I guess you want to say something like this, but I can't know, because you don't tell us about the context of your code:
sub A {
my ($OBJ,$p) = @_;
if ( exists $OBJ->{$p} && exists $OBJ->{'sub'}) {
my $q = $OBJ->{$p};
$q = $OBJ->{'sub'}->($q); # execute the sub stored in $OBJ
print STDOUT "$p=$q\n";
}
my $formatThis = sub {
my ($x) = @_;
my $y = sprintf ( "%.2f", $x);
return $y;
};
$OBJ->{'sub'} = $formatThis;
}
# test it
my $ref = {};
$ref->{'float'} = 3.1415926;
A($ref); # get a sub
A($ref,'float'); # now doit
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
|