Hello Everyone,

I occasionally like to download perl programs off the web to see how they work. And if there are any bugs I try to report back to the original author.

This code was found verbatim (except for the file name) in two different files.

my $conf = which_conf($configuration_file); if (-f $conf) { my @conf_args; open(my $conffile, '<', "$conf") or warn "$P: Can't find a readable $configuration_file file $! +\n"; while (<$conffile>) { my $line = $_; $line =~ s/\s*\n?$//g; $line =~ s/^\s*//g; $line =~ s/\s+/ /g; next if ($line =~ m/^\s*#/); next if ($line =~ m/^\s*$/); my @words = split(" ", $line); foreach my $word (@words) { last if ($word =~ m/^#/); push (@conf_args, $word); } } close($conffile); unshift(@ARGV, @conf_args) if @conf_args; }

my $conf = which_conf($configuration_file); if (-f $conf) { my @conf_args; open(my $conffile, '<', "$conf") or warn "$P: Can't find a readable $configuration_file file $! +\n";

It looks like there is a race condition between the file test and the file open.

The file test does not print a message if the file is not found but the file open does.

If the file cannot be opened a message is printed and then another message is printed when readline tries to read from an invalid file handle.

The file name is copied to a string.


while (<$conffile>) { my $line = $_;

Data is assigned to $_ and then copied to $line but $_ is never used inside the loop.


$line =~ s/\s*\n?$//g;

This is the line that gobsmacked me. I ran some tests with Benchmark and re 'debug' compared to the FAQ answer "s/\s+$//;" and it turns out that this is a good example of what not to do.

This substitution was found in five places in one file and two places in the other file.


$line =~ s/^\s*//g;

Not as bad as the previous line but still not as good as the FAQ answer.


$line =~ s/\s+/ /g;

Correct code, nothing to see here.


next if ($line =~ m/^\s*#/); next if ($line =~ m/^\s*$/);

Why check for whitespace after you've just removed it?


my @words = split(" ", $line); foreach my $word (@words) { last if ($word =~ m/^#/); push (@conf_args, $word); }

This code is where the work is done and obviates the need for the previous five lines. split(" ", $line) removes all whitespace. foreach will not loop if @words is empty. $word =~ m/^#/ will skip to the next line if a '#' comment is found.


close($conffile);

Correct code, nothing to see here.


unshift(@ARGV, @conf_args) if @conf_args;

The conditional is not really required.


Where I found this code:


In reply to Critique of some perl code. by jwkrahn

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.