Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Open Source > Perl
| Search | Today's Posts | Mark Forums Read
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Perl section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old June 14th, 2007, 06:26 AM
Registered User
 
Join Date: Jun 2007
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Customised Error Messages

Hi all, I ahve written a script which will search for some directories and validate them whether they exist or not and accordingly show the messages(output) like

/usr/bin/denzil => OK
/home/denz/temp => Fail

but the problem is that I need to put a condition to show "Invalid or Insufficient Permissions for the directories which are not accessible to me but they exist like
/home/jessica/temp/perl => No Permission

I have tried the code
my $per = `ls $dir`;

if($per =~ /denied/){

// statement

and
my $per = "ls $dir";
eval($per) or $var =1;

if($var){
     //statement


It is showing the default error message everytime ..I need to supress these errors and put my own messages so that it would go to my condition Please help me out

  #2 (permalink)  
Old June 14th, 2007, 10:07 AM
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Yuck! Try this:
Code:
my $path = "/path/to/dir";                       # this is the dir you want to open
die ("$path: No permissions") unless (-r $path); # -r $path checks its readable, die if not
opendir(DIR, $path) ||                           # try to open your directory 
          die "Cannot opendir $path: $!";        # if you can't print an error and die
for(sort readdir(DIR)) {                         # loop through the files in the dir, in sorted order
  print "$_\n";                                  # print their names
}
closedir(DIR);                                   # close directory
--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
  #3 (permalink)  
Old June 15th, 2007, 01:11 AM
Registered User
 
Join Date: Jun 2007
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

well its not working because as soon as it checks for the files which are not accessible gives the
output "the directory does not exist" which is actually because of permission problem...
And my concern is regarding inside any home directory like /home/denzil/temp or
/home/jessica/tmp... Please tell me how to suppress the default error I mean to catch the
default error and put the condition for that

  #4 (permalink)  
Old June 15th, 2007, 10:27 AM
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Quote:
quote:
 well its not working because as soon as it checks for the files which are not accessible gives the
output "the directory does not exist" which is actually because of permission problem...
erm, just check if the file is readbable in the looop then:

Code:
my $path = "/path/to/dir";                       # this is the dir you want to open
die ("$path: No permissions") unless (-r $path); # -r $path checks its readable, die if not
opendir(DIR, $path) ||                           # try to open your directory 
          die "Cannot opendir $path: $!";        # if you can't print an error and die
for(sort readdir(DIR)) {                         # loop through the files in the dir, in sorted order
  die ("$_: No permissions") unless (-r $_);     # <---- Checks file is readable, dies if it isn't
  print "$_\n";                                  # print their names
}
closedir(DIR);                                   # close directory
Gives me:
Code:
charlie@charlie:~$ su
Password:
charlie:/home/charlie# touch aaa
charlie:/home/charlie# chmod 700 aaa
charlie:/home/charlie# exit
exit
charlie@charlie:~$ ./tmp.pl
.
..
.ICEauthority
.Trash
.Xauthority
Perspectives From The World Social Forum.doc
aaa: No permissions at ./tmp.pl line 9.
charlie@charlie:~$
Quote:
quote:
And my concern is regarding inside any home directory like /home/denzil/temp or
/home/jessica/tmp...
Not sure what you mean here

Quote:
quote:
Please tell me how to suppress the default error I mean to catch the
default error and put the condition for that
I think you're asking me to tell you how to check if a file is readable.
I don't know what a "default error" is. You can say
Code:
if (-r $file) { # if the file is readable
  # do something
}
else { #otherwise
  # do something else
}
hope that helps

--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lua error messages trav BOOK Beginning Lua Programming ISBN: 978-0-470-06917-2 1 June 3rd, 2007 01:21 PM
error messages strt_prg Struts 1 November 13th, 2006 07:05 AM
error messages timbal25 Classic ASP Databases 0 August 1st, 2006 02:56 PM
Error messages ldoodle Access ASP 12 June 6th, 2005 01:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.