Hi Yakup,
Glad to hear your code is doing what you want. I do have to admit I found the logic a bit hard to follow, but that isn't necessarily a problem. I would however recommend you run the code through enough test cases, just to make sure.
I just wanted to point out a few potential improvements:
- Using FindBin to locate the directory that the Perl script is located in is much more reliable than dirname $0.
- Using glob is easier than opendir+readdir (with the caveat that glob does things a little bit differently, for example it ignores files whose name begin with a dot by default; see File::Glob for all the details); alternatively there are modules like Path::Class or Path::Tiny to help.
- This is just a stylistic thing, but personally I wouldn't make the regex as complex. I might write something like next if /^\s*#/; if (/--hostname=/) ...
- Smart match (~~) has been placed into experimental status and its implementation might change in the future, so in this case something like List::Util's any is better for now. Or, if all you're using @predefined for is to check whether certain strings have been seen before, a hash would be much better.
- The code (split /\./, $kickstart)[0] will only return the first part of $kickstart up to the first dot anywhere in the string, so it will not do what you want (strip the .ks suffix) for strings like "some.file.name.ks" or "/some.path/some.file.ks". I would recommend a regex, or the all-out solution is File::Basename's fileparse.
- Also, I'd recommend the three-argument form of open, and to limit the scope of your variable declarations to where they are needed.
If I implement those suggestions, I get:
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
use File::Basename qw/fileparse/;
use List::Util qw/any/;
my $lab_root = $FindBin::Bin;
my @kickstarts = glob "$lab_root/*.ks";
my @bsname;
my @predefined;
for my $kickstart (@kickstarts) {
open my $fh, '<', $kickstart or die "Cannot open $kickstart: $!";
while (<$fh>) {
chomp;
next if /^\s*#/;
if (/--hostname=/) {
my @fields = split /[=\s]/;
push @bsname, $fields[1];
push @predefined, $kickstart;
}
}
close $fh;
next if any {$kickstart eq $_} @predefined;
my $hostname = fileparse($kickstart, qr/\.ks$/i);
push @bsname, $hostname;
}
Note that in this code compared to yours, the filenames now all have their pathnames prefixed. Since I don't know what the rest of your code does, this may or may not be a problem for you. If it is, you could for example use the same fileparse function to get only the filename.
Hope this helps, -- Hauke D | [reply] [d/l] [select] |
| [reply] |