473,795 Members | 3,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

trapping file i/o error


In most perl examples, it used this method to trap error:

open(INFILE, $fname) or die "Unable to open $fname";
process_file();
close(INFILE)
other_codes();

Now that if I don't want to die after the open so as to run
other_codes()? Could I test the value of file handle INFILE like what I
did with C?

fhandle=fopen(f name,"r")
if (fhandle > 0) {
process_file();
fclose(fhandle)
}
else
show_error()
other_codes();

--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
^ ^ 5:00pm up 2 days 18:54 load average: 1.00 1.00 1.00
Jul 19 '05 #1
15 6038
toylet wrote:

In most perl examples, it used this method to trap error:

open(INFILE, $fname) or die "Unable to open $fname";
process_file();
close(INFILE)
other_codes();

Now that if I don't want to die after the open so as to run
other_codes()? Could I test the value of file handle INFILE like what I
did with C?

fhandle=fopen(f name,"r")
if (fhandle > 0) {
process_file();
fclose(fhandle)
}
else
show_error()
other_codes();

Try something like this:
if ( -e $fname && -r $fname) {
open(INFILE, $fname) or show_error();
}
else {
die "Unable to open $fname";
}

The "-e" tests if the file exists and the "-r" tests if the file is
readable. If you want to see if it is writable use "-w". By testing
for the existence of the file and if you can read from or write to the
file first you can handle those situations gracefully and not have to
capture the error condition.

There is nothing special about the "or die". The "or" is just a logical
operator. Perl uses a short cut optimization of boolean statements.
the open statement returns a 1 if it suceeds and undefined if it fails.
So when the open statement succeeds it returns 1 and evaluating "1 or
anything" will alwayse be true so it will not do the "anything" on the
other hand of the open fails the returned undefined is treated as false
so the second part of the or needs to be evaluated so it could be any
statement or block of statement you want. Do not expect to get anything
of use out of the fhandle.

If you are going to continue writing scripts in perl I would suggest
getting a good book on it. My preferences are either from O'Reilly or
Wrox (if you can find them).

--
Thanks
Charles LaCour
Jul 19 '05 #2
Try something like this:
if ( -e $fname && -r $fname) {
open(INFILE, $fname) or show_error();
}
else {
die "Unable to open $fname";
}
statement or block of statement you want. Do not expect to get anything
of use out of the fhandle.
Too bad. I thought checking he file handle is the best appraoch. in
fact, many languages do that, like SQLCONNECT() in Foxpro, fopen() in
C/Clipper/Foxpro, ... It would be quite troublesome to work around that.
If you are going to continue writing scripts in perl I would suggest
getting a good book on it. My preferences are either from O'Reilly or
Wrox (if you can find them).


There are many websites hosting Perl books online. I use google.com to
find them. Thanks for the advice. What I really need is a job that
demands the use of perl, which is rather scarse in my city. Most of them
uses M$ tools.
--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
^ ^ 4:08pm up 5:41 1 user 1.03 1.01
Jul 19 '05 #3
toylet wrote:
In most perl examples, it used this method to trap error:

open(INFILE, $fname) or die "Unable to open $fname";
process_file();
close(INFILE)
other_codes();

Now that if I don't want to die after the open so as to run
other_codes()? Could I test the value of file handle INFILE like what I
did with C?


In perl, open() does not return a file handle but it does return
a true/false value you can test.

if (open(INFILE, $fname)) {
process_file(IN FILE);
close(INFILE);
} else {
warn "Unable to read $fname: $!\n";
}
other_codes();

