why can't I hang a "shift" on the front of this statement

S

Sara

Perl is unhappy with this line:

shift my @id = split /ID\s+([^\n]+)\n/, $$_;

Use of implicit split to @_ is deprecated at ./SSMCP10Conditioner.pl
line 283.
Can't modify shift in scalar assignment at ./SSMCP10Conditioner.pl
line 283, near "$_;"
Execution of ./SSMCP10Conditioner.pl aborted due to compilation
errors.

but it works fine without the leading "shift". Of course I can do the
shift on the next line, but I don't understand why this syntax isn't
OK. shift takes an array as an arg, I gave it an array, but its still
not happy? And why is it assuming I'm trying to do anything implicitly
with @_ ?

Unless @id is "undef" when shift executes, but I'd anticipate a
different error message in that case?

And if this is a matter of mucking up this nice clean syntax with
parens, I hope the P6 Porters are reconsidering the priority order. If
that's the answer I'll just go with the next line shift..


TY,
Gx
 
P

Paul Lalli

Perl is unhappy with this line:

shift my @id = split /ID\s+([^\n]+)\n/, $$_;

Use of implicit split to @_ is deprecated at ./SSMCP10Conditioner.pl
line 283.
Can't modify shift in scalar assignment at ./SSMCP10Conditioner.pl
line 283, near "$_;"
Execution of ./SSMCP10Conditioner.pl aborted due to compilation
errors.

but it works fine without the leading "shift". Of course I can do the
shift on the next line, but I don't understand why this syntax isn't
OK.

operator precedence.
shift takes an array as an arg, I gave it an array, but its still
not happy?

It's not complaining you didn't give it an array. It's complaining you're
trying to make an assignment to the shift function. Read the error
message again.
And why is it assuming I'm trying to do anything implicitly
with @_ ?

Because you're using split in a scalar context. And that's what split in
a scalar context does.
And if this is a matter of mucking up this nice clean syntax with
parens, I hope the P6 Porters are reconsidering the priority order. If
that's the answer I'll just go with the next line shift..

I strongly contest that the above is "nice clean syntax". I'd call it
ambiguous at best. And yes, the solution would be parentheses,
because of operator precedence. Unary operators (like shift) bind more
tightly than the = operator. So your code is equivalent to:

(shift my @id) = split /ID\s+([^\n]+)\n/, $$_;
whereas what you want is
shift (my @id = split /ID\s+([^\n]+)\n/, $$_;)

HOWEVER, this will not work either. Because now you're not giving shift
an array, you're giving it a list assignment. The first argument to shift
must be a named array. This is why the corresponding syntax for chomp
would work, but shift does not. (perldoc -f chomp shows that it takes a
LIST, whereas shift takes an ARRAY).

I'm not especially sure you'll find any combination of parentheses that
will do what you want in one line.

Paul Lalli
 
A

Anno Siegel

Paul Lalli said:
Perl is unhappy with this line:

shift my @id = split /ID\s+([^\n]+)\n/, $$_;
[...]

(shift my @id) = split /ID\s+([^\n]+)\n/, $$_;
whereas what you want is
shift (my @id = split /ID\s+([^\n]+)\n/, $$_;)

HOWEVER, this will not work either. Because now you're not giving shift
an array, you're giving it a list assignment. The first argument to shift
must be a named array. This is why the corresponding syntax for chomp
would work, but shift does not. (perldoc -f chomp shows that it takes a
LIST, whereas shift takes an ARRAY).

I'm not especially sure you'll find any combination of parentheses that
will do what you want in one line.

If the purpose is to isolate the first element, this would do

my ( $first, @id) = split ...;

To ignore it, replace "$first" with "undef".

Anno
 
J

Joe Smith

Sara said:
Perl is unhappy with this line:
shift my @id = split /ID\s+([^\n]+)\n/, $$_;
Use of implicit split to @_ is deprecated at ./SSMCP10Conditioner.pl
And why is it assuming I'm trying to do anything implicitly with @_ ?

You'll get that message when doing {$scalar = split /.../}.

Since shift(@id) returns a scalar value, what you've written is like
12345 = split /ID\s+([^\n]+)\n/, $$_;
which has to problems: split is being told to operate in scalar context
and you're attempting to modify a readonly value.

Forget about shift; use a dummy variable or undef to ignore the first item.
my ($first,@id) = split /ID\s+([^\n]+)\n/, $$_;

-Joe
 

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
474,262
Messages
2,571,059
Members
48,769
Latest member
Clifft

Latest Threads

Top