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

Discussion in 'Perl Misc' started by Dr.Ruud, Jan 28, 2010.

  1. Dr.Ruud

    Dr.Ruud Guest

    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.


    --
    Ruud
    Dr.Ruud, Jan 28, 2010
    #1
    1. Advertising

  2. Dr.Ruud

    Steve C Guest

    Dr.Ruud wrote:
    > 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.
    Steve C, Jan 28, 2010
    #2
    1. Advertising

  3. Dr.Ruud

    Steve C Guest

    Steve C wrote:
    > Dr.Ruud wrote:
    >> 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.


    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.
    Steve C, Jan 28, 2010
    #3
  4. Dr.Ruud

    John Bokma Guest

    Steve C <> writes:

    > Dr.Ruud wrote:
    >> 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.


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

    --
    John Bokma j3b

    Hacking & Hiking in Mexico - http://johnbokma.com/
    http://castleamber.com/ - Perl & Python Development
    John Bokma, Jan 28, 2010
    #4
  5. On 2010-01-28, Dr.Ruud <> wrote:
    > 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
    Ilya Zakharevich, Jan 29, 2010
    #5
  6. Dr.Ruud

    sreservoir Guest

    On 1/28/2010 7:40 PM, Ilya Zakharevich wrote:
    > "sub f{ for(1..3) {print $_++ }} f; print q(==); f"


    does your shell do no substitution?

    --

    "Six by nine. Forty two."
    "That's it. That's all there is."
    "I always thought something was fundamentally wrong with the universe"
    sreservoir, Jan 29, 2010
    #6
  7. On 2010-01-29, sreservoir <> wrote:
    > On 1/28/2010 7:40 PM, Ilya Zakharevich wrote:
    >> "sub f{ for(1..3) {print $_++ }} f; print q(==); f"

    >
    > 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
    Ilya Zakharevich, Jan 29, 2010
    #7
  8. On 2010-01-29 08:17, Ilya Zakharevich <> wrote:
    > On 2010-01-29, sreservoir <> wrote:
    >> On 1/28/2010 7:40 PM, Ilya Zakharevich wrote:
    >>> "sub f{ for(1..3) {print $_++ }} f; print q(==); f"

    >>
    >> 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 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
    Peter J. Holzer, Jan 29, 2010
    #8
  9. "Peter J. Holzer" <> wrote:
    >On 2010-01-29 08:17, Ilya Zakharevich <> wrote:
    >> On 2010-01-29, sreservoir <> wrote:
    >>> On 1/28/2010 7:40 PM, Ilya Zakharevich wrote:
    >>>> "sub f{ for(1..3) {print $_++ }} f; print q(==); f"
    >>>
    >>> 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 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
    Jürgen Exner, Jan 29, 2010
    #9
  10. On 2010-01-29, Peter J. Holzer <> wrote:
    >> 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.


    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
    Ilya Zakharevich, Jan 29, 2010
    #10
  11. On 2010-01-29 17:30, Ilya Zakharevich <> wrote:
    > Having code which may be immediately executed overrides all other
    > considerations.


    No.

    hp
    Peter J. Holzer, Jan 29, 2010
    #11
  12. Dr.Ruud

    Steve C Guest

    John Bokma wrote:
    > Steve C <> writes:
    >
    >> Dr.Ruud wrote:
    >>> 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.

    >
    > 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?
    Steve C, Jan 29, 2010
    #12
  13. Dr.Ruud

    C.DeRykus Guest

    On Jan 29, 10:43 am, Steve C <> wrote:
    > John Bokma wrote:
    > > Steve C <> writes:

    >
    > >> Dr.Ruud wrote:
    > >>> 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.

    >
    > > 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?



    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

    --
    Charles DeRykus
    C.DeRykus, Jan 29, 2010
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Irving Kimura

    Why Carp::croak over die?

    Irving Kimura, May 13, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    483
    Irving Kimura
    May 14, 2004
  2. Michael TEpperis

    problems with croak

    Michael TEpperis, Jun 3, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    117
    John Bokma
    Jun 3, 2004
  3. John Bokma

    croak conventions in methods?

    John Bokma, Nov 17, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    84
    John Bokma
    Nov 18, 2004
  4. Peter Wyzl
    Replies:
    3
    Views:
    165
    Peter Wyzl
    Aug 1, 2005
  5. Paul Lalli

    croak/confess from within File::Find

    Paul Lalli, Jul 10, 2007, in forum: Perl Misc
    Replies:
    4
    Views:
    123
    -berlin.de
    Jul 11, 2007
Loading...

Share This Page