JimboMcDeans has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl Monks,

Long time visitor of your forums, first time poster. I still consider myself semi-new to Perl, have only been at it for a few months. I am completely stumped on this error message: Can't do inplace edit: . is not a regular file at Copy (2) of Parsing.pl line 9. I know what causes the error, but for the life of me I cannot discover it in my own code. I am writing something very simple. Here is the code in question:
chdir("C:\\some_directory") or die "$!"; opendir (DIR, ".") or die "$!"; my @files = grep{"\\*?.txt"} readdir DIR; close DIR; local @ARGV = @files; while (<>) { if (/Words: /) { $somenumber = "print $_"; $substringoffset = substr($somenumber, 26); open MYFILE, ">>","C:\\some_directory\\Results.csv"; print MYFILE $substringoffset; } }
Essentially I am looking at multiple text files, looking for one spot where the text file has "Words" and then taking an offset of 26 and saving what comes after the 26th position. The error I get does not affect the overall functionally of my script. It does however generate annoying and unprofessional errors that unfortunately take up my cmd screen. Any ideas?

Replies are listed 'Best First'.
Re: Strange Error
by kennethk (Abbot) on Jun 07, 2011 at 22:33 UTC
    The issue arises because your grep is always true - you need to swap the string "\\*?.txt" to the regular expression /\\*?.txt/ so you filter out the directories . and .., as well as non-.txt files.

    There are some stylistic modifications I could suggest, but this would fix the immediate issue.

      Use of the  glob function as suggested by kejohm also gets around the problem with the regex  /\\*?.txt/ that the  . (dot) is a metacharacter that matches anything (except, in the absence of the  /s modifier, a newline). To match only a literal  '.' character, the regex  /\\*?\.txt/ would be better.

      What would you suggest? Also thank you for the help!
        If I wrote the code, it might look more like (IMHO, YMMV):

        #!perl -w use strict; my $dir = 'C:\\some_directory'; my $out_file = 'Results.csv'; opendir my $dh, $dir or die "$!"; my @files = grep {/\.txt$/} readdir $dh; for my $file (@files) { open my $in, '<', "$dir/$file" or die "$file: $!"; while (<$in>) { if (/Works: /) { open my $out, '>>', "$dir/$out_file" or die "$out_file: $! +"; print $out substr($_, 20) } } }

        Elements of the above of note:

        1. I've added warnings and strict since I have found it catches a lot of would-be mistakes. See Use strict warnings and diagnostics or die (or The strictures, according to Seuss).

        2. I abstracted string constants to the head of the script, to avoid 1-off typos and make strict to more work for me.

        3. I swapped from barewords to indirect file handles. These have a number of benefits, including automatically closing themselves when they go out of scope and again leveraging strict to protect against 1-off typos. Note that since barewords are globals, your output file is always open once the first edit occurs, whereas mine is closed after every iteration. See Indirect Filehandles for more details.

        4. I swapped to a regular expression that literally translates as 'ends with .txt'. Obviously this may not be your intended file set, but seemed to be what you meant.

        5. I moved from a local @ARGV = @files; while (<>) { construct to explicit nested loops with an explicit open. This admittedly increases the character count; however it improves the legibility, the quality of the error messages and avoids a potential bug. As <> will start processing STDIN if fed an empty @ARGV (see I/O Operators), your script would strangely hang if your target directory had no .txt files.

        6. Lastly, I optimized away some of your inner-loop variables since they are never really used.

        There a couple possible variations on the above themes. For example, in practice instead of the grep I'd probably skip undesirable files with Loop Control. I'd probably also scope the output file outside the loop, so I only open it once. But, to my eye, those are minor.

Re: Strange Error
by kejohm (Hermit) on Jun 07, 2011 at 22:48 UTC

    You could try the glob() function instead of filtering the contents of the directory yourself, like this:

    my @files = glob("*.txt");