Perl newb in need of help with script.

D

David K. Worman

So this is probably a simple thing I'm missing, but then I just started
working with perl tonight - and of course I bought the Camel book, and
conveniently left it at work on my desk where it does me no good.

What I'm trying to do is this; I have a single document (template.doc)
that I need a copy of for ~50 people currently, and rather than just
copying it 50 times and renaming them I decided to play with perl instead.

This is what I've come up with so far, going from memory of what I read in
the Camel book thus far.


#!/usr/bin/perl -w

$UNAMES = "unames.txt";
open(UNAMES) || die "Can't open $UNAMES: $!n";

while (<UNAMES>) {
$src = "template\.doc";
($dest = $_) =~ s/\w*/$_\.doc/;
system("cp $src $dest");
}
close(UNAMES);
die();

----

The file 'unames.txt' is just a \n delimited text file like so:

foo
bar
foo_bar

The result I get when running the script is three copies of template.doc
(this is good!) named foo, bar, and foo_bar (this is bad!) instead of
foo.doc, etc.

Can anyone point out what I'm sure is a simple mistake? Thanks in
advance.
 
J

Jürgen Exner

David said:
So this is probably a simple thing I'm missing, but then I just
started working with perl tonight - and of course I bought the Camel
book, and conveniently left it at work on my desk where it does me no
good.

What I'm trying to do is this; I have a single document
(template.doc) that I need a copy of for ~50 people currently, and
rather than just copying it 50 times and renaming them I decided to
play with perl instead.

This is what I've come up with so far, going from memory of what I
read in the Camel book thus far.


#!/usr/bin/perl -w

Good.
You should also enable strictures:
use strict;
$UNAMES = "unames.txt";
open(UNAMES) || die "Can't open $UNAMES: $!n";
Good!

while (<UNAMES>) {
$src = "template\.doc";

Your problem is that each line contains a trailing newline at this point.
You want to remove this using

chomp;
($dest = $_) =~ s/\w*/$_\.doc/;

Otherwise you will be creating files that are named like "MyName\n.doc"
system("cp $src $dest");

No need to shell out a system process for a simple copy.
Just use the File::Copy module.

jue
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top