Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I've made the error of using (). The correct is :manual_error() { }
when calling it asmanual_error { }
and the first definition is active then the program dies with "Too many arguments for main::manual_error "manual_error("hi");
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Too many arguments for subroutine
by Corion (Patriarch) on Oct 20, 2023 at 11:44 UTC | |
See perldiag. Perl can give you more information on its warnings and exceptions if you let it:
And yes, this has to do with specifying a prototype (or subroutine signature) and then using a different number of parameters. | [reply] [d/l] [select] |
Re: Too many arguments for subroutine
by chromatic (Archbishop) on Oct 20, 2023 at 16:12 UTC | |
If you're using a Perl of version 5.20 or newer, you can use subroutine signatures. Perl 5.36.0 removed the experimental tag, so use v5.36; will enable them. See this Effective Perl Programming article about signatures for more details. Improve your skills with Modern Perl: the free book. | [reply] [d/l] |
Re: Too many arguments for subroutine
by eyepopslikeamosquito (Archbishop) on Oct 20, 2023 at 14:34 UTC | |
has this something to do with prototypes? Yes. Thanks for posting, made me realise I didn't have a list of references on this topic yet. So I just created one: Subroutine Prototype References (short summary: Don't use subroutine prototypes) 👁️🍾👍🦟 | [reply] |
by Bod (Parson) on Oct 20, 2023 at 19:50 UTC | |
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! | [reply] |
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] |
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] |
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 |