this is annoying

M

Mr P

shift split /\s+/, @$tx;

Type of arg 1 to shift must be array (not split) at
/production/fe/users/feot/acs/ACS_Spex.pm line 5, near "$tx )"

shift ( split /\s+/, @$tx );
(same error)

It doesn't make sense that I can do:

@array = arrayOp args

but I can't do

@array = arrayOp1 (arrayOp2 args)


**********************************************

Larry you almost always make things work as expected, but for
perlfunction "chains" like

op1 op2 op3 ... opn arg

everything is almost always busted. This is "the stuff" of clean,
concise syntax (chains) and they don't often work in Perl.

scalarOp (stuff)
should always try its best to turn "stuff" into a scalar
(precedence-rules followed of course)

Likewise,
arrayOp (stuff)

should always try its best to turn "stuff" into an array, coercing
lists if necessary

and of course likewise for hashes

This is particularly true when using parens around stuff- can't make it
much clearer than that to the parser!

Larry I've heard you say personally that your intended design on Perl
was that "things would always always work as you'd expect". That's true
for almost every simple case like

operator args

and not true for almost every compound case.

Get your knives out Uri et al, but this is annoying, depsite your
defense that Perl design is "always right", and comp scientsts opposing
views are "always wrong". (I've read myriad responses to that effect).


I'll do it in individual lines, whatever..

Rant --
 
P

Paul Lalli

Mr said:
shift split /\s+/, @$tx;

Type of arg 1 to shift must be array (not split) at
/production/fe/users/feot/acs/ACS_Spex.pm line 5, near "$tx )"

shift ( split /\s+/, @$tx );
(same error)

Ignoring for a moment your case that shift should be able to work on
lists, I don't have any idea what you think that should be doing.
split takes an expression and returns a list. You're giving it an
array. What is it supposed to be splitting?
It doesn't make sense that I can do:

@array = arrayOp args

but I can't do

@array = arrayOp1 (arrayOp2 args)

It makes perfect sense, actually. You seem to either not understand or
not like that shift does two separate things. It both returns the
first argument of the array *AND MODIFIES THAT ARRAY*. It is not
possible to modify a list. That's one of the key differences between a
list and an array. Please see: perldoc -q difference

Now, if you want to make the case that shift() should be able to detect
whether its argument is an array or a list, and not do this second
feature of modification if it's just a list, that's fine. You'll
probably even get a lot of agreement there. But to say that it
"doesn't make sense" for shift() to work the way it does is just
pointless.

Have you considered trying to write a wrapper subroutine that will do
what you want? If passed an array (or possibly necessarily an array
reference?), remove and return the first element, but if passed a list,
just return the first element.
Get your knives out Uri et al, but this is annoying, depsite your
defense that Perl design is "always right", and comp scientsts opposing
views are "always wrong". (I've read myriad responses to that effect).

Now what the hell is the point of this? Are you looking for solutions,
to improve Perl and the experience of Perl programmers? Or are you
just looking to whine bitch moan complain and insult?

Paul Lalli
 
A

anno4000

Mr P said:
shift split /\s+/, @$tx;

Type of arg 1 to shift must be array (not split) at
/production/fe/users/feot/acs/ACS_Spex.pm line 5, near "$tx )"

shift ( split /\s+/, @$tx );
(same error)

As Paul Lalli has noted in his excellent reply, the second argument
of split() should be a string, not an array. I'll assume $str instead
of @$tx.

Even then, what do you expect your code to do? You do nothing with
the return value of shift(), so that doesn't appear to be the point.
But even if Perl turned the list returned by split into an array
to suit you, the array would not be accessible after the statement
is done, so that doesn't seem to be the point either. So what is
it?
It doesn't make sense that I can do:

@array = arrayOp args

but I can't do

@array = arrayOp1 (arrayOp2 args)

How does this correspond to your code? Specifically, what are
arrayOp1 and arrayOp2? Is arrayOp1 shift() and arrayOp2 split()?
In that case, what is an array operator? Something like shift()
that operates on an array (but returns a single scalar)? Or something
like split() that works on one to three scalar arguments and returns
something array-like (a list)? Explain your terminology.

[...]
should always try its best to turn "stuff" into an array, coercing
lists if necessary

Well, Perl as it is doesn't do that for you. It does very little of
its DWIM with behind-the-scene processing. (I've heard mumblings
like "...if we keep the array/list distinction" from the Perl 6 corner,
so there my be hope.)

You can do it yourself however:

shift @{ [ split ' ', $str] };

What would be the advantage if Perl did that automatically? The
anonymous array (which is indeed decapitated by shift) isn't accessible.
The return value is easier and more efficiently captured by

my ( $x) = split ' ', $str;

Anno
 
P

Paul Lalli

Michele said:
As Paul Lalli has noted in his excellent reply, the second argument
of split() should be a string, not an array. I'll assume $str instead
of @$tx.

And I had thought that if e.g. $tx=['foo',2], then it would be awkward
but work. However a quick experiment seems to imply it doesn't...

Nope. split() is prototyped to take 1, 2, or 3 scalars. Therefore, an
array passed as the EXPR is treated in scalar context:

perl -le'
my @letters = ("a".."z");
my @foo = split //, @letters;
print "@foo";
'
2 6

Paul Lalli
 
A

anno4000

Michele Dondi said:
On 3 Oct 2006 06:24:55 -0700, "Mr P" <[email protected]> wrote:
But you're aware that

: split /PATTERN/,EXPR,LIMIT
: split /PATTERN/,EXPR
: split /PATTERN/
: split Splits the string EXPR into a list of strings and returns that

Aren't you? So unless @$tx contains an "EXPR,LIMIT" couple you're
doing something strange.

That's a valiant attempt to justify MisterPerl's broken code, but even
that won't work. The second argument of split() is evaluated in scalar
context. An array in that position is equivalent to the number of its
elements. It will split an unsigned integer, which will rarely make
sense.

Anno
 
T

Tad McClellan

Mr P said:
shift split /\s+/, @$tx;

Type of arg 1 to shift must be array (not split) at
/production/fe/users/feot/acs/ACS_Spex.pm line 5, near "$tx )"


Are you going to continue to post this annually until you get an
answer that you like?

Larry you almost always make things work as expected,
Larry I've heard you say personally that your intended design on Perl
was that "things would always always work as you'd expect".


Larry has not participated here for many years. He is not seeing
your sage advice.

Even if you repeat it here every 12 months, he still won't see it.

If you post your patch to p5p he might see it...
 
T

Tad McClellan

Paul Lalli said:
Mr P wrote:

Now what the hell is the point of this?


MrP> There will probably be a plethora of criticisms about this response.
MrP> Don't be put off by them, there are a few folks here who really want to
MrP> assist others. Just figure out who they are and screen the rest. There
MrP> are some great people here but you need to pay attention to who are
MrP> here just to criticize.

in Message-ID: <[email protected]>

Which is exceedingly rich, considering the source...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top