Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: My coding guidelines

by cLive ;-) (Parson)
on Nov 25, 2002 at 18:56 UTC ( #215700=note: print w/replies, xml ) Need Help??


in reply to My coding guidelines

Re point 15:

"Perl doesn't compile check the types of or even the number of arguments"

Am I missing something, or isn't that what prototypes are for?

.02

cLive ;-)

Replies are listed 'Best First'.
Re: Re: My coding guidelines
by thelenm (Vicar) on Nov 25, 2002 at 19:06 UTC
Re: Re: My coding guidelines
by jryan (Vicar) on Nov 25, 2002 at 19:18 UTC
    You are missing something, here are two examples:
    # prototypes only check arg format # i.e. (scalar/array/hash/code/glob and number of args) package Sample; sub stringify () { return ($_[0]->{data}) x $_[1] } package main; sub print_string($) { print $_[0]->stringify(1) } my $obj = bless ( {data=>"foo\n"}, 'Sample'); print_string($obj); # fine print_string(5); # uh oh... basic numbers don't have a stringify me +thod, # yet the arg passed the prototype since 5 is a sc +alar.
    And also:
    # methods don't check prototypes... at all. sub print_string_bad($) { print $_[0]->stringify() # Will pass the prototype, yet break since $_ +[1] will be # undef in stringify } print_string_bad($obj);

    Hence the reason for the general distaste for prototypes among the perl community. For most cases, they're pointless. They're only there so that you can call your own subs like perl-builtins.

Re: My coding guidelines
by Abigail-II (Bishop) on Nov 26, 2002 at 09:49 UTC
    You can't use prototypes to have the compiler check whether an argument is an integer, or an object of a certain type. Prototypes are less useful than you might think at first, and they are sometimes bloody nasty. The following doesn't do what you want:
    sub foo ($$) {....} my @bar = (1, 2); foo @bar;
    &foo isn't called with two arguments - even while @bar has two elements. Remove the prototype, and it will work as expected.

    Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://215700]
help
Chatterbox?
choroba waves
[Mj1234]: Hello Monks, I have installed Active perl 5.16 and 5.14 but I dont find Devel::GDB package when I try to install through ppm
[Discipulus]: i'v installed ubuntu 16.04lts and i'm quite disappointed: is deadly simmilar to new win osses where you cannot find even simple things...
[Corion]: Hi Discipulus ;)
[Corion]: Mj1234: Maybe Ubuntu didn't package Devel::GDB? In that case, consider using local::lib and install the module from CPAN
[Corion]: But a quick Google search turns up this page, which seems to suggest that it should be packaged
[Discipulus]: iirc active perl has repositories only for last 2 o 3 major release of perl Mj1234. I strongly suggest portable version of strawberry perl and use cpan client to install modules

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2016-12-07 08:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (125 votes). Check out past polls.