This is a question I asked about on Google+ a while ago: How do you have multiple subroutines responding to the same name, distinguished only by the subroutine signature? It isn't yet a part of the experimental "signatures" feature that came in for 5.20 (to my understanding).
Turns out, there's a name for it. Multi-methods.
And it turns out there's a module in CPAN already: Class::Multimethods.
And, like so many very strange, very cool modules you don't know you need until you get there, it was written by Damian Conway.
The way I expect I'd handle it, I'd have each test() version handle the input to get what I need as input, then have an internal subroutine handle the munged input. But then again, I'm not sure. My mind is still blown by this, so I don't really know how to use it.
You'll excuse me; it took several years before I felt confident enough to take on the Schwartzian Transform, much less understand and use it, so I'm very happy to get this, even before knowing where or if I'd use it.
(I don't use unicode in the examples, but think that more people should know about the unicode features in newer Perl.)
Turns out, there's a name for it. Multi-methods.
And it turns out there's a module in CPAN already: Class::Multimethods.
And, like so many very strange, very cool modules you don't know you need until you get there, it was written by Damian Conway.
The way I expect I'd handle it, I'd have each test() version handle the input to get what I need as input, then have an internal subroutine handle the munged input. But then again, I'm not sure. My mind is still blown by this, so I don't really know how to use it.
You'll excuse me; it took several years before I felt confident enough to take on the Schwartzian Transform, much less understand and use it, so I'm very happy to get this, even before knowing where or if I'd use it.
(I don't use unicode in the examples, but think that more people should know about the unicode features in newer Perl.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use feature qw'say unicode_eval unicode_strings' ; | |
use Carp ; | |
use Data::Dumper ; | |
use LWP::UserAgent ; | |
use Multi ; | |
say test() ; | |
say test(29) ; | |
say test('fnord') ; | |
say test(29,21) ; | |
say test( { id => 'foo' } ) ; | |
say test( [ 1 .. 20 ] ) ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Multi ; | |
use Class::Multimethods ; | |
use Exporter qw(import) ; | |
our @EXPORT = qw{ | |
test | |
} ; | |
multimethod test => () | |
=> sub { | |
return 0 | |
} ; | |
multimethod test => ( '#' ) | |
=> sub { | |
return 1 | |
} ; | |
multimethod test => ( '$' ) | |
=> sub { | |
return 2 | |
} ; | |
multimethod test => ( '#' , '#' ) | |
=> sub { | |
return 3 | |
} ; | |
multimethod test => ( HASH ) | |
=> sub { | |
return 4 | |
} ; | |
multimethod test => ( ARRAY ) | |
=> sub { | |
return 5 | |
} ; | |
1 ; |
No comments:
Post a Comment