Be sure to include $! in the error message; it has strerror(errno) .
-Joe
Jul 19 '05 #4
> In perl, open() does not return a file handle but it does return
a true/false value you can test.
if (open(INFILE, $fname)) {
Be sure to include $! in the error message; it has strerror(errno) .
-Joe


that's what I should be going after. thanks.

--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
^ ^ 7:46pm up 9:19 1 user 1.00 0.94
Jul 19 '05 #5
> Be sure to include $! in the error message; it has strerror(errno) .

"$!" is a text message. can I get the errorno?
is it "$?" as in bash?

--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
^ ^ 7:48pm up 9:21 1 user 1.00 0.94
Jul 19 '05 #6
toylet wrote:
Be sure to include $! in the error message; it has strerror(errno) .

"$!" is a text message. can I get the errorno?
is it "$?" as in bash?


$! = 28; # ENOSPC = 'No space left on device'
print "As a string, the last error was '$!'\n";
print "As a number, errno was ", $!+0, "\n";

That is, $! is magic. See also 'perldoc perlvar'.
-Joe
Jul 19 '05 #7
Thank you. Seems that perl requires the programms the know about context.
print "As a string, the last error was '$!'\n";
print "As a number, errno was ", $!+0, "\n";


--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
^ ^ 3:28pm up 15:48 1 user 1.02 1.00
Jul 19 '05 #8
bob
toylet wrote:
Thank you. Seems that perl requires the programms the know about context.
print "As a string, the last error was '$!'\n";
print "As a number, errno was ", $!+0, "\n";



Yes. but once you *do* know about it, it can be very convenient.
Jul 19 '05 #9
hmm... how do you force a variable into a certain context (could I also
call it "type casting")?

for integer, $i+0 or (int)$i.
for string, $i+""? or is it (string)$i?
for array
for hash
Yes. but once you *do* know about it, it can be very convenient.


--
.~. Might, Courage, Vision. In Linux We Trust.
/ v \ http://www.linux-sxs.org
/( _ )\ Linux 2.4.22-xfs
^ ^ 1:10pm up 2:19 1 user 1.41 1.33
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6902
by: Nathan Bloomfield | last post by:
Hi there, I am having difficulty with a piece of code which would work wonders for my application if only the error trapping worked properly. Basically, it works as follows: - adds records from rsSource into rsDest - if it finds a key violation then it deletes the current record from rsDest and adds the new record from rsSource. This works perfectly - but only for the first found duplicate record, it brings up the error
21
2567
by: Neil | last post by:
Is there a way to trap an error generated in another app that is controlled via automation? I have an Access 2000 app that opens Word 2000 and proceeds to open a series of documents and, in each document, parse the contents and write it to an Access table (the Access app, after opening Word, runs a macro within Word which parses and writes to the Access app via DAO). Occasionally we get an error from Word (such as "document is locked for...
3
2749
by: windandwaves | last post by:
Hi Gurus Does anyone know how I set the error trapping to option 2 in visual basic. I know that you can go to tools, options and then choose on unhandled errors only, but is there a VB command that I can use instead? Cheers Nicolaas
13
4486
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently runs into problems on the system where it will actually be used, and since I used so little error-trapping it dies very ungracefully. I will of course try to fix whatever is causing the error and add error-trapping to the functions where the...
4
2101
by: Bill | last post by:
Despite our best efforts occasionally in an aspx file, something like <%=x%> where x is not defined sqeaks by and I get the ugly asp error message. I want to be able to identify this particular error and issue a pretty message. I use the global.asax on application error to handle generalized error handling. I want to be able to capture and identify the above error.
6
1484
by: SMG | last post by:
Hi , Sory for incomplete message in last post here is the actual problem.. I am using following code in web.confiig for trapping all the error through out my site.. <customErrors mode="On" defaultRedirect="WebForm1.aspx"> <error statusCode="404" redirect="ServerError.aspx"></error> <error statusCode="500" redirect="WebForm3.aspx"></error>
2
1568
by: Fred Nelson | last post by:
I'm devloping a VB.NET web application and I'm having a problem with trapping errors and logging the cause of them. When an unexpected error occurs I want to write it to a file - or e-mail it to me. I have set up everything according to the documentation however when I get to my error page "errorpage.aspx" I can't determine why I'm there! In my web.config file I have the line: <customErrors ... defaultredirect="errorpage.aspx"> In...
6
8206
by: sara | last post by:
I have a procedure to automate bringing several Excel files into our Access tables, on a daily basis. The problem is that if the user has a problem, and tries to run the import again (maybe 3 files imported then there was a data problem and they want to re-import after fixing the problem), I can't get the Error handling to fire if the user is attempting to import duplicate key records. Message when I try to import records already on...
2
3880
by: Captain Nemo | last post by:
I'm still using Office 2000 myself, but some of my clients have Office 2003. I've recently added a piece of code to create an instance of Word, open a document, fill in the blanks and become visible so the document can be printed and/or modified. This all takes place within one form, in which the Word.Application and Word.Document objects are both private form-level variables. Just to be on the safe side I included this piece of code in...
9
2110
by: 47computers | last post by:
Pretty new to PHP, I recently started learning about error trapping. As of right now, I include the following into a page in my website: -------BEGIN PASTE-------- error_reporting(E_ERROR | E_PARSE); set_error_handler("SendErrorReport"); function SendErrorReport($errorNumber, $errorMessage, $errorFile, $errorLine, $vars) {
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10437
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.