Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This :
$#{@{//}}
gives:

Bizarre copy of ARRAY in leave at line 1

Why?

Replies are listed 'Best First'.
Re: Why "Bizzare copy" ?
by davido (Cardinal) on Mar 30, 2004 at 08:43 UTC
    // is an empty regexp.

    @{ ... } dereferences a reference into an array. In this case, you're trying to coerce the return value of $_ =~ m// into being an array. That actually attempts to create a symbolic reference, creating a variable named @1.

    $#{ ... } tries to find the highest index of @1.

    If you turn on use strict; and use warnings; you'll get all sorts of clues.


    Dave

      In that case
      $#{@1}

      would die in the same way. It does not.

        $#{@1}
        would die in the same way. It does not.

        No, it wouldn't, as it doesn't use a symbolic reference...

        If you take the time to dump or peek at @1, you'll see that it exists as empty array...as do all @NUM I tested.

        My guess was, that @1 exists as a side effect of $1 beeing a default-variable and the "multi-type" nature of perl variables. I'd love to be corrected/educated on this rather blunt assumption I wasn't able to backup with my first dive into Devel::Peek and the like.

        regards,
        tomte


        Hlade's Law:

        If you have a difficult task, give it to a lazy person --
        they will find an easier way to do it.

        Actually, it does die, but only in the debugger.

        % perl -d -e '$#{@1}' Default die handler restored. Loading DB routines from perl5db.pl version 1.07 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(-e:1): $#{@1} DB<1> c Bizarre copy of ARRAY in leave at -e line 1. Debugged program terminated.

        Without the debugger, it works fine.

        This error message is created in sv.c, in the function Perl_sv_setsv. This function copies a scalar value from one location to another. "Bizarre copy of ARRAY in leave" means that an opcode called "leave" tried to copy an array to a scalar, should be impossible. "leave" is the opcode for leaving a block of code. Why this happened, I cannot figure out.

        correction: in that case
        $#{@{"1"}}
        would die in the same way. This however, does work:
        @a=@{//};$#a
        Why?
Re: Why "Bizzare copy" ?
by liz (Monsignor) on Mar 30, 2004 at 14:43 UTC
    Don't know why. But it does the same for any version of Perl, threaded and unthreaded, between 5.00504 and 5.9.0 that I tried. And it is purely Perl internals related, as the problem is valgrind clean in Intel boxen.

    Seems to me a bug report is in order.

    Liz

Re: Why "Bizzare copy" ?
by ambrus (Abbot) on Mar 30, 2004 at 16:41 UTC

    Strange indeed.

    And $#{$x=@{//}}; does not fail.

Re: Why "Bizzare copy" ?
by dave_the_m (Monsignor) on Mar 30, 2004 at 22:45 UTC
    Its a bug. Needs fixing sometime, but I'm not in a hurry to do so.