should C<++$_ for -1..1> croak?

D

Dr.Ruud

Should C<++$_ for -1..1> croak?

Or is it better to leave it as it is?



$ perl -wle'++$_ for 1..1'


$ perl -wle'++$_ for 1'
Modification of a read-only value attempted at -e line 1.
 
S

Steve C

Dr.Ruud said:
Should C<++$_ for -1..1> croak?

Or is it better to leave it as it is?



$ perl -wle'++$_ for 1..1'


$ perl -wle'++$_ for 1'
Modification of a read-only value attempted at -e line 1.

for implicitly aliases $_ to the constant 1, which cannot be incremented.
 
S

Steve C

Steve said:
for implicitly aliases $_ to the constant 1, which cannot be incremented.

perl -wle 'sub mod{++$_[0]} mod(1)'
Modification of a read-only value attempted at -e line 1.

The for example seems to be consistent behavior, if that's what you're asking.

I guess I would be upset if after calling mod the constant 1 became 2.
That used to happen to me with FORTRAN programs on IBM when I passed a
constant to a sub which modified its parameters.
 
J

John Bokma

Steve C said:
for implicitly aliases $_ to the constant 1, which cannot be
incremented.

Note that there are two examples and only the last one complains about a
modification of ro value.
 
I

Ilya Zakharevich

Should C<++$_ for -1..1> croak?
Or is it better to leave it as it is?

Tough call. I reported it many years ago. *Then* it was like this
perl5.00455 -wle "sub f{ for(1..3) {print $_++ }} f; print q(==); f"
1
2
3
==
2
3
4

This was fixed; nowadays it is "not that bad"...

I know this does not help,
Ilya
 
I

Ilya Zakharevich

does your shell do no substitution?

There is no hope to give command line examples which would work with
all 3 major shell types... This is why I always use q() and qq() in
my posts: one needs to change only two delimiters to suit your shell...

Hope this helps,
Ilya
 
P

Peter J. Holzer

There is no hope to give command line examples which would work with
all 3 major shell types...

This is why I avoid posting Perl code in the form
perl -e '...'
It gratuitously involves some shell which may or may not do some
interpolation of its own. The reader must then guess the type of shell
and interpolation (if any) it performs.

If you just post the Perl code:

sub f{ for(1..3) {print $_++ }} f; print q(==); f

it is clear that only the behaviour of perl needs to be considered,
and any shell is irrelevant. To test this, the reader can store it in a
one-line file or use the shell with appropriate quotes.

Besides, if you aren't constrained to one line by the deficiencies of
your shell, you can make the code a bit more readable:

sub f{
for(1..3) {
print $_++
}
}

f;
print q(==);
f

hp
 
J

Jürgen Exner

Peter J. Holzer said:
This is why I avoid posting Perl code in the form
perl -e '...'
It gratuitously involves some shell which may or may not do some
interpolation of its own. The reader must then guess the type of shell
and interpolation (if any) it performs.

Thank you! Finally someone with some common sense to speak up!

jue
 
I

Ilya Zakharevich

This is why I avoid posting Perl code in the form
perl -e '...'
It gratuitously involves some shell which may or may not do some
interpolation of its own. The reader must then guess the type of shell
and interpolation (if any) it performs.

If you just post the Perl code:

sub f{ for(1..3) {print $_++ }} f; print q(==); f

it is clear that only the behaviour of perl needs to be considered,
and any shell is irrelevant.

Having code which may be immediately executed overrides all other
considerations. How would the user KNOW that this is complete
snippet? How would they know the version of Perl it runs under? How
would they know what @ARGV and command-line options are required?

Perl culture is very strongly intertwined with the command-line
culture. If somebody cannot see that ""-delimiters are used, and does
not know about shell quoting semantic variability, I do not care if I
lose this person's attention in the discussion.
To test this, the reader can store it in a
one-line file or use the shell with appropriate quotes.

But she, most probably, won't.
Besides, if you aren't constrained to one line by the deficiencies of
your shell, you can make the code a bit more readable:

sub f{
for(1..3) {
print $_++
}
}

f;
print q(==);
f

Since I (and many readers) am, this does not matter much, right?

Yours,
Ilya
 
S

Steve C

John said:
Note that there are two examples and only the last one complains about a
modification of ro value.

perlop says:
"In the current implementation, no temporary array is created when the
range operator is used as the expression in "foreach" loops"

I assume that to mean that in the first case $_ is being set to an lvalue
by the loop code rather than aliased to a constant in a list.
Would this throw an error in older perl versions that did build a list,
or has it always worked?
 
C

C.DeRykus

perlop says:
"In the current implementation, no temporary array is created when the
range operator is used as the expression in "foreach" loops"

I assume that to mean that in the first case $_ is being set to an lvalue
by the loop code rather than aliased to a constant in a list.
Would this throw an error in older perl versions that did build a list,
or has it always worked?


I think you're right and B::Concise seems to confirm your
statement that the first case tries to set the constant to
a modifiable lvalue while the second doesn't. Maybe someone
familiar with the internals can clarify...

perl -MO=Concise,-exec -wle'++$_ for 1'

1 <0> enter
2 <;> nextstate(main 1 -e:1) v:{
3 <;> nextstate(main 1 -e:1) v:{
4 <0> pushmark sM
5 <$> const[IV 1] sM <---- M = Will modify (lvalue)
....

perl -MO=Concise,-exec perl -wle'++$_ for 1..1'
1 <0> enter
2 <;> nextstate(main 1 -e:1) v:{
3 <;> nextstate(main 1 -e:1) v:{
4 <0> pushmark s
5 <$> const[IV 1] s
....


Deja vu all over again too. Didn't Ben just mention the likelihood
this was a bug:

http://groups.google.com/group/comp.lang.perl.misc/browse_thread/thread/f5fe9cb45c9adb90?hl=en
 

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

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top