I'm trying to loop through all files in a directory, read each file into a varible (@config) and then perform regex operations and spit out the output to a file. I'm having a problem reading the files into the variable. I'm getting "Can't open 'Filename.txt': No such file or directory at ./find.pl line 14", but there are files in the directory.
#!/usr/bin/perl -w # use warnings; use strict; opendir(DH, "Configs/"); my @files = readdir(DH); closedir(DH); foreach my $file (@files) { # Read config from .txt files in Dir. open my $fh, "<", $file or die "Can't open '$file': $!"; my @config = <$fh>; close $fh; chomp @config; my $configref = \@config; # Extract names from config. my @names = map { /^name \d+\.\d+\.\d+\.\d+ ([A-Za-z0- +9-_]+)$/ ? $1 : () } @{ $configref }; # Remove names from config. @config = grep { $_ !~ /^name / } @config; # Find unused name references. foreach my $name (@names) { if (!grep { $_ =~ /$name/ } @config) { print "name $name unused.\n"; } } # Extract objects from config. my @objects = map { /^(object|object-group) (network|s +ervice) ([A-Za-z0-9-_]+)$/ ? $3 : () } @{ $configref }; # Remove objects from config. @config = grep { $_ !~ /^(object|object-group) / } @co +nfig; # Find unused object references. foreach my $object (@objects) { if (!grep { $_ =~ /$object/ } @config) { print "object $object unused.\n"; } } }

In reply to Trouble opening and reading file within loop by H0tc@xe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.