Pathologically Eclectic Rubbish Lister | |
PerlMonks |
Re: Default subroutine parametersby stephen (Priest) |
on Apr 22, 2002 at 18:52 UTC ( [id://161126]=note: print w/replies, xml ) | Need Help?? |
The "defined" method is the best way, but there's a way to have your cake and eat it too, for a price... just define a sub that processes your arguments, filling in as necessary. Like so: Then, in the rest of your code, you can just run your subroutines through _fd(), like so:
Then, test_default('luke') prints "Hello my name is luke", while test_default() prints "Hello my name is ben". There's some overhead to calling subroutines and doing list processing all the time, but you can use it most of the time. Update: After thinking about this for a while, I figured out a way to use theDamian's Attribute::Handlers and a small amount of symbol-table work to give Perl a default attribute: Attribute::Default.
stephen
In Section
Seekers of Perl Wisdom
|
|