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


in reply to Re: 'do' command is dead? Surely not?
in thread 'do' command is dead? Surely not?

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).

Replies are listed 'Best First'.
Re^3: 'do' command is dead? Surely not?
by Eily (Monsignor) on May 19, 2017 at 14:24 UTC

    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

Re^3: 'do' command is dead? Surely not?
by KurtZ (Friar) on May 19, 2017 at 14:28 UTC
    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

        Recent versions of Perl (v5.24.1+) removed '.' from @INC

        I knew this is coming for 5.26.0+ ... but backporting it before 5.26.0 is released seems rather extraordinary to me.

        For the OP's case, I think
        do './test-response.pl';
        is sufficient.

        Cheers,
        Rob

        Intriguing, though a bit technical for me to grasp. I update the OS religiously, and this might have been a trigger. Currently seem to be on Perl v5.24.1.

        Recent versions of Perl (v5.24.1+) removed '.' from @INC,

        Ehm , why?

Re^3: 'do' command is dead? Surely not?
by haukex (Archbishop) on May 19, 2017 at 14:25 UTC
    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!

        You don't show us the output of the file existence tests. Maybe the filename has whitespace at its end. If -f 'whatever.pl' fails, then for sure, Perl cannot see that file, for whatever reason.

        Also, the interesting variable is @INC, not %INC.

        You can also check if do './whatever.pl' works for you. That would also work around the problem of . not being in @INC.

        You can also check if do '/home/Andy/Desktop/whatever.pl' works for you. That would also work around the problem of . not being in @INC.