469,645 Members | 1,941 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

Perl newb in need of help with script.

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.

--
dkw
ac*******@hotmail.com
Jul 19 '05 #1
2 1907
David K. Worman wrote:
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
Jul 19 '05 #2
Thanks! Got it working now. Appreciate the help =)

--
dkw
On Thu, 18 Sep 2003, wrote:


David K. Worman wrote:
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


Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by sam | last post: by
9 posts views Thread by Martin Foster | last post: by
6 posts views Thread by Richard Trahan | last post: by
reply views Thread by Kirt Loki Dankmyer | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.