Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

how does splice do it?

by LanceDeeply (Chaplain)
on Jan 09, 2003 at 22:51 UTC ( [id://225672]=perlquestion: print w/replies, xml ) Need Help??

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?

Replies are listed 'Best First'.
Re: how does splice do it?
by Zaxo (Archbishop) on Jan 09, 2003 at 23:01 UTC

    This is what prototypes are properly used for.

    $ 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

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:
    1. 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.)
    2. 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.
      I'm not sure if they'll "correct" it , by your definition of "correct". Perl 6 will support full, C-style prototypes, with types and names declared in the subroutine declaration. (It'll still use @_ if you don't declare a prototype.) But by default, arrays won't flatten if there's a prototype on the subroutine; you'll have to use the unary * (flattening) operator either in the declaration or in the call.

      I have a Perl6::Parameters module on the CPAN that implements a (pathetically small and out-of-date) subset of the Perl 6 behaviors.

      =cut
      --Brent Dax
      There is no sig.

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

      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?

        ...and why?

        Prototypes don't work for method calls. So, they are generally useless for an alternative to passing a reference.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://225672]
Approved by mojotoad
Front-paged by diotalevi
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-23 07:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found