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

Hi, I'm trying to run a perl script that is meant to extract a certain section from numerous text documents. The only thing the script requires of me is to input the read and write directories. In Windows it worked like a charm, though took several minutes to run on a relatively small batch (150) of text files. Later I will be automating this process on a cluster, and need to have it working in a Unix environment. When I do the same process I did on my Windows box on my Mac or on the cluster, perl instantaneously feeds me back the command prompt. On Windows, when I'd mess up I'd get an error message. Something like "No matches". For the time being I have set all permissions to 777 on the directories and files I am using (including the script). I've also ran a simple hello world script and received an output. I even tried using the dos2unix command on the text file to no avail. Below is the beginning of my script I am using:

#!/usr/bin/perl # Script to extract "item 7" from a 10-K report. # This will write the "good" part of the file to stdout, and will writ +e # a "schema string" on a single line to stderr. $dirtoget="E:\######"; $dirwrite="E:\#####"; opendir(IMD, $dirtoget) || die("Cannot open directory"); @thefiles= readdir(IMD); closedir(IMD); foreach $f (@thefiles) { unless ( ($f eq ".") || ($f eq "..") ) { $fr="$dirtoget$f"; open(FILEREAD, "< $fr"); $f=~s/.txt/.mda/g; $fw="$dirwrite$f"; #print $f1; open(FILEWRITE, "> $fw"); $x=""; while($line = <FILEREAD>) { $x .= $line; } # read the whole file into +one string close FILEREAD;

And yes, I am using forward slashes when switching to Unix directories. I've tried everything from single-quotes to starting with the root (~). I've also tried changing the shebang to env perl to no effect.

Also, I got it to the point where the code is running, but it is outputting: # This will write the "good" part of the: No such file or directory It would seem in the comment on line 5, it is trying to analyze "file" even thought it is commented out.

Update: turns out it was the end of the lines on the script. For some reason I forgot I received the text as an MS Word file (god only knows) and had to convert to txt from MS Word. When I used dos2unix command it got rid of all my line spacing all together for some odd reason. I had to reopen to original docx, save it as a text file and specify to end lines with lf. Thanks for all the time you took helping me today!

Replies are listed 'Best First'.
Re: Perl Script in Windows Works, but not in Unix
by kcott (Archbishop) on Jun 26, 2015 at 19:53 UTC

    G'day dobster936,

    Welcome to the Monastery.

    One potential problem is lines like

    $fr="$dirtoget$f"; ... $fw="$dirwrite$f";

    Do $fr and $fw evaluate to /some/dirname/somefilename or /some/dirnamesomefilename? Add print statements to find out. See the builtin File::Spec module to "portably perform operations on file names".

    Another problem is performing I/O without checking whether it worked. See open for examples of how to do this. Alternatively, use the autodie pragma and let Perl do it for you.

    And yet another issue is assuming everything, except "." and "..", is a normal file:

    unless ( ($f eq ".") || ($f eq "..") ) {

    See the file test operators. Given that unless block has no closing brace, you could just change those two lines to:

    next unless -f $f;

    Global (package) variables have all sorts of problems. Choose lexical variables and control their scope yourself. See "Private Variables via my()" for further discussion.

    Finally, as others have already indicated, use the strict and warnings pragmata. There's really no value in spending time tracking down problems in your code when Perl will do it for you.

    -- Ken

Re: Perl Script in Windows Works, but not in Unix
by stevieb (Canon) on Jun 26, 2015 at 18:56 UTC

    Add:

    use strict; use warnings;

    To the top of the file, then add in some print statements so see what's going on. After I add strict and warnings, look at the problems... particularly, note the last one... you're missing a closing curly brace (actually two, but after you fix one, strict will cough again).

    $ ./scr.pl Global symbol "$dirtoget" requires explicit package name at ./scr.pl l +ine 10. Global symbol "$dirwrite" requires explicit package name at ./scr.pl l +ine 11. Global symbol "$dirtoget" requires explicit package name at ./scr.pl l +ine 12. Global symbol "@thefiles" requires explicit package name at ./scr.pl l +ine 13. Global symbol "$f" requires explicit package name at ./scr.pl line 15. Global symbol "@thefiles" requires explicit package name at ./scr.pl l +ine 15. Global symbol "$f" requires explicit package name at ./scr.pl line 17. Global symbol "$f" requires explicit package name at ./scr.pl line 17. Global symbol "$fr" requires explicit package name at ./scr.pl line 19 +. Global symbol "$dirtoget" requires explicit package name at ./scr.pl l +ine 19. Global symbol "$f" requires explicit package name at ./scr.pl line 19. Global symbol "$fr" requires explicit package name at ./scr.pl line 20 +. Global symbol "$f" requires explicit package name at ./scr.pl line 21. Global symbol "$fw" requires explicit package name at ./scr.pl line 22 +. Global symbol "$dirwrite" requires explicit package name at ./scr.pl l +ine 22. Global symbol "$f" requires explicit package name at ./scr.pl line 22. Global symbol "$fw" requires explicit package name at ./scr.pl line 24 +. Global symbol "$x" requires explicit package name at ./scr.pl line 26. Global symbol "$line" requires explicit package name at ./scr.pl line +27. Global symbol "$x" requires explicit package name at ./scr.pl line 27. Global symbol "$line" requires explicit package name at ./scr.pl line +27. Missing right curly or square bracket at ./scr.pl line 28, at end of l +ine

    -stevieb

      I did not post the full script. If you're interested:

        Still, without strict and warnings, few people will pour through your code trying to figure out if a variable is getting clobbered where it shouldn't be or something along those lines. It's very difficult to troubleshoot code that way, and it's very inefficient.

        Now might be a good time to introduce you to the <readmore>and </readmore>tags.

        Nicely done with those <readmore></readmore>tags.  :-)

Re: Perl Script in Windows Works, but not in Unix
by jcklasseter (Sexton) on Jun 26, 2015 at 18:45 UTC
    In your '$dirto...' section, UNIX file directories are done like: usr/Documents/Data, not like usr\Documents\data as Windows uses. That might fix the problem.
      As in, go to wherever you're trying to call the directory from, type  pwd and then put that into the place instead.

      I realize this. I've been using:

      $dirtoget="/netscr/name/extraction/10K-txt/"; $dirwrite="/netscr/name/extraction/MDA/";

      Does this look fine?

        As in, go to wherever you're trying to call the directory from, type  pwd and then put that into the place instead.
Re: Perl Script in Windows Works, but not in Unix
by marinersk (Priest) on Jun 26, 2015 at 18:43 UTC

    I see an indent on the first line.

    1. Is that really in the file?
    2. Does it matter?

      Not actually in file. Fixing now in post.