in reply to Re: Too many arguments for subroutine
in thread Too many arguments for subroutine
short summary: Don't use subroutine prototypes
Well...that begs the question...what are they included in the language?
I never use prototypes because I tried to understand them, failed, and decided to remember they exist so if I ever come across a situation where they seem like they might make life easier, I can go back and try to properly learn how to use them. That situation has occurred yet!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Too many arguments for subroutine
by haukex (Archbishop) on Oct 20, 2023 at 20:42 UTC | |
The one place where Prototypes are useful* is if you want to write a replacement for a built-in function that has its arguments parsed in the same way as the builtin. For example, prototype("CORE::lc") is "_", and let's say we want to write a replacement:
The prototype does three things:
* However:
therefore, they are firmly in the "only use this if you know what you are doing and can explain exactly why" category, or, rephrasing that for non-experts, "Don't use subroutine prototypes"... Update: Switched code from oneliners to a script. | [reply] [d/l] [select] |
|
Re^3: Too many arguments for subroutine
by kcott (Archbishop) on Oct 21, 2023 at 04:44 UTC | |
G'day Bod, [Assumption of typos: first sentence (after quote) s/what are they included/why are they included/; last sentence s/situation has occurred yet/situation has not occurred yet/.] "Well...that begs the question... The only time I use prototypes is when I want a subroutine to be inlined (and then only in rare cases). You can read about inlining in "perlsub: Constant Functions". Reproducing the test examples (a fair way down in that section):
When subroutine signatures are enabled (see ++chromatic's earlier discussion; and also "perlsub: Signatures") there's a conflict with this syntax:
You can resolve this by using the :prototype attribute (see "perlsub: Prototypes"):
But don't also try to use a signature as that will break it again:
Now, I did say at the outset "only in rare cases". That's because, for the most part, the constant pragma does what I want regardless of whether signatures are enabled or not.
In fact, needing an empty prototype to inline a subroutine is so rare, I can't immediately think of an example. Perhaps I'll come up with one later. :-) — Ken | [reply] [d/l] [select] |
|
Re^3: Too many arguments for subroutine
by afoken (Chancellor) on Oct 21, 2023 at 12:46 UTC | |
Don't use subroutine prototypes As usual in Perl: compatibility with legacy code. Prototypes are OLD. They exist in the very first version of Perl 5 found on CPAN (5.002b3, https://metacpan.org/release/LWALL/perl5.002b3/view/pod/perlsub.pod#Prototypes), dated 1996-Feb-03, more than a quater century ago. At that time, prototypes seemed to be useful. And once a feature is included in Perl, it is extremely unlikely to be ever removed again. I thought function prototypes already existed in Perl 4, but I could not find any documentation for them in the 5.001m sources (see Slackware 3.0 sources at http://ftp.gwdg.de/pub/linux/slackware/slackware-3.0/disk1/source/d/perl-5.001/perl5.001m.tar.gz, released in October 1995). Also, no traces of function prototypes in perl 4.036 (Slackware 2.3, http://ftp.gwdg.de/pub/linux/slackware/slackware-2.3/source/d/perl/perl-4.036.tar.gz, released June 1995). Alexander
-- Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-) | [reply] |
by Bod (Parson) on Oct 21, 2023 at 23:57 UTC | |
dated 1996-Feb-03, more than a quater century ago I started using Perl not long after that...if only I'd ventured into The Monastery earlier who knows where my coding would be... | [reply] |