in reply to Re: Warning for "unused sub declarations"? in thread Warning for "unused sub declarations"?
If the sub isn't declared or defined before use, then you
can't call it like a builtin - without & and parentheses around the arguments. That's the main disadvantage for me.
chas
Re^3: Warning for "unused sub declarations"?
by thor (Priest) on May 19, 2005 at 23:20 UTC
|
Fair enough. But, now I'm curious. Since Perl doesn't have type checking, what good does declaring the subroutine get you? Why is perl not able to parse the file, say "hey, chas has subroutines foo, bar, and baz, so if I see them referenced, I'll use them"? Just seems odd to me.
thor
Feel the white light, the light within
Be your own disciple, fan the sparks of will
For all of us waiting, your kingdom will come
| [reply] |
|
Reasonable question! I don't know enough about how the parsing/compiling takes place to say. But I suppose it may save some time (i.e. decrease the number of passes) if it is not necessary to look ahead for sub defs. I'll bet someone else around here knows, though. BTW, I usually do like you do and put sub defs at the end (and call with &.) My previous response was just an answer to why someone might declare subs before using; I used to predeclare, but lately it seems more organized to have all the subs defined/declared in just one place (and that outweighs the disadvantages most of the time.)
chas
| [reply] |
|
BTW, I usually do like you do and put sub defs at the end (and call with &.)
Sorry for the repetition, but repetita iuvant. You can put your sub defs wherever you want. But you should never ever call them with & unless you know what you're doing. perlsub will explain you why far better than I could.
| [reply] |
Re^3: Warning for "unused sub declarations"?
by blazar (Canon) on May 20, 2005 at 07:22 UTC
|
If the sub isn't declared or defined before use, then you can't call it like a builtin - without & and parentheses around the arguments. That's the main disadvantage for me.
Huh?!? You're joking? Indeed I do declare my subs to be able to call them without parentheses - fair enough. But it is never stressed enough that in Perl5 (as opposed to Perl4 and previous versions) the &-form of of sub call should never ever be used unless you really need it, i.e. you do know what you're doing. See perlsub.
| [reply] |
|
It seems to me that people are over-reacting here. I don't generally like to call subs with the ampersand because IMO the code is more readable without it, but honestly, if the other poster wants to write code that calls subroutines using ampersands and parens, I don't see how that will really harm him. Yeah, it suppresses prototypes, but so what? Nobody who needs to be pointed at perlsub is using prototypes anyway.
The only really weird wrinkle with the ampersand sigil
is what happens if you also leave off the parens, but the
other poster specifically said ampersand and parens, so he's not likely to run into that.
"In adjectives, with the addition of inflectional endings, a changeable long vowel (Qamets or Tsere) in an open, propretonic syllable will reduce to Vocal Shewa. This type of change occurs when the open, pretonic syllable of the masculine singular adjective becomes propretonic with the addition of inflectional endings."
— Pratico & Van Pelt, BBHG, p68
| [reply] |
|
use strict;
sub foo(\@;@) { shift; print @_, "\n"; };
my @arr1 = ( [1, 2], 2, 3 );
my @arr2 = (1, 2, 3);
foo( @arr1, @arr2 );
&foo( @arr1, @arr2 );
__END__
123
23123
I'll grant you that this example is tremendously cooked, but the issue is real. As you say, the OP might be unlikely to use such prototypes, but the OP is likely to be using the code of others.
| [reply] [d/l] |
|
|
Thanks for the comment; I was aware of the possible pitfalls, but after rereading the docs, I'm rethinking the situation.
chas
| [reply] |
|
In the meanwhile jonadab added an informed comment (Re: Warning for "unused sub declarations"?) as to why it could not be that bad after all - in your case. It is still a bad habit and should be avoided IMHO, especially since it may not do many harm, but it doesn't improve readability nor have any other immediate advantage either.
| [reply] |
|
|