Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: 'do' command is dead? Surely not?

by haukex (Abbot)
on May 19, 2017 at 14:00 UTC ( #1190616=note: print w/replies, xml ) Need Help??

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 '' to the following:

    use strict; use warnings; print "Hello world!\n"; my $file=''; 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 No such file or directory at lin +e 10. couldn't run at line 11. Did that work?

    The file '' 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.


      The file '' 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/';. Also, what version of Perl are you using?

        Thanks both, nice tips.

        Can confirm that both '' and '' are recognised in the following tests

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


        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/ /usr/share/perl/5.24/warnings/ /usr/share/perl/5.24/ /usr/share/perl/5.24/ /usr/lib/x86_64-linux-gnu/perl/5.24/ /usr/share/perl/5.24/ /usr/share/perl/5.24/ File/ /usr/lib/x86_64-linux-gnu/perl/5.24/File/ /usr/share/perl/5.24/ Data/ /usr/local/share/perl/5.24.1/Data/ /usr/share/perl/5.24/ /etc/perl/ /etc/perl/ /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/ /usr/share/perl/5.24/


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1190616]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2018-06-23 08:48 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.