help to understand a broken script

C

Chad

I am still trying to learn perl. I know there are basic rename scripts
out there, but I am trying to make one as a learning tool.

The script should take a directory from the prompt and make a renamed
copy in the same directory replacing spaces with underscores. It seems
to do everything except the actual file copy.

It's not that I need this program. Instead I would be gratefull for
any insight and undestanding as to why it does not work.

Thank you
Chad
---

#!/usr/bin/perl
use File::Copy;

$output = `cmd 2>&1`; #trying to see errors


$olddirname = $ARGV[0];
$newdirname = "$olddirname.new";

opendir(DirNameold,"$olddirname") or die "could not open
directory:$olddirname";

while (defined($file = readdir DirNameold )) {

$newfilename = $file;
$newfilename =~ tr/ /_/;
copy("$file","$newfilename") unless $file eq $newfilename;

};

---
 
J

Jürgen Exner

Chad said:
I am still trying to learn perl. I know there are basic rename scripts
out there, but I am trying to make one as a learning tool.

The script should take a directory from the prompt and make a renamed
copy in the same directory replacing spaces with underscores. It seems
to do everything except the actual file copy.

It's not that I need this program. Instead I would be gratefull for
any insight and undestanding as to why it does not work.

Thank you
Chad

You should always ask for as much help from perl as possible.
Right at the beginning you are missing a

use strict;
use warnings;
use File::Copy;
Good!

[...]
copy("$file","$newfilename") unless $file eq $newfilename;

And here you are missing the fail condition.
Besides, that is a useless use of quotes.

{copy($file, $newfilename) or die "Can't copy $file to $newfilename
because $!"}
unless $file eq $newfilename;

I didn't analyse you program any further because it is humiliating to ask a
human to do work that a machine can do better and faster.
The actual error message should give you a hint about what is wrong.

jue
 
J

JSV

Chad said:
I am still trying to learn perl. I know there are basic rename scripts
out there, but I am trying to make one as a learning tool.
It's not that I need this program. Instead I would be gratefull for
any insight and undestanding as to why it does not work.
copy("$file","$newfilename") unless $file eq $newfilename;

I guess, you are executing this copy command, when not present inside the
directory passed in $ARGV[0]. Also trap error by using die "$!" after the
copy.
 
T

Tassilo v. Parseval

Also sprach Jürgen Exner:
Chad wrote:

And here you are missing the fail condition.
Besides, that is a useless use of quotes.

{copy($file, $newfilename) or die "Can't copy $file to $newfilename
because $!"}
unless $file eq $newfilename;

That's a syntax error. You can't (yet) use statement modifiers on bare
blocks. You don't even need a block. Simply

copy($file, $newfilename)
or die "Can't copy $file to $newfilename because $!"
unless $file eq $newfilename;

will do.

Tassilo
 
T

Tad McClellan

Chad said:
I would be gratefull for
any insight and undestanding as to why it does not work.


Reading the documentation for the functions that you use often
provides insights. :)

opendir(DirNameold,"$olddirname") or die "could not open
^ ^
^ ^
directory:$olddirname";


What's wrong with always quoting "$vars"?

If it fails, wouldn't you want to know _why_ it failed?

If so, then include $! in your diagnostic message.

while (defined($file = readdir DirNameold )) {


perldoc -f readdir

... you'd better prepend the directory in question

copy("$file","$newfilename") unless $file eq $newfilename;


copy("$olddirname/$file", "$newdirname/$newfilename") ...
 
C

Chad

Thanks to everyone who contributed. it works now.
all the advice made the difference. The code is still ugly, but it
works and I can work on more elegant code as I progress.

Once again
Thank you

Chad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top