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

I'm trying to check a directory for the existence of a specific file using pattern matching. If the file is there, it matches and executes the "Block" normally. However, if there is no file there, it should do something else.....instead it does nothing, I guess because there's no file to attempt to match the pattern to. Should I use 2 seperate if conditionals? 1. if a file is there & it matches do this, and then a seperate if conditional...if there are no files do this....I would surely like to do it with one if-else conditional.
opendir(DULS, 'D:\COLD\DULS'); chdir 'D:\COLD\DULS'; @files = glob ('cold*'); foreach $file (@files) { print CBDOWNLOAD "Cold Backup file exists for DULS for $FileName.\n" i +f ($file =~ m/^cold/); system ('awtrap -h RLGH -f DULS "DULSVASG001: Unable to download the +file."') if ($file !~ m/^cold/); }
Please drop some knowledge on a brotha....!!!

Replies are listed 'Best First'.
Re: File check w/no files
by VSarkiss (Monsignor) on Nov 29, 2001 at 04:50 UTC

    Heh, sometimes it seems Perl offers a few too many choices.

    To expand a bit on what my parsimonious brother chip has stated, I'd probably structure it like this:

    my @files = glob '*.cold'; # you are using strict, no? if (scalar(@files) == 0) { print "Nothing matched\n"; } else { foreach (@files) { if (/^cold/) { print CBDOWNLOAD "Cold Backup file exists for DULS for $Fi +leName.\n" } else { system ('awtrap -h RLGH -f DULS "DULSVASG001: Unable to d +ownload the +file."'); } } }

    HTH

      Thanx HTH, I'd been bangin my head on this one, til I saw the white meat. It worked perfectly. Brutha's in Perl.....
Re: File check w/no files
by chip (Curate) on Nov 29, 2001 at 04:34 UTC
    Probably all you need is to test @files in a separate if() statement.

    BTW, i fyou use glob(), there's no need for opendir().

        -- Chip Salzenberg, Free-Floating Agent of Chaos

Re: File check w/no files
by buckaduck (Chaplain) on Nov 29, 2001 at 21:16 UTC
    Now that you've got it working, here's a more fundamental question:

    Why are you bothering with the regex in the first place? Each $file in @files must already begin with "cold" because that's what you've globbed for. So in your code, the regex pattern always matches and the system() command will never run.

    You might mean something like this instead:

    chdir 'D:\COLD\DULS' or die "Can't chdir: $!"; my @files = glob ('cold*'); if (scalar @files) { foreach my $file (@files) { print CBDOWNLOAD "Cold Backup file exists for DULS for $FileName.\ +n" ... } } else { system ('awtrap -h RLGH -f DULS "DULSVASG001: Unable to download th +e file."'); }
    Unless I'm misunderstanding your intent, which is perfectly possible...

    buckaduck

Re: File check w/no files
by MZSanford (Curate) on Nov 29, 2001 at 16:12 UTC
    As a thought (untested code ahead):
    chdir 'D:\COLD\DULS'; my @all_files = glob('*'); my @cold_suff = grep(/^cold/,@all_files); my @hot_suff = grep(!/^cold/,@all_files); ## do stuff wth hot and cold running data streams.

    i had a memory leak once, and it ruined my favorite shirt.