Re: Splitting and comparing file names



Jake wrote:
On Dec 14, 9:42 am, "Paul Lalli" <mri...@xxxxxxxxx> wrote:
hostname#val1#val2#val3.load

Are those # symbols actually part of the file name, or are you using
them to demark the different values? Giving actual sample data along
with the general format would really be much preferred...

That is a pretty accurate representation of the data. The filename is
a contatonation of the hostname and uptime values for that host with
the # as the value delimeter.

It's "accurate" only because you know what the actual data looks like
:-). Just based on what you typed though, no one else knows for sure
what the data is. This description you just typed helps, however.

chdir ("/store/admin/tools/load");
Why? Changing the current local directory is not necessary for opening
that directory for reading.

Thanks for pointing that out. I actually had copied that logic from my
previous script that required it because of a write to the directory.

chdir() isn't necessarily required for that either, but without knowing
exactly what your program did, it's difficult to say one way or the
other.

For some reason opendir doesn't set the path for when you use
open...unless I'm missing something there as well.

No it doesn't. Nor is it supposed to. opendir() opens a directory.
chdir() changes the current working directory. Two completely distinct
functions.

Are you aware that open() can take a path, not just a filename?
open my $fh, '<', "$dir/$file" or die "Couldn't open $dir/$file: $!";

opendir (DIR, "/store/admin/tools/load")
You should use lexical file and directory handles these days, not
global barewords.

I'm afraid I'm not savvy enough on the lingo to really understand what
that means. My apologies...

DIR is global. Anywhere in your entire program you use DIR, you're
using the same directory handle. It's also a bareword (ie, there's no
$ before it), rendering it not subject to strictures. This invites
errors caused by typos, and collision when you accidentally use the
same directory handle in two functions 500 lines apart, but that get
run immediately near each other. For example:

opendir DIR, $dir1 or die "..."
while (my $file = readdir (DIR)){
process($file);
}
#500 lines later....
sub process {
my $current = shift;
#see if that file name exists in this other directory:
opendir DIR, $dir2 or die "...";
while (my $file = readdir DIR) {
#compare $file to $current
}
}

As soon as the first invocation of process() opened DIR, the directory
that had already been opened by DIR is closed, thus causing the first
while loop to only read in the first file from $dir1, and then
terminating. Good luck spotting that bug without pulling out your
hair....

If each of those opendir() calls had replaced the global baredword (ie,
`DIR`) with a lexical variable (ie, `my $DIR`), the nature of lexicals
would have prevented the two from colliding. They would be two
separate and independent directory handles.

|| die "Directory not found or unable to be opened. $!\n";
Don't guess as to the problem in your hard-coded error message. That's
what $! is there for.
opendir my $DIR, '/store/admin/tools/load' or die "Could not open
directory: $!";

I think this is a general case where I dislike most error messages I
see and wanted to add more detail.

Adding detail is fine.... but you're making an assumption. There could
be many reasons that opendir failed, not at all related to the
directory not being found. Error messages that lie are a huge
detriment to productive debugging.

Thank you for all the information. I'm sure it will help make me a bit
better at writing perl in the future.

You're welcome. Glad to help.

Paul Lalli

.



Relevant Pages

  • Re: Auto die on failed sys calls?
    ... > Is there any way to instruct perl to die whenever a system call ... Note also that open and opendir belong in a different category than ... backticks and system. ...
    (comp.lang.perl.misc)
  • Re: Using a Perl script in AIX / Unix
    ... use warnings; ... opendir my $dh, $dir or die $!; ... why not put both tests in the first grep: ...
    (perl.beginners)
  • Re: calculate date 4 days ago
    ... Quoth Paul Lalli: ... BZZT! ... opendir my $DIR, $dir or die ...; ...
    (comp.lang.perl.misc)
  • Re: trapping errors
    ... > I know the die can abort on errors but not sure how to trap the error it ... You can use eval to trap errors in your programs, but in this case, I ... here's where you test your opendir. ...
    (comp.lang.perl.misc)