Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > File test operator (-r) returns 'not exist' rather than 'not readable'

Reply
Thread Tools

File test operator (-r) returns 'not exist' rather than 'not readable'

 
 
Justin
Guest
Posts: n/a
 
      10-30-2003
Hi,

I've noticed that the -r operator returns undef if used on a file in an
unreadable directory (where undef indicates the file is missing) - I'd
like -r to return false (unreadable) in such a case.

I'm comparing our back-up file-list to the file-system. I need to know
if a file exists or if I just can't read it.

ls returns 'Permission Denied' which is what I'd like -r to return.

Any ideas?

Cheers,

Justin

Here's an example of what I mean:

[/tmp] mkdir Test
[/tmp] touch Test/testfile
[/tmp] ls -l Test
total 0
-rw-rw-r-- 1 ja 0 Oct 30 12:58 testfile
[/tmp] chmod 000 Test
[/tmp] perl -e 'print "Not Exist\n" unless defined (-r "Test/testfile")'
Not Exist
[/tmp] ls Test/testfile
ls: Test/testfile: Permission denied
[/tmp] perl -e 'print "Not Exist\n" unless defined (-r "Test")'
[/tmp]

 
Reply With Quote
 
 
 
 
Mike Stok
Guest
Posts: n/a
 
      10-30-2003
In article <3fa16552$0$560$(E-Mail Removed)>,
Justin <(E-Mail Removed)> wrote:
>Hi,
>
>I've noticed that the -r operator returns undef if used on a file in an
>unreadable directory (where undef indicates the file is missing) - I'd
>like -r to return false (unreadable) in such a case.
>
>I'm comparing our back-up file-list to the file-system. I need to know
>if a file exists or if I just can't read it.
>
>ls returns 'Permission Denied' which is what I'd like -r to return.
>
>Any ideas?


>[/tmp] perl -e 'print "Not Exist\n" unless defined (-r "Test/testfile")'
>Not Exist


[mike@ratdog src]$ touch foo
[mike@ratdog src]$ chmod 0 foo
[mike@ratdog src]$ perl -e 'print "Not Exist\n" unless -e "foo"'
[mike@ratdog src]$ perl -e 'print "Not Readable\n" unless -r "foo"'
Not Readable
[mike@ratdog src]$

Hope this helps,

Mike

--
http://www.velocityreviews.com/forums/(E-Mail Removed) | The "`Stok' disclaimers" apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
(E-Mail Removed) | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA
 
Reply With Quote
 
 
 
 
Steve Grazzini
Guest
Posts: n/a
 
      10-30-2003
Justin <(E-Mail Removed)> wrote:
> I've noticed that the -r operator returns undef if used on a
> file in an unreadable directory (where undef indicates the file
> is missing) - I'd like -r to return false (unreadable) in such
> a case.


It doesn't matter whether the directory is readable -- it's the
execute bit (search bit) that determines whether you can stat() a
file in that directory.

> I'm comparing our back-up file-list to the file-system. I need to know
> if a file exists or if I just can't read it.


If you can't read *or* search the directory, you simply can't know
whether the file exists or not. If you can read it, you could use
opendir() to check...

> ls returns 'Permission Denied' which is what I'd like -r to return.


But this is easy; just check $! if the file test returns undef.

--
Steve
 
Reply With Quote
 
Justin
Guest
Posts: n/a
 
      10-30-2003
Steve Grazzini wrote:
> Justin <(E-Mail Removed)> wrote:
>
>>I've noticed that the -r operator returns undef if used on a
>>file in an unreadable directory (where undef indicates the file
>>is missing) - I'd like -r to return false (unreadable) in such
>>a case.

[snip]
>>ls returns 'Permission Denied' which is what I'd like -r to return.

>
> But this is easy; just check $! if the file test returns undef.


That's the key - I checked if $! eq 'Permission denied' after an undef
-r, that tells me if it failed due to an unreadable dir.

Thanks Steve - much appreciated!

Justin


 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      10-30-2003

Justin <(E-Mail Removed)> wrote:
> Steve Grazzini wrote:
> > Justin <(E-Mail Removed)> wrote:
> >
> >>I've noticed that the -r operator returns undef if used on a
> >>file in an unreadable directory (where undef indicates the file
> >>is missing) - I'd like -r to return false (unreadable) in such
> >>a case.

> [snip]
> >>ls returns 'Permission Denied' which is what I'd like -r to return.

> >
> > But this is easy; just check $! if the file test returns undef.

>
> That's the key - I checked if $! eq 'Permission denied' after an undef
> -r, that tells me if it failed due to an unreadable dir.


Better would be to use Errno (or perl -MErrno) and check $!{EACCES}.

Ben

--
Musica Dei donum optimi, trahit homines, trahit deos. |
Musica truces molit animos, tristesque mentes erigit. | (E-Mail Removed)
Musica vel ipsas arbores et horridas movet feras. |
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
jar file from Eclipse at the root of the jar file rather than nestedin directories wong_powah@yahoo.ca Java 0 03-27-2008 06:26 PM
Force browser to open a file (rather than save/open/cancel) tiewknvc9 Java 12 02-19-2007 08:21 PM
Main Advantages i get when i do a stand alone app in .Net rather than in VB. Punya Narra ASP .Net 5 02-17-2004 12:02 PM
Greater than operator Vs Equal to operator : Which one is efficient? Vivek Mandava C Programming 28 09-11-2003 10:43 AM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments