Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: Preferred technique for named subroutine parameters?

by akho (Hermit)
on May 22, 2009 at 20:28 UTC ( #765739=note: print w/ replies, xml ) Need Help??


in reply to Re: Preferred technique for named subroutine parameters?
in thread Preferred technique for named subroutine parameters?

That's because you are doing it wrong. Carp is your friend:

use warnings; use strict; use Carp; func(one => 'uno', two => 'dos', three => ); sub func { croak "wrong number of arguments for func(); has to be even" if sc +alar(@_) % 2; my %args = @_; print "func: one translates to $args{one} \n"; }
prints
wrong number of arguments for func(); has to be even at a.pl line 8 main::func('one', 'uno', 'two', 'dos', 'three') called at a.pl lin +e 5


Comment on Re^2: Preferred technique for named subroutine parameters?
Select or Download Code
Re^3: Preferred technique for named subroutine parameters?
by AnomalousMonk (Abbot) on May 22, 2009 at 20:43 UTC
    But then you have to test,  croak and  carp all over the place. This mitigates against 'clean' code (for some definition of 'clean').

    With the anonymous hash approach, don't you get pertinent warnings 'for free'?

      You have to test that you have the correct number of arguments anyway (in most cases, at least).

      Also: specific error messages are way better than generic ones.

        You have to test that you have the correct number of arguments anyway (in most cases, at least).
        But this is really about parameter validation: required versus optional parameters, acceptable ranges of parameter values, misspelled parameter names, etc. Depending on the situation, validation can be quite simple (or non-existent!) or quite involved. In any case, it is something that can only be handled 'within the function'.

        But again, to me, the question of whether each named parameter has some value associated with it is a question that can be (and is best) handled by default, right there and then at the function call.

        Also: specific error messages are way better than generic ones.
        I tend to agree, but this becomes a matter of individual taste. For myself, a message like  Odd number of elements in anonymous hash at 765727_1.pl line 38. is both sufficiently specific and sufficiently proximate to the point of occurrence – especially if the program died at line 38!
Re^3: Preferred technique for named subroutine parameters?
by tilly (Archbishop) on May 22, 2009 at 20:51 UTC
    I happen to like Carp so much that I rewrote it to make it better, but try as it might it cannot always properly assign blame. In this case it reaches all of the way up the call stack, gives up, and gives a complete stack backtrace. If your code appeared in a module, you'd have blamed the user with a very confusing message.

    By contrast the anonymous hash always blames the exact right line of code.

    While I have preferred the flat list approach, I'm going to have to rethink that preference based on this point.

      Quite the opposite: moving func to a separate module makes Carp work.

      And it gives an error that is more specific (for example, it includes the name of the function) than the generic message returned by the anonymous hash.

      Besides, it actually dies, not prints something and then goes on as if nothing happened.

        use strict; Foo::bar(); package Foo; use Carp qw(croak); sub bar { baz(one => 'uno', two => 'dos', three => ); } sub baz { croak "wrong number of arguments for baz(); has to be even" if sca +lar(@_) % 2; my %args = @_; print "func: one translates to $args{one} \n"; } __END__ wrong number of arguments for baz(); has to be even at - line 2
        Seems like more typing for a worse result to me. Oh, and if you rename your function, you have to synchronize the function with the error message. A little laziness on that part could lead to all sorts of puzzlement.
        Besides, it actually dies, not prints something and then goes on as if nothing happened.
        Ah, but if you  use warnings FATAL => 'all'; , then everything comes to a screeching halt! Again, a very broad benefit for what seems a minimal investment.

        (Actually, I'm starting to really like the practice of making all warnings fatal.)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2014-11-27 13:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (184 votes), past polls