doesn't do what it is supposed to do.

So, what does it instead?


I see a few problems in the code:

#!bin/perl

No problem for Perl. In fact, Perl does not even need this line. But your operating system may need it. The path to the perl executable should be absolute, not relative, so a leading slash is missing between #! and bin/perl. And the path looks wrong, I've never seen Perl installed to /bin/perl. The system perl is usually /usr/bin/perl, administrators tend to install a (probably newer) perl to /usr/local/bin/perl. Sometimes, the latter is just a symlink to the former.

opendir(DIR,$directory);

No error check. Use opendir(DIR,$directory) or die "Could not open directory $directory: $!";

Also, bareword handle instead of scalar. This does not hurt, but is considered bad style. See the example in readdir.

my $a = @oldfiles; my $b = @newfiles; if ( $a ne $b ) { print "the numbers are not equal\n" and die; }

Don't use variables named $a or $b, they are reserved for sort and because of this, they are excluded from some of Perl's error checks.

You are comparing numbers using a string compare operator (ne). This may accidentally work, but you really want to use a numeric operator (!=). See also Re^3: xs modifying passed value.

You are writing an error message to STDOUT, then let die write a non-helpful default error message to STDERR. Error messages don't belong to STDOUT. Just pass the error message to die.

If print fails and therefore returns false, die won't be called and your code will continue even if you have detected a fatal error. Again, just pass the error message to die and get rid of print and and here.

The error message is not very useful. What numbers? And why should they be equal? A user should not be forced to read the source code to understand error messages. Try die "Found $a files in $directory, but expected $b. Died"; to see the difference.

for (my $i = 0; $i < $a; $i++) { rename $oldfiles[$i],$newfiles[$i]; }

Rename lacks an error check. Like for opendir, append or die with a reasonable error message: rename $oldfiles[$i],$newfiles[$i] or die "Could not rename $oldfiles[$i] to $newfiles[$i]: $!";.


If you don't want to manually check each and every I/O operation for errors, add the line use autodie; to your code after having read autodie.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re^5: Renaming files in directory by afoken
in thread Renaming files in directory by WisDomSeeKer34

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.