tux242 has asked for the wisdom of the Perl Monks concerning the following question:

I am having a hard time trying to get my hash of files to copy to work with the code below I actually wanted for the path and the files names to be dumbed down to variables if possible. Thanks.

#! /usr/bin/perl use strict; use warnings; my @FILES = ('file1',' file2'); `cp /home/wbill/lazarus/@FILES .`;

Replies are listed 'Best First'.
Re: Hashes or arrays in the command line
by hardburn (Abbot) on Nov 20, 2003 at 18:26 UTC

    You aren't using a hash, but a regular array. What you probably want is in File::Copy.

    use File::Copy; my @FILES = ('file1', 'file2'); copy( "/home/wbill/lazarus/$_", '.' ) foreach (@FILES);

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

Re: Hashes or arrays in the command line
by EvdB (Deacon) on Nov 20, 2003 at 18:25 UTC
    The code you posted will try to execute:
    cp /home/wbill/lazarus/file1 file2 .
    You probably want something like:
    #! /usr/bin/perl use strict; use warnings; my @FILES = ('file1',' file2'); my $path = "/home/wbill/lazarus"; foreach my $this_file ( @FILES ) { `cp $path/$this_file .`; }
    What the foreach does is take every item in the array @FILES and places it in the variable $this_file - which is then used in the backticks

    More likely though you should consider using a module such as File::Copy rather than use backticks - it makes the code less platform dependent.

    --tidiness is the memory loss of environmental mnemonics

      You probably also want to change
      my @FILES = ('file1',' file2');
      to:
      my @FILES = ('file1', 'file2');
      note the removal of the space in front of file2.

      --tidiness is the memory loss of environmental mnemonics

      Ok I goofed the file is static like /etc/passwd and I am taking it from a backup sub directory and the server is the variable that changes not the file itself, so it should be like:

      #! /usr/bin/perl use strict; use warnings; my @SERVERS = ('sneezy','sleepy','dopey','grumpy'); my $path = "/home/backup/"; foreach my $this_server ( @SERVERS ) { `cp $path/$this_server/etc/passwd .`; }

      Does this lokk right??

        It looks fine.

        If you want to do a sanity check try doing this:

        foreach my $this_server ( @SERVERS ) { my $command = "cp $path/$this_server/etc/passwd ."; warn "Will execute '$command'\n"; # system ($command); # uncomment when ready. }

        --tidiness is the memory loss of environmental mnemonics

Re: Hashes or arrays in the command line
by Limbic~Region (Chancellor) on Nov 20, 2003 at 18:26 UTC
    tux242,
    You mean something like this:
    #!/usr/bin/perl -w use strict; use File::Copy; my @files = qw( file1 file2 ); my $base = '/home/wbill/lazarus/'; for my $file_name (@files) { my $file = $base . $file_name; copy( $file , "./$file_name" ) or warn "Unable to copy $file_name +: $!"; }
    Cheers - L~R