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

Yes, buy why does doing ->meth involve a modification or a creation of the left hand side?

Replies are listed 'Best First'.
Re^3: Negative array subscript method call bug?
by hossman (Prior) on Aug 08, 2004 at 05:54 UTC

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

      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 :)

          A reply falls below the community's threshold of quality. You may see it by logging in.