Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

'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: 'test.pl'

print "Hello world!\n"; do 'test-response.pl'; print "Did that work?";
File: 'test-response.pl' (same directory as 'test.pl')
print "Hello Andy!\n";
Output:
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!

Andy

Replies are listed 'Best First'.
Re: 'do' command is dead? Surely not?
by haukex (Monsignor) 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 '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.
        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?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2017-09-21 11:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (245 votes). Check out past polls.

    Notices?