http://www.perlmonks.org?node_id=1190616


in reply to 'do' command is dead? Surely not?

You may want to try adding the error checking from the do docs:

unless (my $return = do $file) { warn "couldn't parse $file: $@" if $@; warn "couldn't do $file: $!" unless defined $return; warn "couldn't run $file" unless $return; }

Update: Oh, and of course Use strict and warnings.

Replies are listed 'Best First'.
Re^2: 'do' command is dead? Surely not?
by ACI (Acolyte) on May 19, 2017 at 14:12 UTC

    Thanks haukex - swift reply!

    What, me, not using strict and warnings? ;-) Have changed 'test.pl' to the following:

    use strict; use warnings; print "Hello world!\n"; my $file='test-response.pl'; unless (my $return = do $file) { warn "couldn't parse $file: $@" if $@; warn "couldn't do $file: $!" unless defined $return; warn "couldn't run $file" unless $return; } print "Did that work?\n";

    Output is now

    Hello world! couldn't do test-response.pl: No such file or directory at test.pl lin +e 10. couldn't run test-response.pl at test.pl line 11. Did that work?

    The file 'test-response.pl' is certainly there.... (the files are on my desktop and I'm calling them from a command prompt having changed directory to the desktop, by the way).

      You can check perl's current directory with Cwd (no matter how unlikely it seems for something to be the cause of a defect, if you're stuck, it's better to check than "just know"). You can also check which files .pl perl sees in the current folder with:

      use Data::Dump qw( pp ); pp <*.pl>;
      I used Data::Dump instead of a simple print, because it will explicitly display characters that might go otherwise unnoticed. If you don't want to install it, you can use Data::Dumper and set $Data::Dumper::Useqq to 1

      do docs say "searches the @INC", so maybe check if "." is missing.

        Recent versions of Perl (v5.24.1+) removed '.' from @INC, so this is probably right on the mark. If the OP is using a recent enough Perl version it will not look in the dot path unless something like this happens:

        use lib '.';

        Though that is subject to the challenges that the dot path has always faced. It would be better to do something like this:

        use FindBin; use lib "$FindBin::Bin/../lib"; # Or whatever is accurate for this scr +ipt's needs.

        This eliminates sensitivity to where the script was invoked from.

        See also: FindBin, lib, and perl5241delta.


        Dave

      The file 'test-response.pl' is certainly there...

      Not to Perl it seems... I would investigate this further. For example, try things like print -e $file ? "exists\n" : "doesn't exist\n";, use Cwd; print getcwd, "\n";, or my $file='/absolute/path/to/test-response.pl';. Also, what version of Perl are you using?

        Thanks both, nice tips.

        Can confirm that both 'test.pl' and 'test-response.pl' are recognised in the following tests

        my @files=(); @files=glob('*'); print join("\n",@files),"\n";

        and

        use Data::Dump qw( pp ); pp <*.pl>;

        and that this test yields '/home/Andy/Desktop' as expected

        use Cwd; my $dir = getcwd; print "$dir\n";

        Finally, %inc contains:

        warnings/register.pm /usr/share/perl/5.24/warnings/register.pm overload.pm /usr/share/perl/5.24/overload.pm vars.pm /usr/share/perl/5.24/vars.pm Cwd.pm /usr/lib/x86_64-linux-gnu/perl/5.24/Cwd.pm Exporter.pm /usr/share/perl/5.24/Exporter.pm overloading.pm /usr/share/perl/5.24/overloading.pm File/Glob.pm /usr/lib/x86_64-linux-gnu/perl/5.24/File/Glob.pm warnings.pm /usr/share/perl/5.24/warnings.pm Data/Dump.pm /usr/local/share/perl/5.24.1/Data/Dump.pm strict.pm /usr/share/perl/5.24/strict.pm /etc/perl/sitecustomize.pl /etc/perl/sitecustomize.pl XSLoader.pm /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/XSLoader.pm subs.pm /usr/share/perl/5.24/subs.pm

        Confusing!