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

Now, here's something I don't understand:

c:\@Work\Perl\monks>perl -wMstrict -le "my $ar; print(@$ar); print 'done'; " Can't use an undefined value as an ARRAY reference at -e line 1. c:\@Work\Perl\monks>perl -wMstrict -le "sub foo { return @_; } ;; my $ar; print foo(@$ar); print 'done'; " done
Apparently, dereferencing an undefined value in the context of a user-defined subroutine call takes the stink off of this strict no-no. Huh?!? What am I missing? A statement like  my @ra = @$ar; is also fatal as expected. (Tested under ActiveState 5.8.9 and Strawberry 5.14.4.1.)


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re: Dereference of undefined value OK under strictures?
by LanX (Saint) on Jun 28, 2019 at 20:18 UTC
    My guess is auto vivification
    $ perl -wMstrict -le 'sub foo { return @_; } ;; my $ar; print foo(@$ar); print $ar;' ARRAY(0xada8b410) $

    The aliasing is bypassing the strict check.

    Something like this at least ... ;)

    Update

    DB<12> use strict;print @$b Can't use an undefined value as an ARRAY reference at (eval 39)[/data/ +data/com.termux/files/usr/lib/perl5/5.28.2/perl5db.pl:738] line 2. DB<13> use strict;print @$b=(1..3) 123 DB<14> p $b ARRAY(0xb0a92180) DB<15>

    I wasn't too wrong, strict tolerates it as long as it is an lvalue. And aliasing seems to have this effect.

    HTH! :)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      ... auto vivification ... strict tolerates it as long as it is an lvalue ... aliasing seems to have this effect.

      Ah, yes! And now that you mention it, I vaguely remember a previous discussion along these lines. Thanks!


      Give a man a fish:  <%-{-{-{-<

      DB<12> use strict;print @$b Can't use an undefined value as an ARRAY reference at (eval 39)[/data/ +data/com.termux/files/usr/lib/perl5/5.28.2/perl5db +.pl:738] line 2. DB<13> use strict;print @$b=(1..3) 123 DB<14> p $b ARRAY(0xb0a92180) DB<15>

      Using $a or $b triggers a special case for sort. This can be seen by using some other variable name instead:

      >perl -w -Mstrict -E 'say @$b' Can't use an undefined value as an ARRAY reference at -e line 1. >perl -w -Mstrict -E 'say @$c' Global symbol "$c" requires explicit package name (did you forget to d +eclare "my $c"?) at -e line 1. Execution of -e aborted due to compilation errors. >perl -w -Mstrict -E 'say @$b=(1..3)' 123 >perl -w -Mstrict -E 'say @$c=(1..3)' Global symbol "$c" requires explicit package name (did you forget to d +eclare "my $c"?) at -e line 1. Execution of -e aborted due to compilation errors. >

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        Yes I kept it short, but what's your point?

        We are testing against strict refs not vars.

        $ perl -w -Mstrict -E 'my $c; say @$c' Can't use an undefined value as an ARRAY reference at -e line 1. $ perl -w -Mstrict -E 'my $c; say @$c=(1..3)' 123 $

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: Dereference of undefined value OK under strictures?
by Don Coyote (Hermit) on Jun 29, 2019 at 08:25 UTC

    I tried -MO=Deparse and -d options, passing them through system function however these did not provide any deeper understanding from my standpoint.

    I maybe need to try passing some -D flags.

    I've recently begun using padre with strawberry perl, and I am informed to Recompile perl with -DDEBUGING to use -D switch.

    update: -Mdiagnostics=-verbose with debugger steps through some functions in the diagnostics module, such as diagnostics::death_trap and then splain's the error as:

    Can't use an undefined value as an ARRAY reference at -e line 2 (#1) (F) A value used as either a hard reference or a symbolic reference must be a defined value. This helps to delurk some insidious errors. Uncaught exception from user code: Can't use an undefined value as an ARRAY reference at -e line 2. at -e line 2

    But this doesn't necessarily get to the l-value derivation of the issue. Also the -traceonly option only prints the uncaught exception part without the useful diagnostic provided pre-amble.

      > But this doesn't necessarily get to the l-value derivation of the issue

      From perlglossary#autovivification

      • autovivification
      A Graeco-Roman word meaning “to bring oneself to life”. In Perl, storage locations (lvalues) spontaneously generate themselves as needed, including the creation of any hard reference values to point to the next level of storage.

      Hence it's not undefined anymore when strict refs start checking.

      Hth

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        Thank you, yes the autovivication entry is informative and useful.

        Rather I meant that for one or another reason the debugger steps over each line in both programs. Whereas I would like to explicitly step into the print function or subroutine call to expose for example, the lvalue determinations or aliasing operations. Although on a preliminary read-through of the docs that may be out of scope for the debugger.