I have a series of spreadsheets and a large directory structure to move them into. I have a list of the filenames for each sheet, along with the final residing directory. Rather than move these all manually, I decided to write a quick script to do this.

The script worked fine, and moved all of the files into the right directories (and created the directories if they didn't exist) but along the way I ran into some errors that I can't explain. Specifically, some null lines seem to have infested my DATA section...

After changing the names to protect the innocent, this is the script I'm using:

#!/usr/bin/perl use strict; use warnings; my $base_dir = "C:/Documents/perl"; while (my $line = <DATA>) { chomp $line; next if $line =~ /^\s*\#/; # next if not $line; my ($filename, $filepath) = split '\t', $line; if (not -d "$base_dir/$filepath") { system "c:/cygwin/bin/mkdir -p $base_dir/$filepath"; print "Made $base_dir/$filepath\n"; } if (not $filename) { warn "Filename is blank, filepath is [$filepath], line is [$li +ne]\n"; } else { system "mv $base_dir/$filename.xls $base_dir/$filepath"; print "Moved $base_dir/$filename.xls to $base_dir/$filepath\n" +; } print "\n"; } __DATA__ filename1 some/file/path filename2 some/other/file/path filename3 yet/another/file/path/oooh/this/one/is/long
The script produces the following output:
Made C:/Documents/perl/some/file/path Moved C:/Documents/perl/filename1.xls to C:/Documents/perl/some/file/p +ath Use of uninitialized value in concatenation (.) or string at C:\Docume +nts\perl\try.pl line 15, <DATA> line 2. Use of uninitialized value in concatenation (.) or string at C:\Docume +nts\perl\try.pl line 21, <DATA> line 2. Filename is blank, filepath is [], line is [] Made C:/Documents/perl/some/other/file/path Moved C:/Documents/perl/filename2.xls to C:/Documents/perl/some/other/ +file/path Use of uninitialized value in concatenation (.) or string at C:\Docume +nts\perl\try.pl line 15, <DATA> line 4. Use of uninitialized value in concatenation (.) or string at C:\Docume +nts\perl\try.pl line 21, <DATA> line 4. Filename is blank, filepath is [], line is [] Made C:/Documents/perl/yet/another/file/path/oooh/this/one/is/long Moved C:/Documents/perl/filename3.xls to C:/Documents/perl/yet/another +/file/path/oooh/this/one/is/long

Note that I can easily fix this behavior by uncommenting the next if not $line part. The question is not how to avoid this error, the question is why it's happening in the first place.

All of the lines are read, and I don't see anything in the script that would cause me to read another line. I don't recall seeing this sort of behavior before, but I do normally include the check against empty lines.

What simple thing am I overlooking here? I SuperSearched for "__DATA__ null" as well as "__DATA__ empty" with no promising results. I've spent too much time trying to diagnose this problem, though, so I'm throwing in the towel and asking for ideas or information.

(by the way, "This is perl, v5.8.7 built for MSWin32-x86-multi-thread")

Update: I don't see the same behavior on my Mac (running OS X 10.4.1). "This is perl, v5.8.6 built for darwin-thread-multi-2level" So I'm totally baffled.


In reply to odd behavior with DATA section by Nkuvu

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.