In a message dated 3/22/2008 10:43:42 A.M. Eastern Standard Time,  
[EMAIL PROTECTED] writes:
 
> Hello All,
> 
> After reading a lot on the subject, I'm  guessing that it's not possible to 
> do what I want to do, but anyway I  wanted to ask you about and hopefuly be 
> wrong. :-)
> 
> I  want to write a sub so I can use it like this:
> 
>  --------------------------------
> mysub (a => 1, b => 2, c => 3)  {
>    # ...CODE...
> };
>  --------------------------------
> 
> But, I haven't found any valid  prototype combination to make that work. 
> After reading the perlsub  documentation, at first I thought I would be 
able 
> to do so like  this:
> 
> --------------------------------
> sub mysyb  (\%&) {
>    # ...CODE...
> }
>  --------------------------------
> 
> But, of course, I was wrong.  Taking out the backslash is also wrong since 
> the % will eat anything  after it.
> 
> 
> Please let me know if any of you know the  way to correctly construct such 
> prototype.
> 
> Thanks in  advance.
> 
> 
> Cheers, :-)
> 
> Paco
 
 
you are right that you cannot do what want to do with prototypes (unless  you 
use perl 6), 
because this is not what perl 5 prototypes are for.   
 
however, you can do something similar (i.e., named,  defaulted parameters) as 
follows:   
 
sub func {
 
    my %args = (
        param1 => 1,  #  parameter and its default value
        param2 => 2,
        param3 => 3,
        defined $_[0] ? %{$_[0]} :  (),  # passed parameters, if any
        );
 
    return $args{param1} * $args{param2} +  $args{param3};
 
    }
 
my $x = func({ param3 => 33,  param1 => 11, });
 
 
hth -- bill walters   


_______________________________________________
ActivePerl mailing  list
[email protected]
To unsubscribe:  http://listserv.ActiveState.com/mailman/mysubs







**************Create a Home Theater Like the Pros. Watch the video on AOL 
Home.      
(http://home.aol.com/diy/home-improvement-eric-stromer?video=15?ncid=aolhom00030000000001)
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to