Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re (tilly) 3: Generating similar functions in unrelated objects

by tilly (Archbishop)
on Aug 31, 2001 at 07:25 UTC ( #109280=note: print w/ replies, xml ) Need Help??


in reply to Generating similar functions in unrelated objects
in thread Why I hate File::Find and how I (hope I) fixed it

You would want to do a typeglob assignment. If you assign an anonymous function to a typeglob, you define that function. Like this:

package Stringify; sub objectToString { my $name = shift; return sub { $name . (shift)->getParametersAndValues(); }; } # Time passes package Fu; *toString = Stringify::objectToString("Fu");
But I should note that you can make this nicer with overload and an autogenerated method. For instance:
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;
would allow you to simply:
package Fu; use Stringify; # Use objects as strings, and they stringify
If you want you could create an autogenerated function called toString and pass a reference to that function to overload.


Comment on Re (tilly) 3: Generating similar functions in unrelated objects
Select or Download Code
Re: (t'mo) - (tilly) Generating similar functions in unrelated objects
by t'mo (Pilgrim) on Aug 31, 2001 at 16:37 UTC

    You know what happened, don't you? Just as I was leaving the building, not more than five minutes after I asked the question, the answer came to me: symbol table. :-)

    The import approach is interesting. It gives me more food for thought, especially in relation to this question I asked about writing your own import. Thanks.

      Are custom imports a good idea?

      Can I say it depends?

      Most of what I was saying about AUTOLOAD at Re (tilly) 4: Perl and Objects, how do you resolve the two? applies to custom imports as well. A custom import is like a very good sword without a hilt. It can cut really well, but you need to be very cautious before using it.

      My rule of thumb is this. If my custom import could be done with Exporter, I don't use it. If it makes for a significantly improved interface and works very differently, then I will consider it. But if I won't actually use it unless I am going to make it a meme in the body of code that I am working on. It doesn't matter how nice each customized rule is, a million and one customized high-level interfaces is unmaintainable.

      Or, to put it a different way, It is OK to be clever, but only one person should be being clever at a time.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://109280]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2014-09-17 19:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (95 votes), past polls