×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

eval {some code};
2

eval {some code};

eval {some code};

(OP)
I have a question about the eval{} function.

How would I get Perl to evaluate a piece of code that checks to see whether a file exists, and depending on whether it does or not, something happens.

Say for example, I have this code:


eval {
open(FILE, "file.txt");
@data = <FILE>;
close(FILE);
};

if ($@) { print "the file does not exist\n"; }
else { print "the file does exist\n"; }



Whenever I try that, I always get the same message, whether or not the file does or does not exist.

If anyone can help, THANKS in advance.

-Vic

vic cherubini
malice365@hotmail.com
epic software
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
Wants to Know: Java, Cold Fusion, Tcl/TK
====

RE: eval {some code};

2
The $@ special variable contains something only if the 'eval' fails.  You can have a successful EVAL and still fail to do what was in the EVAL block.  So, checking to see if the EVAL threw an error is not the same as testing to see if the file was created.  Instead, I would use a file test operator or put a little error checking in with the 'open'.

eval {
open(FILE, "file.txt") or $complaint = 'Failed';
@data = <FILE>;
close(FILE);
};

# look at $complaint or use -e
if ($complaint) { print "Failed";}

# or
unless (-e file.txt) { print "Failed"; }

'hope this helps.....

????why wrap this in a 'eval'????   I am interested to know what effect you are trying to achieve.




keep the rudder amid ship and beware the odd typo

RE: eval {some code};

(OP)
goBoating:

Thanks for the help. In the end, I decided just to use the if (-e $file) {print "passed"}. I wanted to wrap it in an eval because I knew about the reserved Perl variable $@, and thought that if I tried to open a file that did not exist, it would fail the eval, returning a failing value to the $@. I tried that and it did not work.

One way I did get it to work is by doing this:

open(FILE, "file.txt");
@data = <FILE>;
close(FILE);

if ($!) {
print "failed\n";
} else {
print "passed\n";
}


Where the $! variable contained where the file did not work. That did work, but in the end, like I said, just used the -e file tester.

I am doing this for an AI program that I am writing. A few entries back I asked about programming AI with Perl, and decided to do it. I have to do it for a school project, so I was going to have the script set a .txt file on the users computer with a lot of information in it, and read from that file so that it will simulate some AI. I had it test the existance of the file, because if the file did not exist, then they could enter in all of their information so the computer would rememer it.

This is an on going project, so the programming aspect of it does not have to be too deep, just the general understanding of AI. If you have any suggestions, I am open to all of them.

Thanks a lot for the help.

-Vic

vic cherubini
malice365@hotmail.com
epic software
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
Wants to Know: Java, Cold Fusion, Tcl/TK
====

RE: eval {some code};

(OP)
Mike:

Thanks for that. I will try to use that code too.

-Vic

vic cherubini
malice365@hotmail.com
epic software
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
Wants to Know: Java, Cold Fusion, Tcl/TK
====

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close