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

Very simply I don't have a 'use Data::Dumper' line in my script
but when later on doing 'print Dump $myvar' I get no error!
Also when doing 'print Dumperrrrrrrrrr $myvar' I get no error too!
Why is that???????
  • Comment on Perl doesn't give error when module is not found

Replies are listed 'Best First'.
Re: Perl doesn't give error when module is not found
by choroba (Cardinal) on Mar 26, 2024 at 10:54 UTC
    That's because you ignore the advice to always use warnings.

    use warnings; print Dumpper 'Hi';

    Perl gives us two warnings:

    Name "main::Dumpper" used only once: possible typo at ... line 2. print() on unopened filehandle Dumpper at ... line 2.
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Yes you're right, I didn't use 'warnings' but shouldn't it die anyway?
        No. print returns true if successful, and in this case it correctly returns undef.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Perl doesn't give error when module is not found
by Jenda (Abbot) on Mar 26, 2024 at 14:19 UTC

    Even if a pair of braces ain't exactly required, it's still better to include them, to make sure your intent is clear. If you wrote it as print Dump($myvar); you'd receive Undefined subroutine &main::Dump called at..., but written like this, the Dumper was treated as a filehandle.

    Similar to this:

    print STDERR "This is an error!\n";

    Jenda
    1984 was supposed to be a warning,
    not a manual!

Re: No error from typo [was: Perl doesn't give error when module is not found]
by ikegami (Patriarch) on Mar 27, 2024 at 12:50 UTC

    If a sub named Dumperrrrrrrrrr exists, then

    print Dumperrrrrrrrrr $myvar;
    means
    print( Dumperrrrrrrrrr( $myvar ) );

    But there is no sub named Dumperrrrrrrrrr, so

    print Dumperrrrrrrrrr $myvar;
    means
    print( Dumperrrrrrrrrr $myvar ); # File handle and thing to print.

    print tries to write to Dumperrrrrrrrrr, but no file handle is associated with that symbol, so it returns false.

    You can use no feature qw( bareword_filehandles ); (5.34+) to detect these errors. use v5.38; (5.38+) also does the trick (plus more).

      I don't think

      print( Dumperrrrrrrrrr $myvar );

      helps anyone to understand what's actually going on. While the situation is more complicated in case of print, the print Dumperrrrrrrrrr $myvar; is at least syntactically the Indirect Object Syntax. If the filehandle does exist, you can think of that statement as "calling the print() method of the Dumperrrrrrr object with the parameter $myvar".

      Jenda
      1984 was supposed to be a warning,
      not a manual!

        I don't think print( Dumperrrrrrrrrr $myvar ); helps anyone to understand what's actually going on.

        That's why I didn't write (just) print( Dumperrrrrrrrrr $myvar );.

        And no, it's not actually an indirect method call. Or any kind of method call. It's a function (i.e. a named operator) with a funky syntax.