in reply to Your named arguments

Personally, I never really cared about fancy prototype handling. I just:

my ($this, $a, $b) = @_;

in my code. When doing named params, I will normally do something like %h and then immediately convert then into specific vars.

My reason for posting was I saw your example:

sub convert (:$from, :$to, :$thing) { ... }

It occurred to me that it would be rather easy to support such a thing WIHTOUT sourcefilters thanks to the prototype function.

There are dozens of potential impls. using prototype and I will leave that as an excersize to the readers. But, I don't think I have ever seen a module that does that without source filters. Then again, I haven't looked very hard.

Update: Working on an example of said package.

Update: Here is a quicky that I through together. I have to go home, so I can't spend too much time on this, but I can see why no one has done this before. The code I wrote is:

package Signatures; sub import { no warnings; no strict; my $package = caller; for (keys %{ $package . '::' }) { next unless exists &{ $package . "::$_" }; my $sub = \ &{ $package . "::$_" }; next unless my $proto = prototype $sub; *{ $package . "::$_" } = eval qq { sub { package $package; local ($proto) = \@_; \$sub->() } } } }

Before you kill me, please understand that this was written in 10 minutes and is not intended to be used by anyone. That is why there is no "use strict" and "use warnings", etc. :-) There were several major problems I encountered.

Anyway, here is the code I was able to run. It does work, but I didn't really test this mess.

#!perl -l foo("aaaa", "bbb"); sub foo ($a, $b) { print $a; print $b } use Signatures;

Well, it was an interesting idea, but I don't know if it can really go anywhere. I'll think more about it tonight.

Ted Young

($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)

Replies are listed 'Best First'.
Re^2: Your named arguments
by Jenda (Abbot) on Nov 08, 2005 at 17:51 UTC

    It's not hard to modify it so that the location of the use statement doesn't matter:

    package Signatures; my @packages; sub import { my $package = caller; push @packages, $package; } INIT { for my $package (@packages) { no warnings; no strict; for (keys %{ $package . '::' }) { next unless exists &{ $package . "::$_" }; my $sub = \&{ $package . "::$_" }; next unless my $proto = prototype $sub; eval qq { package $package; sub $_ { local ($proto) = \@_; \$sub->() } } } } } 1;
    It still seems like the function using the prototype has to be defined AFTER any calls to it though. And modules using the Signatures have to be used after all calls to the functions in them. Otherwise I get "Malformed prototype for foo: $a,$b at C:\temp\Proto\test.pl line 6." Not sure if there is any way around that. And I don't see any way to lexicalize the parameters and make strict happy.

    Jenda
    XML sucks. Badly. SOAP on the other hand is the most powerfull vacuum pump ever invented.