denzil_cactus has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Customised error messages
by moritz (Cardinal) on Jun 14, 2007 at 11:41 UTC
    You should check the directory for readability with -r and for executability (ie you are allowed to chdir to it) with -x:

    if (-r $dir && -x $dir ){ print "$dir => OK\n"; }
      thanx for the reply
Re: Customised error messages
by clinton (Priest) on Jun 14, 2007 at 11:43 UTC
    Read the section about backticks in perlop to learn how to capture STDERR.

    But I wouldn't use ls to perform this simple check. Instead use the builtin file checks (eg -r checks if the file is readable).

    Clint

Re: Customised error messages
by andreas1234567 (Vicar) on Jun 14, 2007 at 11:43 UTC
    use strict; use warnings; my @dirs = qw ( /root /home /sbin /tmp /lost+found /not-a-dir /etc/pas +swd ); for (@dirs) { if (! -e ) { print "$_ => does not exist"; next; } if (! -d ) { print "$_ => not a directory"; next; } (opendir(my $FH, $_)) ? print "$_ => ok" : print "$_ => denied"; } __END__ $ perl -l 621211.pl /root => denied /home => ok /sbin => ok /tmp => ok /lost+found => denied /not-a-dir => does not exist /etc/passwd => not a directory
    Update: Added tests on existence and type.
      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