Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
If you need particular types in your parameters, another way to achieve this (and quite possibly more robust than prototype as the clinical dissection above shows) is to use the "program by contract" paradigm.

So if you need the first parameter to be an array ref, put in code to test this precondition -

sub PUSH { my ($aref, @new) = @_; carp "first parameter must be an array reference" unless ref $aref e +q 'ARRAY'; ... }

Even better, use Params::Validate

If your emulating a builtin like push, using the same interface has advantages (in this case positional parameters), but I prefer using named parameters as much as possible - it also can cut out a number of errors if you choose appropriate parameter names e.g.

sub NAMED_PUSH { my(%args) = @_; carp "dest_aref parmeter must be an array reference" unless ref $arg +s{dest_aref} eq 'ARRAY'; ... }

Your POD for this sub (dont give me that puzzled look !!!) should document what keys are expected, and the code checks you have keep your part of the contract.

use brain;


In reply to Re^2: Gratuitous use of Perl Prototypes by leriksen
in thread Gratuitous use of Perl Prototypes by grantm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found