in reply to Overloading inherited class methods
First off, i'm not the hugest fan of named parameters. I agree they are useful when you have a lot of items to pass around, or when you are generating code from a DB or the like, but I find I reuse mostly code that has simple and easy to understand semantics. Usually that means only a few arguments. But sometimes it means being a bit more creative like this
You see what I mean? We can use both positional and named parameters in the same call, so long as you put the positional item first and shift them off before you assign to the %hash. Now we can saysub new { my $class=shift; my $id; if (@_ % 2) { $id=shift; } my %args=@_; #.... }
You can extend this idea to more arguments, but you have to be careful how you do it. For a single argument however it works fine. You can also play other games, for instance checking type. Many subs we commonly use do basic type checking on their arguments to provide a natural feel, but also a fine level of control. File::Find is a good example. Personally I think that a bit of extra work setting up an intuitive interface saves a lot of hassle later.my $foo=Foo->new(100); my $foo=Foo->new(100,%ARGS); my $bar=Bar->new(%ARGS); my $bar=Bar->new();
cheers, :-)
--- demerphq
my friends call me, usually because I'm late....
|
|---|