FAQ
Should I preserve this behaviour?

$ perl -wle'$[=2; print substr("abcdefghij", -2);'
ij

$ perl -wle'$[=1; print substr("abcdefghij", -2);'
ij

$ perl -wle'$[=0; print substr("abcdefghij", -2);'
ij

$ perl -wle'$[=-1; print substr("abcdefghij", -2);'
ij

$ perl -wle'$[=-2; print substr("abcdefghij", -2);'
abcdefghij

$ perl -wle'$[=-3; print substr("abcdefghij", -2);'
bcdefghij

Search Discussions

  • Nicholas Clark at Feb 12, 2010 at 6:26 pm

    On Fri, Feb 12, 2010 at 01:15:52PM -0500, Eric Brine wrote:
    Should I preserve this behaviour?

    $ perl -wle'$[=2; print substr("abcdefghij", -2);'
    ij

    $ perl -wle'$[=1; print substr("abcdefghij", -2);'
    ij

    $ perl -wle'$[=0; print substr("abcdefghij", -2);'
    ij

    $ perl -wle'$[=-1; print substr("abcdefghij", -2);'
    ij

    $ perl -wle'$[=-2; print substr("abcdefghij", -2);'
    abcdefghij

    $ perl -wle'$[=-3; print substr("abcdefghij", -2);'
    bcdefghij
    Without knowing how hard it is, "Yes, if possible" seems to be the best answer
    I can give.

    Although it's not like I can imagine anyone sane using $[ with a negative
    value.

    Nicholas Clark
  • David Nicol at Feb 12, 2010 at 6:54 pm
    I'm stunned. I'd always assumed that $[ mods were applied to the index
    before anything else happens. My feeling on this matter, which must be
    missing from the test suite, is that an appropriate path would be to
    determine how it should be, add tests to test that it is that way,
    make it that way, and make some noise about it in the release notes.

    In particular,


    $ perl -wle'$[=-1; print substr("abcdefghij", -2);'
    ij

    should give j not ij.

    howver,
    http://perldoc.perl.org/perldata.html#Subscripts doesn't mention $[
    and says negs start from the tail;

    and http://perldoc.perl.org/perlvar.html/ doesn't mention negatives at
    all while it discourages the use of $[.

    so the behavior you list seems to all be working as documented.

    Left undefined (and unexplored in your post) are the situations where
    the index is greater than or equal to zero and less than a positive $[
    greater than one.

    wow, with a $[ of 2, a substr starting from 1 died with
    panic: sv_setpvn called with negative strlen at -e line 1.


    the -f substr docs say clearly
    # substr EXPR,OFFSET,LENGTH
    # substr EXPR,OFFSET

    Extracts a substring out of EXPR and returns it. First character is at
    offset 0 , or whatever you've set $[ to (but don't do that). If OFFSET
    is negative (or more precisely, less than $[ ), starts that far from
    the end of the string.

    so maybe the implementation should be changed to match the documentation.
  • Nicholas Clark at Feb 12, 2010 at 7:11 pm

    On Fri, Feb 12, 2010 at 12:52:58PM -0600, David Nicol wrote:
    I'm stunned. I'd always assumed that $[ mods were applied to the index
    before anything else happens. My feeling on this matter, which must be
    missing from the test suite, is that an appropriate path would be to
    determine how it should be, add tests to test that it is that way,
    make it that way, and make some noise about it in the release notes.
    the -f substr docs say clearly
    # substr EXPR,OFFSET,LENGTH
    # substr EXPR,OFFSET

    Extracts a substring out of EXPR and returns it. First character is at
    offset 0 , or whatever you've set $[ to (but don't do that). If OFFSET
    is negative (or more precisely, less than $[ ), starts that far from
    the end of the string.

    so maybe the implementation should be changed to match the documentation.
    You seem to be forgetting that $[ is deprecated, and likely to be removed
    pretty soon.

    I think that the only thing we really need to do right now is oil and
    fuel the chainsaw, so that it is ready for action when needed.

    Nicholas Clark
  • Eric Brine at Feb 12, 2010 at 8:00 pm

    On Fri, Feb 12, 2010 at 1:25 PM, Nicholas Clark wrote:
    On Fri, Feb 12, 2010 at 01:15:52PM -0500, Eric Brine wrote:
    Should I preserve this behaviour?

    $ perl -wle'$[=2; print substr("abcdefghij", -2);'
    ij

    $ perl -wle'$[=1; print substr("abcdefghij", -2);'
    ij

    $ perl -wle'$[=0; print substr("abcdefghij", -2);'
    ij

    $ perl -wle'$[=-1; print substr("abcdefghij", -2);'
    ij

    $ perl -wle'$[=-2; print substr("abcdefghij", -2);'
    abcdefghij

    $ perl -wle'$[=-3; print substr("abcdefghij", -2);'
    bcdefghij
    Without knowing how hard it is, "Yes, if possible" seems to be the best
    answer
    I can give.
    Keeping the existing behaviour adds no difficulty. I felt like saying
    something about the oddness since I'm working in that area.
  • Eric Brine at Feb 12, 2010 at 8:36 pm
    Charming:

    $ perl -wle'$[=2; print substr("abcdefghij", 1);'
    panic: sv_setpvn called with negative strlen at -e line 1.

    Gonna treat it as out of bounds.

    (This message serves as a note to make sure I check that I fixed this)
  • Zefram at Feb 12, 2010 at 7:16 pm

    Eric Brine wrote:
    Should I preserve this behaviour?
    Negative $[ is worse than you think. $[ was obviously never intended
    to be negative, and it has the obvious clash with negative numbers for
    end-relative indexing. In fact, the behaviour isn't even consistent:

    $ perl -lwe '$[=-2; @x=qw(a b c d e f g h i); print $x[-2], $x[-1]'
    ab
    $ perl -lwe '$[=-2; @x=qw(a b c d e f g h i); print @x[-2,-1]'
    hi

    I'd be surprised if anyone at all deliberately uses a negative $[.
    But we shouldn't be looking to change the behaviour. $[ is being
    deprecated anyway, so there's no reason to fix it.

    Fwiw, in Array::Base, my modulified reimplementation of the $[ concept,
    I didn't bother to support end-relative indexing at all.

    -zefram
  • James Mastros at Feb 13, 2010 at 9:57 am
    Strange behavior of this sort (not exactly here, but vaugely similar,
    as I recall) was the impeteus for me to look into how hard it'd be to
    get rid of $[ entirely. The answer seems to be "not hard at all", so
    I really wonder what you are up to... but I'd say that you needn't
    bother trying to keep it working, unless you are doing something you
    expect to go in before 5.12, since as soon as 5.12 ships, I intend to
    get rid of it entirely. If it's going in before that, then try, but
    not too hard -- next to nobody uses the blasted thing anyway.

    -=- James Mastros

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupperl5-porters @
categoriesperl
postedFeb 12, '10 at 6:16p
activeFeb 13, '10 at 9:57a
posts8
users5
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase