in reply to Re^2: Negative array subscript method call bug?
in thread Negative array subscript method call bug?

I started to write this...

It doesn't. perl can't even make it that far because it can't figure out what it is you are trying to call "meth" on. Before it ever even gets arround to trying, it has to "make an array value spring into existence" and when it tried, it found that "subscript -1" ment "the subscript was (probably) negative" so ultimately you were asking it to set (aka: modify) a "non-creatable array value"

...but then i started to see your point: "Modification" ? why is refrencing the (non-creatable) array value to call a method on it considered a modification?

It would make more sense to say something like "Reference of non-creatable array value attempted"...

laptop:~> perl -e '$y = \$x[-1000];' Modification of non-creatable array value attempted, subscript -1000 a +t -e line 1.

Replies are listed 'Best First'.
Re^4: Negative array subscript method call bug?
by ysth (Canon) on Aug 08, 2004 at 07:39 UTC
    Hint: in a method call, the object is passed as the first parameter. Perl allows subroutines to modify parameters.

      That doesn't strike me as a very legitimate reason, because method invocation isn't the only place where this happens. hence the other example I posted...

      laptop:~> perl -e '$y = \$x[-1000];' Modification of non-creatable array value attempted, subscript -1000 a +t -e line 1.

      ...there's nothing remotely close to a modification there.

        ...there's nothing remotely close to a modification there.

        Sure there is. The reference operator must have something to reference. And inorder to reference an element of an array the array element must exist. Thus before the reference can be taken it tries to create the element, but fails because it makes no sense. Try doing the same thing but with a positive index, and then print out how large the array is afterwards....


        ---
        demerphq

          First they ignore you, then they laugh at you, then they fight you, then you win.
          -- Gandhi


        Literally, you are correct, and the error message is misworded. However, this is an example of what I'm going to call for lack of a better term "modification context". Same applies to &foo($x[-1000]) or $x[-1000]->foo. That's what the error message is referring to.
Re^4: Negative array subscript method call bug?
by BUU (Prior) on Aug 08, 2004 at 06:21 UTC
    How about just "method called on undefined value"?

    perl -e'$x= $x[-1]' $x is, of course, undefined.

    So logically $x=$x[-1];$x->meth and $x[-1]->meth should produce the same error, right? Theres no attempt at creation anywhere. Just trying to find an index that doesn't exist.

     $x[1]->meth produces the "Can't call method on undefined value" error, so why is -1 different than 1 when neither exist?

      It's a case of which error came first.

      In this first case, $x[ 0 ] exists, so $x[ -1 ] can be translated as

      $x[ @x - 1 ] ==> $x[ 1 - 1 ] ==> $x[ 0 ]

      which is valid and currently undef, so the error occurs when try to indirect through undef for the method call.

      P:\test>perl -e" @x = undef; $x[ -1 ]->fred()" Can't call method "fred" on an undefined value at -e line 1.

      But in this case you have $x[ 0 - 1 ], which translates to a post-normalisetion, negative index (-1), which is illegal.

      So that's the error you see, and the indirection is never attempted.

      P:\test>perl -e" $x[ -1 ]->fred()" Modification of non-creatable array value attempted, subscript -1 at - +e line 1.

      Of course, the subscript normalisation also incorporates $[, but the always 0...right :)

        In this first case, $x[ 0 ] exists, so $x[ -1 ] can be translated as
        Which first case? In all of my cases, @x is empty. my @x; @x = (); $x = $x[-1]; $x->meth Theres no problem there with just *accessing* the non existant index to assign it to a variable. It doesn't even error. So why does calling the method attempt to modify it and there for create aforesaid error message?
        A reply falls below the community's threshold of quality. You may see it by logging in.