Re: Function Prototypes and Array vs. List (Pt. 2)
by demerphq (Chancellor) on Jun 13, 2002 at 15:35 UTC
|
| [reply] |
Re: Function Prototypes and Array vs. List (Pt. 2)
by ariels (Curate) on Jun 13, 2002 at 14:09 UTC
|
But you aren't "convert[ing] or expand[ing] an array into a list"; you're converting a list into a scalar. That's what the prototype means. It might not be what most people would want prototypes to mean, but that' still what the prototype means.
How do you convert a list to a scalar? Easy. Meditate on this bit from perlfunc:
Remember the following important rule: There is no rule
that relates the behavior of an expression in list context
to its behavior in scalar context, or vice versa.
There is no general rule.
You have to read the documentation for whatever it is you're using.
| [reply] |
|
| [reply] [d/l] [select] |
|
create($bob[0])
Case closed.
-- Randal L. Schwartz, Perl hacker
update:
And if you can show me where you "Well, sometimes you've just got to." in actual useful code and not some junky little hypothetical misdesign, I'd be surprised. | [reply] [d/l] [select] |
|
|
|
|
No, your hypothetical create method is simply wrong. It's buggy. It should be (to paraphrase John Cleese) a NON-method. It should be bereft of life and pushing up the daisies.
The only reason to use prototypes is in order to give your functions the same syntax as various Perl builtins. E.g., with prototypes you can write a mymap that will have the same semantics as map. Your ``create'' method isn't doing that; it's using a prototype in order to create a bug.
I could equally well claim that map is broken, because this code
my %hash = map { $_ => X => 1 } qw(a b c d)
does something very strange.
You're right about coding, though: In order to make sure code does not have a bug, it is not enough to check documentation. You must look at the code.
Why are you using a prototype? So far you've shown excellent reasons for you not to use a prototype for this function. ``Doctor, whenever I leave the spoon in the cup and drink my tea, my eye hurts!''
| [reply] [d/l] [select] |
|
Re: Function Prototypes and Array vs. List (Pt. 2)
by Abigail-II (Bishop) on Jun 13, 2002 at 14:35 UTC
|
The question is, is there a simple method to convert or expand an array into a list?
Well, yes! The simplest way possible. As simple as converting
a number to a string. If you want an array to act like a list,
use it in list context!
Abigail
| [reply] |
|
The problem is how adamant Perl is about backpropagating the wantarray = undef "scalar only, arrays will be converted automatically on sight" property of the function prototype. It just keeps going...and going...and going, all the way down the stack. In the example I posted, even reverse is joining the party, and it's a level removed!
I didn't write the function that's prototyped, I just want to give data that happens to be in an array without fussing around too much, like expanding it out the hard way: $n[0],$n[1],...,$n[$#n]
I don't disagree with your remark, I'm just astounded at how stringently prototypes are interpreted.
| [reply] [d/l] [select] |
|
But the reverse call isn't one level removed. The call
to reverse is the first argument and it is being evaluated
in scalar context. In your example you have only one case of using a
literal list in the foo_bar() call, which gives you a prototype
error. In all other cases you are calling foo_bar() with a single
expression that will be evaluated according to the context of the first (or
only in your case) prototype.
| [reply] [d/l] |
|
|
|