in reply to exists(&subname) causes strange autovivification problem

It's a bug in autovivification.

$ t() { perl -Mv5.40 -e' no if $ARGV[0], "autovivification"; my @a; push @a, [ exists(&nonesuch) ]; say $a[0][0] ? "exists" : "doesn\x27t exist"; ' "$@" } $ t 0 doesn't exist $ t 1 Modification of a read-only value attempted at -e line 4.

Workaround posted in a different post.

Replies are listed 'Best First'.
Re^2: exists(&subname) causes strange autovivification problem
by choroba (Cardinal) on Nov 06, 2024 at 18:38 UTC
    > It's a bug in autovivification.

    Reported.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re^2: exists(&subname) causes strange autovivification problem
by LanX (Saint) on Nov 06, 2024 at 20:43 UTC
    I've narrowed it down to

    perl -E'no autovivification; push @a, [exists(&no)]; ' Modification of a read-only value attempted at -e line 1.

    please note that the push is also needed to cause that error

    as already explained, is exists returning an empty list - or something "similar" - for false in the buggy case.

    perl -MData::Dump -E'dd exists(&non); no autovivification; dd exists(& +non);' "" ()

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

Re^2: exists(&subname) causes strange autovivification problem
by Danny (Chaplain) on Nov 08, 2024 at 20:51 UTC
    Can someone explain this syntax to me? I understand what it's doing but not why it's doing it.
    no if $ARGV[0], "autovivification";
      no if $ARGV[0], "autovivification";

      no MODULE LIST; is like use MODULE LIST, but calls a different method (unimport(LIST) instead of import(LIST)) - see no

      if is a module named if, not your usual if, but the same idea. The if module accepts a condition, a module, and a (possible empty) argument list. If the condition is true, the if module loads the module passed after the condition and calls its import() or unimport() method.

      All that glued together: if $ARGV[0] is true, call the unimport() method of autovivification. This disables autovivification if $ARGV[0] is true, at compile time.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        Awesome, Thanks! Never heard of the "if" module but it seems pretty handy.