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
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.