in reply to Subroutine Prototyping/Subroutine Argument Parsing
A purely personal view, but I think the main problem is that you are over specifying the subroutine.
The 'obvious' way (IMO...YMMV...yada yada yada:), to use this routine, is as a function. Ie. In either of the following ways:
I cannot envisage the utility if calling it as a subroutine.
masked_input( \my $password, $mask );
If the mask argument is present, simple use substr( $mask, 0, 1 ) as your mask character and ignore anything extra.
I'm also not adverse to using perl's prototypes when I feel that they benefit my code, but in this case, I don't see the benefit. If you haven't already read FMTEYEWtKaPiP, then I highly recommend it. If you still want to use a prototype for this after doing so, then I'd probably suggest a simple (;$) at most, but even that is overkill I think.
Your major problem/concern seems to be that you want to allow for multiple, omitable arguments. As I already said, I don't think this case warrents it, but you have two (probably more, but these come to mind) options, if you don't use a prototype,.
If you wish to specify the ordering of the arguments, and allow the user to supply the second argument whilst omitting the first, he can do so by using undef
eg. my $pw = masked_input undef, '#';.
The second option would be to use named parameters:
masked_input( ref => \my $pw, mask => '#' ); my $pw = masked_input( mask => '@' ); my $pw = masked_input();
Again, I think this is overkill for this application, but for some purposes, this can be a very useful way to do things.
|
|---|