Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: What Are The Rules For Subs And Modules When Fed Wrong Input

by dpuu (Chaplain)
on Jul 21, 2002 at 03:51 UTC ( [id://183753]=note: print w/replies, xml ) Need Help??


in reply to What Are The Rules For Subs And Modules When Fed Wrong Input

Obviously, it depends...

The first thing to say is that, if you are using sub is known at compile time (e.g. its not an object-method), then you should probably use prototypes: then you get the errors, not your users:

sub foo ($$) { print "@_\n"; } foo(1,2,3); % perl foo.pl Too many arguments for main::foo at foo.pl line 2, near "3)" Execution of foo.pl aborted due to compilation errors.
If you want to do run-time checking, then you should consider that it may well be end-users who see the errors, not the person writing the script. In this case, I'd use the following guidelines:
  • If possible, have default values for params not supplied -- then its not an error
  • If you really want to die, then you should provide an error message that tells the user that its not their fault:

    Internal Error: The script you are running has found an error made by its programmer, and regrets that it is unable to continue. Please email (the developer), and include the following information (... stack dump ...). Please accept our appologies

Basically, detailed messages probably won't help the user, so don't confuse them. --Dave.

Replies are listed 'Best First'.
Re: Re: What Are The Rules For Subs And Modules When Fed Wrong Input
by Cody Pendant (Prior) on Jul 21, 2002 at 04:02 UTC
    sub foo ($$) { print "@_\n"; } foo(1,2,3); >>> Too many arguments for main::foo at ...
    Could you please explain that in a little more detail? I'd really appreciate it, as I don't know what's meant by "prototypes".

    But my concern is about what to do if I'm writing a module, not if I'm using a module. Perhaps the real answer is, write good and unambiguous documentation?
    --

    ($_='jjjuuusssttt annootthheer pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;
      When you define a sub, you can tell the compiler what args it expects. In my example, the ($$) means that it must be passed two scalar values. Rather than me going into more detail myself, checkout this tutorial. Another view is given by Tom Christiansen, here. Also, don't forget the Camel book. --Dave.
Re: Re: What Are The Rules For Subs And Modules When Fed Wrong Input
by ktingle (Sexton) on Jul 22, 2002 at 16:26 UTC
    Is there a simple way to print the call stack w parameter values? I could use this a great deal.
      check out Devel::DumpStack --Dave.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 18:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found