in reply to What's the better Perl style? Define parameters or not?

Apparently, you should throw out the Perl for C Programmers book. If that's the advice they're giving you, I have to wonder about the rest of the book.

As ikegami said, it's called 'prototypes'. And they're evil like goto in C. If you don't know why it's evil, and what the exceptions are, don't use it. There are valid uses for it, but as a general rule, don't use it in production code. (Trying it out in play-code is fine, as that's one way to learn why it's evil, and what its uses really are. And positing questions here on said play code is fine, too.)

These have nothing to do with prototypes in C. That is, int foo(char const* bar); ensures that the parameter passed in to foo is a pointer to constant characters (or convertible to that type, such as a char*, though the function will then treat the pointer as if it pointed to a constant). Perl doesn't have anything like that. sub foo($); says that the first parameter passed to foo is evaluated in scalar context. That's almost never what you want. And it kills a number of really useful facets of Perl that C/C++ just can't match, such as:

sub exp($$) { # perform exponention. } my @values = ( [ 3, 3 ], # cube of 3 [ 81, 0.5 ], # square root of 81 # ... ); for my $params (@values) { printf "exp(%s,%s) = %s", @$params, exp(@$params); }
The prototype will cause this to die. To fix it, simply remove the prototype. Or call it as "&exp(@$params)" or "exp($params->[0], $params->[1])", but those are both far uglier.

Replies are listed 'Best First'.
Re^2: What's the better Perl style? Define parameters or not?
by ikegami (Patriarch) on Jan 05, 2010 at 06:01 UTC

    And they're evil like goto in C.

    goto is very useful in C.

    TYPE* foo(...) { ... if (...) goto Error; ... if (...) goto Error; ... if (...) goto Error; ... return p; Error: ... cleanup acquired resources ... return NULL; }

    In Perl, not so much.

      Yes, and prototypes are the same as that. They're useful in narrow circumstances where you know that you're not violating the principles that make them evil. And you can consider the pronouns in the previous sentence to be talking about either goto in C/C++ or prototypes in Perl. Personally, I've only used prototypes when trying to duplicate map or grep-like syntax (first parameter is a code block), but, even then, I usually avoid them. I'm not sure you could get try { BLOCK } catch { BLOCK } kind of syntax without prototypes, but I wouldn't want those new to perl to be writing that code as they probably wouldn't understand the details of prototypes (or much else around the desired syntax).

        I'm not sure you could get try { BLOCK } catch { BLOCK } kind of syntax without prototypes,
        You can't1. You'd have to write it as:
        try sub {BLOCK}, catch sub {BLOCK}
        I wouldn't want those new to perl to be writing that code as they probably wouldn't understand the details of prototypes
        That's quite an elitist remark which I do not agree with.

        1 Well, you can if you leave Perlland, and enter the world of source filters. Or perhaps the world Devel::Declare or some other universe that messes with internals.