Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Gratuitous use of Perl Prototypes

by perrin (Chancellor)
on Nov 08, 2004 at 22:49 UTC ( #406232=note: print w/replies, xml ) Need Help??

in reply to Gratuitous use of Perl Prototypes

My first thought any time I see code with prototypes is that the person who wrote it doesn't know Perl very well. There's no reason to use them except to try and do something tricky like your PUSH() example, and I think that's a bad idea because it has hidden behaviors and doesn't work the way other subs do. Better to treat all your subs consistently and use references explicitly when you want them.

Replies are listed 'Best First'.
Re^2: Gratuitous use of Perl Prototypes
by leriksen (Curate) on Nov 09, 2004 at 00:56 UTC
    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;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://406232]
[LanX]: DBI: is there an easy method to get the content of a column as an array
[LanX]: values %{ $DBI->fetchrow_has href('column_name' }
[LanX]: ehm ... fetchall_hashref
[Jenda]: my int @a = ^5_000_000 Coerces the argument to Numeric, and generates a range from 0 up to (but excluding) the argument.
[Jenda]: The apparent design principle of Perl6 operators was "Let's confuse 'em dudes." Whose braindead idea was this particular operator and the three hundred around it?

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2018-07-16 16:49 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (344 votes). Check out past polls.