Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^4: How do I prototype a function with a varying number of arguments?

by chromatic (Archbishop)
on Jul 30, 2011 at 00:24 UTC ( #917576=note: print w/replies, xml ) Need Help??


in reply to Re^3: How do I prototype a function with a varying number of arguments?
in thread How do I prototype a function with a varying number of arguments?

... how is it, that putting a limitation on the number and type of arguments that can be passed to a function, make it generic and more suitable to use as a standby function?

How do you pass the elements of a two-item array to a function with the $$ prototype?

  • Comment on Re^4: How do I prototype a function with a varying number of arguments?
  • Download Code

Replies are listed 'Best First'.
Re^5: How do I prototype a function with a varying number of arguments?
by JavaFan (Canon) on Jul 30, 2011 at 10:01 UTC
    How do you pass the elements of a two-item array to a function with the $$ prototype?
    sub foo ($$) {...} my @array = (1, 2); &foo(@array);
    ;-)
Re^5: How do I prototype a function with a varying number of arguments?
by koolgirl (Hermit) on Jul 30, 2011 at 02:07 UTC
    oO

    Instinctively, I think maybe this is a trick question, but, nonetheless....like this?

    &sub ($@[0], $@[1])

    I mean, technically a reference to a single array element is a scalar variable, so, that would work then....right?

      I think maybe this is a trick question...

      How do you pass the contents of a two-element array to an unprototyped function which takes two scalar arguments?

      some_func( @two_elements );

      How do you pass the contents of a two-element array to a $$ prototyped function?

      some_func( $two_elements[0], $two_elements[1] );

      That looks less generic to me, as it forces callee details to the caller side.

Re^5: How do I prototype a function with a varying number of arguments?
by koolgirl (Hermit) on Jul 30, 2011 at 02:14 UTC

    Hhmm, so, I'm sitting here looking at your question and my obvious answer, and I think maybe I'm seeing the point you maybe were trying to get me to see.....the $$ prototype isn't really a limitation, it's just a declaration of how many scalar arguments a function takes, like a <cmp> function, for example...or perhaps it's late and I'm thinking a bit too hard on this one.....

    UPDATE:

    OK, so I was thinking too hard...apparently it seems my original notions about prototypes making functions less generic is shared by quite a few of you out there. Well, this goes fantastic with one of my other recent posts, lol, especially related to planetscape's "brain fart" reply, which this last reply of mine most definitely seems to qualify as..*turns red* ;P

      I think maybe I'm seeing the point you maybe were trying to get me to see.....the $$ prototype isn't really a limitation
      Perhaps the point is that the $$ prototype can be a (confusing) limitation. For example:
      use strict; use warnings; sub Fred($$) { my $arg1 = shift; my $arg2 = shift; print "arg1='$arg1'\n"; print "arg2='$arg2'\n"; } my @two_elt_arr = ( 'abc', 'def' ); Fred(@two_elt_arr); # oops, compile error: "Not e +nough arguments" Fred($two_elt_arr[0], $two_elt_arr[1]); # this works
      See Modern Perl, "The Problem with Prototypes", page 242:
      Prototype coercions work in subtle ways, such as enforcing scalar context on incoming arguments ... (examples elided) ... Those aren't even the subtler kinds of confusion you can get from prototypes.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://917576]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2017-10-24 05:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (286 votes). Check out past polls.

    Notices?