Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Open Source > Perl
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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

Reply With Quote
  #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
Reply With Quote
  #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

Reply With Quote
  #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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 03:11 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.