Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

'do' command is dead? Surely not?

by ACI (Acolyte)
on May 19, 2017 at 13:57 UTC ( #1190615=perlquestion: print w/replies, xml ) Need Help??
ACI has asked for the wisdom of the Perl Monks concerning the following question:

Hi there. This is infuriating me and I can't comprehend why it's happening. Best guess is that I've mangled the perl folder somehow. But re-installing the OS (Ubuntu Linux) doesn't help. Have I disabled something? Can I debug somehow? How can I find out why this is suddenly not working? Any assistance deeply appreciated!

File: ''

print "Hello world!\n"; do ''; print "Did that work?";
File: '' (same directory as '')
print "Hello Andy!\n";
Hello world! Did that work?

Couldn't be simpler, surely? Done this a million times and never had a problem (unless my code has errors, which is not the problem here).

Thanks in advance!


Replies are listed 'Best First'.
Re: 'do' command is dead? Surely not?
by haukex (Prior) on May 19, 2017 at 14:00 UTC

    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.

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1190615]
Approved by haukex
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2017-07-22 19:50 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (340 votes). Check out past polls.