Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^5: Renaming files in directory

by afoken (Chancellor)
on Jan 12, 2018 at 12:27 UTC ( [id://1207154]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Renaming files in directory
in thread Renaming files in directory

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". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1207154]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-24 07:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found