package Stringify;
sub objectToString {
my $name = shift;
return sub {
$name . (shift)->getParametersAndValues();
};
}
# Time passes
package Fu;
*toString = Stringify::objectToString("Fu");
####
package Stringify;
use strict;
use Carp;
sub import {
my $pkg = caller();
my $code = qq(
# A trick for custom error reporting - see perlsyn.
\n# line 1 "'Autogenerated for $pkg by Stringify'"
package $pkg;
use overload '""' => sub {
__PACKAGE__ . (shift)->getParametersAndValues();
};
);
eval($code);
if ($@) {
confess("Cannot autogenerate stringification for package '$pkg': $@
The code is:\n$code");
}
}
1;
##
##
package Fu;
use Stringify; # Use objects as strings, and they stringify