trapping errors
From: Simon (simalt_at_totalise.co.uk)
Date: 03/31/04
- Next message: David K. Wall: "Re: multiple lines / success or failure?!"
- Previous message: Darek Adamkiewicz: "Is there any standard newsgroup messages parser?"
- Next in thread: A. Sinan Unur: "Re: trapping errors"
- Reply: A. Sinan Unur: "Re: trapping errors"
- Reply: Paul Lalli: "Re: trapping errors"
- Reply: Tore Aursand: "Re: trapping errors"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 31 Mar 2004 15:21:42 +0100
How can I trap an error and let the program continue without aborting e.g
I know the die can abort on errors but not sure how to trap the error it
generates and let the program continue.
e.g the following program opens four dirs and read the contents and returns
dir count.
#!/perl/bin/perl -w
use strict;
my($dir1, $dir2, $dir3, $dir4, @file_count, $count, @hold_paths, $num,
@hold_all, $directory_numbers,
$value, $len);
# These are the directory tree paths,
$dir1 = '/tools';
$dir2 = '/test';
$dir3 = '/develop';
$dir4 = '/utils';
@hold_paths = ($dir1,$dir2,$dir3,$dir4);
$num = 0;
for(@hold_paths) { # iterate over each path, open the dir read num of files,
put into array @hold_all
opendir(DIR, $hold_paths[$num]) || die "can't opendir
$hold_paths[$num]: $!";
@file_count = readdir(DIR);
$hold_all[$num] = $hold_paths[$num] ." ". scalar(@file_count) ."\n";
closedir DIR || die "can't opendir $hold_paths[$num]: $!";
$num++;
}
$directory_numbers = 'dir.txt'; # will write to current directory, unless
#you put a 'path'
# this is the file your
writing you data #to
# This is where you actually write to the file
# Will 'overwrite' file each time
open(FILE, ">$directory_numbers") || die "can't open $hold_paths[$num]:
$!";
print FILE @hold_all;
close FILE || die "can't close $hold_paths[$num]: $!";
==============================================
Now what if one of the directory was not available or removed it simply
generates this message:
can't opendir /test: No such file or directory at countdirx.pl line 17.
Is there a way I can allow the program to continue even though the "test"
dir is not available but the program can report on "test" being unavailable
and show what is available.
Thanks for your help.
Simon
- Next message: David K. Wall: "Re: multiple lines / success or failure?!"
- Previous message: Darek Adamkiewicz: "Is there any standard newsgroup messages parser?"
- Next in thread: A. Sinan Unur: "Re: trapping errors"
- Reply: A. Sinan Unur: "Re: trapping errors"
- Reply: Paul Lalli: "Re: trapping errors"
- Reply: Tore Aursand: "Re: trapping errors"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|