in reply to Is this a bug in splice?

You've somehow successfully manipulated the array @$. I proved it by adding the following snippet of code:
for my $key (sort keys %main::) { next unless *{$main::{$key}}{ARRAY}; print "$key\n"; }
Before your splice, I found only +, -, ARGV, INC and _ populated. After your splice you added $. The last array was, of course, empty. But the following code clearly populates it:
#!/usr/bin/perl use strict; use warnings; my @list = map {[qw/foo bar blah baz/]} 1 .. 5; splice(@$, 0, 1, "hello", "world") for @list; print "@$\n";
Edit: Simplified my code. I knew there was an easier way to write @{*main::{'$'}{ARRAY}}...

Update: Upon thinking about it, this behaviour likely falls out of whatever logic makes $$ be recognized as a valid variable name. Because if you can name a variable $, then you should be able to put any sigil you want in front of that $. Or dereference it any way you want.

This is one of the astounding things about Perl. You start with a piece of behaviour and you say, "WTF?" Then you figure out what it did and you say, "Oh, here is what it did." Then you figure out why it did it and you go, "Oh, that is really quite reasonable for it to do." And suddenly it doesn't seem all that surprising.

Replies are listed 'Best First'.
Re^2: Is this a bug in splice?
by Limbic~Region (Chancellor) on Feb 20, 2008 at 03:22 UTC
    tilly,
    The frustrating thing is that you can't intentionally create @$ without some effort:
    my @$ = 1..4; # issues an error
    On the other hand, if you accidentally use it without declaration - it springs into existence and doesn't throw any errors nor warnings. Even if that is documented behavior, it smells bad (of course, there is probably very good reason for it).

    Cheers - L~R

      Take a look at the error you get. Perl knows that @$ is not a valid name for a lexical variable. So it won't let you declare it with my. But it is a valid global variable name, and Perl recognizes it as such.

      Try declaring it with our. Also see my update to my original response.

      Edit: I said $@ and meant @$. Thanks ysth.

        tilly,
        I understand that and that this is documented behavior. I still don't like it.

        Perhaps it is because it is the first time I can remember getting bit by it. In a nutshell, perl is telling you that if you want to use a variable that starts with a punctuation character - you are on your own (no warnings/errors) so don't make a mistake.

        Cheers - L~R