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:
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.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); }
|
|---|
| 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 | |
by Tanktalus (Canon) on Jan 05, 2010 at 15:23 UTC | |
by JavaFan (Canon) on Jan 05, 2010 at 15:46 UTC |