Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

eval {some code}; 2

Status
Not open for further replies.

vcherubini

Programmer
May 29, 2000
527
US
I have a question about the [tt]eval{}[/tt] 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:

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

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

[/tt]

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 [sig]<p>vic cherubini<br><a href=mailto:malice365@hotmail.com>malice365@hotmail.com</a><br><a href= software</a><br>====<br>
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director<br>
Wants to Know: Java, Cold Fusion, Tcl/TK<br>
====[/sig]
 
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, &quot;file.txt&quot;) or $complaint = 'Failed';
@data = <FILE>;
close(FILE);
};

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

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

'hope this helps.....

????why wrap this in a 'eval'???? I am interested to know what effect you are trying to achieve. [sig]<p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo[/sig]
 
goBoating:

Thanks for the help. In the end, I decided just to use the if (-e $file) {print &quot;passed&quot;}. 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:

[tt] open(FILE, &quot;file.txt&quot;);
@data = <FILE>;
close(FILE);

if ($!) {
print &quot;failed\n&quot;;
} else {
print &quot;passed\n&quot;;
}
[/tt]

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 [sig]<p>vic cherubini<br><a href=mailto:malice365@hotmail.com>malice365@hotmail.com</a><br><a href= software</a><br>====<br>
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director<br>
Wants to Know: Java, Cold Fusion, Tcl/TK<br>
====[/sig]
 
or you could say

Code:
if(open(F,&quot;somefile.txt&quot;)){
[tab]print &quot;passed\n&quot;;
} else {
[tab]print &quot;failed\n&quot;;
}
[sig]<p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>Making mistakes, so you don't have to. &lt;grin&gt;[/sig]
 
Mike:

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

-Vic [sig]<p>vic cherubini<br><a href=mailto:malice365@hotmail.com>malice365@hotmail.com</a><br><a href= software</a><br>====<br>
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director<br>
Wants to Know: Java, Cold Fusion, Tcl/TK<br>
====[/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top