LanceDeeply has asked for the wisdom of the Perl Monks concerning the following question:
for: splice @ar, 2, 1;
how does splice modify the contents of @ar?
whenever i write a sub to modify an array, i have to pass the array by reference. how does splice do it?
Re: how does splice do it?
by Zaxo (Archbishop) on Jan 09, 2003 at 23:01 UTC
|
$ perl -e'print prototype( "CORE::splice"),$/'
\@;$;$@
The \@ prototype requires a variable with the @ sigil and implicitly converts it to a reference. If you define sub mysplice (\@;$;$@) { ...}, you will obtain splice-like behavior with respect to the arguments.
After Compline, Zaxo | [reply] [d/l] [select] |
Re: how does splice do it?
by pg (Canon) on Jan 10, 2003 at 16:14 UTC
|
The answer to this question is now clear, however I am worried to hear that some monks were thinking this is stylish, and want to follow it in the future.
I would say NO to this style:
- First, put it plain, you didn't gain anything by doing this. Performance would be the same, if it is not worse. Some people may think it looks nice, okay, maybe, but you lose the correctness of your code. (I am not saying the way this syntax being used is wrong, but more saying it is wrong for Perl to support this kind of hacking.)
- Perl does have a problem with HACKING, and this is just one example. The hacking approach makes Perl more modern without costs too much effort, but remember that those are not part of Perl's foundation.
For this particular question we are facing now, let me borrow this slogan from GUI design, WHAT YOU SEE IS WHAT YOU GET. The syntax of a programming language should follow the same rule, and one syntax-look-alike should not have two meanings.
Prototype is supposed to be a guardian, who stops parameter type errors, but now this good will is VIOLATED AND BROKEN. The extra effort of implicit conversion is a little interesting trick, but that's it.
I am a big fan of Perl, but definitely not a big fan of those hackings.
To be frank, I would expect this to be one of the first things they want to CORRECT in Perl 6. | [reply] |
|
| [reply] |
Re: how does splice do it?
by submersible_toaster (Chaplain) on Jan 09, 2003 at 23:09 UTC
|
++ LanceDeeply for sensible question, ++ Zaxo's consise answer.
Methinks many places I am passing references I could be using prototypes.
Thankyou.
Update: OK , I am justly chastised , just because this feature exists
is no carte-blanche reason to be using it.
I can't believe it's not psellchecked | [reply] |
|
Do be careful with using prototypes vs. passing references. IMHO, code is much more readable and maintainable if you make a habit of passing references. In this manner, you don't have to look at the subroutine declaration to see what arguments it expects.
It seems to me that some core perl functions, such as splice, push, pop, shift and unshift use prototypes because the name of the functions as well as the way they are called read easier in the english language. "Push onto this array the following list" rolls off the tongue better than "Call the push() function with a reference and a list to add to it". push \@array, @list; just doesn't look right. Whenever I see a call to a subroutine with an array as the first argument, I generally assume that a copy of an array is being passed to the subroutine and that each item in the array will fill @_, not that it will all be within $_[0].
It all comes down to a matter of preference, but I'd like to think that more people stick to passing references than creating a prototype for every subroutine they write. I'd be interested in hearing other opinions on this matter. Which method of passing arguments do you use more frequently and why?
| [reply] [d/l] [select] |
|
| [reply] |
|
|
|
|