Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

problem with do and file existence.

by emilford (Friar)
on Oct 04, 2005 at 15:13 UTC ( [id://497275]=perlquestion: print w/replies, xml ) Need Help??

emilford has asked for the wisdom of the Perl Monks concerning the following question:

I am using do to pull in and excute raw Perl code based on certain criteria. Things were fine, doing the error checking below from the Cookbook.
unless ($return = do $file) { warn "couldn't parse $file: $@" if $@; warn "couldn't do $file: $!" unless defined $return; warn "couldn't run $file" unless $return; }
Someone else came behind and added code that basically checks for file existence:
if (-e "/path/to/file") { # do something here; }
This causes the second error check to fail. I print the error message and get "couldn't do file: No such file or directory". The file being checked for might not always exist. What gives? The file compiles correctly from the command line, yet fails with the do

Replies are listed 'Best First'.
Re: problem with do and file existence.
by sauoq (Abbot) on Oct 04, 2005 at 15:20 UTC

    I don't really understand your question. Maybe it would help if you showed how those 2 chunks of code exist in relation to each other. I think a little more explanation would help us help you.

    -sauoq
    "My two cents aren't worth a dime.";
    
      Sorry for the lack of clarity. We have our main file, say homepage.cgi, that will execute 1 of 2 Perl files: x.cgi and y.cgi. Both x.cgi and y.cgi worked correctly, being called by homepage.cgi's do. Someone else went in and modified x.cgi to include the if (-e "/some/file") code that I mentioned above. Executing x.cgi loads, but I know get an error message with the do in homepage.cgi, complaing that the file in x.cgi does not exist. So the issue is that something with the file check in x.cgi causes the do to dork up in homepage.cgi. Hope that makes better sense.

        Yes, that makes much more sense. Your problem is that the -e is setting $! when the file doesn't exist. It's staying set when you get back to your homepage.cgi. And apparently your x.pl script is returning undef. I'd check the logic in x.pl to see if that new if statement causes a return.

        Update: To elaborate, if you want to do manual error checking like that, it's necessary for you to return something useful from the file you do. Otherwise, the last expression evaluated in the file will be your return value. If that value is undef, you can't tell it apart from those times when do returns undef because of an error or a non-existent file.

        -sauoq
        "My two cents aren't worth a dime.";
        
Re: problem with do and file existence.
by Perl Mouse (Chaplain) on Oct 04, 2005 at 15:20 UTC
    Where did he add the code? And what did he add? Is he perhaps modifying the content of $file?
    Perl --((8:>*
      The change was made inside $file, but the code still compiles correctly and looks fine. It's just a simple file check that seems to be affecting the do.
Re: problem with do and file existence.
by blazar (Canon) on Oct 04, 2005 at 15:31 UTC
    I'm by no means sure about whay you're really asking -but that may just be me-. Why don't you put the unless ($return = do $file) { # etc code inside the block of the second if clause?

    Or else, why don't you use some alternate flow control construct? For example:

    for ($file) { # for aliasing too last unless -e; unless (my ret=do $_) { # ... } }
    or something like that;

    But seriously, you may consider making that file into a proper module...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://497275]
Approved by blazar
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-24 14:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found