Perl newb in need of help with script.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David K. Worman

    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
    ackhayman@hotma il.com
  • Jürgen Exner

    #2
    Re: Perl newb in need of help with script.

    David K. Worman wrote:[color=blue]
    > 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[/color]

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

    Good!
    [color=blue]
    > while (<UNAMES>) {
    > $src = "template\.doc" ;[/color]

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

    chomp;
    [color=blue]
    > ($dest = $_) =~ s/\w*/$_\.doc/;[/color]

    Otherwise you will be creating files that are named like "MyName\n.d oc"
    [color=blue]
    > system("cp $src $dest");[/color]

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

    jue


    Comment

    • David K. Worman

      #3
      Re: Perl newb in need of help with script.

      Thanks! Got it working now. Appreciate the help =)

      --
      dkw


      On Thu, 18 Sep 2003, wrote:
      [color=blue]
      >
      >
      > David K. Worman wrote:[color=green]
      > > 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[/color]
      >
      > Good.
      > You should also enable strictures:
      > use strict;
      >[color=green]
      > > $UNAMES = "unames.txt ";
      > > open(UNAMES) || die "Can't open $UNAMES: $!n";[/color]
      >
      > Good!
      >[color=green]
      > > while (<UNAMES>) {
      > > $src = "template\.doc" ;[/color]
      >
      > Your problem is that each line contains a trailing newline at this point.
      > You want to remove this using
      >
      > chomp;
      >[color=green]
      > > ($dest = $_) =~ s/\w*/$_\.doc/;[/color]
      >
      > Otherwise you will be creating files that are named like "MyName\n.d oc"
      >[color=green]
      > > system("cp $src $dest");[/color]
      >
      > No need to shell out a system process for a simple copy.
      > Just use the File::Copy module.
      >
      > jue
      >
      >
      >[/color]

      Comment

      Working...