Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Just another Perl shrine
 
PerlMonks  

How to make script die with "Not enough args" on compilation step if args are passed as a hash ref? See inside

by Doctrin (Beadle)
on Jun 21, 2013 at 08:05 UTC ( #1040106=perlquestion: print w/ replies, xml ) Need Help??
Doctrin has asked for the wisdom of the Perl Monks concerning the following question:

Hello dear Monks. If i do:
sub MySub($$;$) { my ($a,$b,$c) = @_; print "$a $b $c"; return 1; } ... MySub(1);
script would die on COMPILATION step. How can I make a completely same thing with arguements passed as a hash ref? Like that:
sub MySub($args) { ... #and if I do not pass, say, $args->{my_obligatory_arg} whole thing die +s on COMPILATION step }
Thanks in advance

Comment on How to make script die with "Not enough args" on compilation step if args are passed as a hash ref? See inside
Select or Download Code
Re: How to make script die with "Not enough args" on compilation step if args are passed as a hash ref? See inside
by choroba (Abbot) on Jun 21, 2013 at 08:20 UTC
    What should the compilation step do with a code like this?
    my $args = {}; $args->{my_obligatory_arg} = 'present' if .5 < rand 1; my_sub($args);
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Schrödinger's cat!

      Oh... It looks like I cannot do what I need... Thanks!
Re: How to make script die with "Not enough args" on compilation step if args are passed as a hash ref? See inside
by Anonymous Monk on Jun 21, 2013 at 08:21 UTC
Re: How to make script die with "Not enough args" on compilation step if args are passed as a hash ref? See inside
by Anonymous Monk on Jun 21, 2013 at 12:16 UTC
    die "arg $_ not found" unless exists($hash->{$_})
    for (qw/apple pear peach/)
      Do you think that not using the <code> tags will make the code run in the compilation phase?
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: How to make script die with "Not enough args" on compilation step if args are passed as a hash ref? See inside
by Eily (Hermit) on Jun 21, 2013 at 16:06 UTC

    I'll just go and assume that your arguments are known at compile time because ... well ... your question doesn't make much sense otherwise. If this checking is really that important to you, you could always try source filtering. With Filter::Simple you could try to find every calls to that function (and I'm not talking about special cases like calling from a sub reference, because there's no prototype checking there either) and check the parameters.

    If you call it only like MySub( {key => value, otherKey => value, ... } ) you could do with a regex. And if you call it like MySub(CanYieldAHashRefAtCompileTime); You could use your filter to get the parameters as a string, eval it (which would give you a hashref), make your verification, and even replace the string by its "value" by using Data::Dumper. That probably is a bad idea if your data contains bless references (instances) or tied variables.

    So you could do it, and that wouldn't be that difficult provided you read the documentations carefully, but you should still ask yourself if it really is worth the trouble and added complexity to your program.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1040106]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2014-04-16 04:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (413 votes), past polls