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.


In reply to Re: What's the better Perl style? Define parameters or not? by Tanktalus
in thread What's the better Perl style? Define parameters or not? by pureHeart

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.