I want to interpolate methods in a string as scalars. I wrote some code
that works, but my questions are
1) I couldn't find a pre-existing module that does any of this. Can that
really be true?
2) I'm not happy with this solution. In particular, what is the scope of
the tied ${$symbol} ? Will it go out of scope at the end of the call to
interpolate_methods? If not, then each call to interpolate_methods will
clutter up namespace and memory with a bunch of little tied scalar
objects.
package NBER::InterpolateMethods;
use base Exporter;
our @EXPORT = qw(interpolate_methods);
sub interpolate_methods {
my($object,$template) = @_;
my $class = ref($object);
my $namespace = $class . '::';
foreach my $symbol ( keys %$namespace ) {
if ( $object->can($symbol) ) {
tie(${$symbol}, 'NBER::TieMethodScalar', $object, $symbol);
}
}
my $out = eval $template;
$@ and die "Error parsing template: $@ $!";
return $out;
}
package NBER::TieMethodScalar;
sub TIESCALAR {
my ($class,$object,$method) = @_;
my $self = {object=>$object,
method=>$method};
return bless $self, $class;
}
sub FETCH {
my $self = shift;
my $method = $self->{method};
return $self->{object}->$method();
}
sub STORE { die "can not use tied scalar to modify"; };
Thanks for any suggestions,
--
- Alex Aminoff
BaseSpace.net
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm