Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: File existance check failure

by toolic (Bishop)
on Nov 10, 2012 at 20:59 UTC ( #1003276=note: print w/replies, xml ) Need Help??

in reply to File existance check failure

There is nothing in the file test documentation to indicate that it supports wilcard expansion. You can use glob. Something like:
use warnings; use strict; for (glob '*.a') { if (-f $_) { print "file $_ exists\n"; } }

UPDATE: Fixed typo (thanks 2teez)

Replies are listed 'Best First'.
Re^2: File existance check failure
by Festus Hagen (Novice) on Nov 11, 2012 at 01:34 UTC
    Much Thanks.

    With that I brewed the following:

    sub fexist($) { my $str = shift; for (glob qq("$str")) { return 1 if -e "$_"; } return 0; }

    However I have use for the Link, Directory, File result so:

    sub fexist($) { my $str = shift; for (glob qq("$str")) { return 3 if -l "$_"; return 2 if -d "$_"; return 1 if -f "$_"; } return 0; }

    Critiques ??
    Comments ??
    Suggestions ??
    Improvements ??


    fh : )_~

      Critiques ??

      As a general rule, avoid subroutine prototypes unless you have good reason for them. (See, e.g., Far More than Everything You've Ever Wanted to Know about Prototypes in Perl -- by Tom Christiansen.) With the prototype removed, and employing some Perl idioms, sub fexist can be simplified to:

      sub fexist { for (glob $_[0]) { return 3 if -l; return 2 if -d; return 1 if -f; } return 0; }
      Improvements ??

      Since the aim is to cleanup unwanted files, perhaps it would be better to integrate sub fexist with the cleanup code? Something like this:

      use v5.14; ... sub cleanup { for (glob $_[0]) { cleanup_link($_) when -l; cleanup_dir ($_) when -d; cleanup_file($_) when -f; } }

      Hope that helps,

      Athanasius <°(((><contra mundum


        Awakens the the spacy name beast!

        Proto's ... geez my Cisims bite again. :)

        fh : )_~

      One more small detail, which probably won't be significant unless you happen to be scanning very large quantities of files, links and/or directories: you can use the special operand "_" (the underscore character) on the file-test operators, in order to use the file stat information from the previous stat call (so you don't do repeated stat calls on the same file). Adapting the most recent suggestion from Athanasius:
      sub cleanup { for (glob $_[0]) { cleanup_link($_) when -l; cleanup_dir ($_) when -d _; cleanup_file($_) when -f _; } }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1003276]
[marto]: It's not worse, just different. Different shells, different tools. Different ways
[choroba]: Perl to the rescue!
[choroba]: yes, it's documented in man bash. You need to echo 'Joe'\''s home'
[marto]: That's on my list of t-shirts to sell "Perl to the rescue", " I hate Oracle"....
[choroba]: Will there be a discount if I buy both?

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2017-11-24 10:27 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (346 votes). Check out past polls.