I don't know how to exactly recreate the magic that surrounds grep, but at least it's somewhat possible by taking some magic from MeowChows treemap node :
#!/usr/bin/perl -w use strict; sub on { my($f1,$f2,$l)=@_; my $parmtype = ref $f1; print "f1: $parmtype\n"; if ($parmtype eq 'CODE') { local $_=$$l; if (&$f1) { local *_=$l; &$f2($l); } }; } my $foo="abba is foo"; on sub{ /foo/ }, sub { tr/ab/ba/; }, \$foo; print "$foo\n"; on "bla", sub { tr/ab/ba/; }, \$foo;
As you see, it's still far from perfect, as you'll have to use the sub keyword for your code parameters, but at least your subroutine on can now determine through the $type parameter, whether CODE or something else was passed in.
In reply to Re: Polymorphic prototypes? (creating new syntax)
by Corion
in thread Polymorphic prototypes? (creating new syntax)
by kaatunut
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |