Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Function Prototypes and Array vs. List (Pt. 2)

by Abigail-II (Bishop)
on Jun 13, 2002 at 16:01 UTC ( [id://174252]=note: print w/replies, xml ) Need Help??


in reply to •Re: Re^2: Function Prototypes and Array vs. List (Pt. 2)
in thread Function Prototypes and Array vs. List (Pt. 2)

I'd say the specification of create is incomplete. Prototyping does act surprisingly because it goes against the list flattening you'd expect from unprototyped functions. Not specifying that a function is prototyped, or not saying that the first argument will be evaluated in scalar context means, IMO, that the description is incomplete (and hence wrong).

I only use prototypes if there are clear benefits (\@ and & prototypes, sometimes a prototype with a single $ (which changes the way perl compiles your Perl)). But often it's too much of a nuisance.

I also don't agree with your reasoning that with such a simple description of create you should use $array[0] Take a look at the description of POSIX::strftime. With your reasoning, the proper way of calling it would be:

use POSIX; my @chunks = localtime; print strftime "some format", $chunks [0], $chunks [1], $chunks [2 +], $chunks [3], $chunks [4], $chunks [5 +], $chunks [6], $chunks [7], $chunks [8 +];
instead of
print strftime "some format", localtime;

strftime could have been prototyped as $$$$$$$;$$$; the description doesn't say it's not, and spells out the arguments for strftime.

Abigail

Replies are listed 'Best First'.
Re^N+1: Function Prototypes and Array vs. List (Pt. 2)
by tadman (Prior) on Jun 14, 2002 at 04:46 UTC
    The cause of all this fuss was use of a function in an existing library that is mostly, for some reason, prototyped. This is real code, not some "hypothetical misdesign".

    I hardly ever prototype, only when I have to, such as those examples you mention. I've used prototypes before, and they caused all sorts of wierdness, so I stopped. This might have been when they were still considered neat and not inherently dangerous.

    Because of this, I've come to rely on doing things like chaining functions together, not unlike your example, or saving repeated parameters in arrays. I don't suggest that you should expand your arrays into lists just because. Not at all. What I'm saying is that if the function is prototyped, then you have no choice, which is another way of saying "sometimes you just have to". That's not my reasoning, that's reality! If you don't use prototypes, then this isn't an issue, and that's probably the best course of action. If you do, then you're going to have to be prepared for the consequences, so you'd better have a really good reason.

    Although it probably wasn't clear, the whole point of the original post was to validate the theory that, if one can't deprototype the target function, then the consequences are that you really have to do all this extra work to use that function properly. I was hoping for the programmatic equivalent of a rubber glove that could be used to idiomatically insulate code A from prototyped code B, but no such technique seems to be available. Not that I was depending on this, of course.

    This is yet another reason why the widespread use of prototypes is not a great idea. In Perl 6, things look like they will be much smoother.
      if one can't deprototype the target function...

      If all you want to do is disable the prototype, then calling the function with & will do it --- beware that @_ is passed implicitly if you leave off the parens, and you'll have to adjust your argument passing for more complex prototypes (such as: (&@) or (\@)) to ensure the function gets what it is expecting.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://174252]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-25 12:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found