Hello perl newbie,

Thursday, August 09, 2001, perl newbie <[EMAIL PROTECTED]> wrote:


pn> I am using the following code to do two things:

pn> 1) create a unique directory name using @ARGV, and
pn> 2) recursively copy the contents of a source directory
pn> into the target directories.

[..]

pn> I would appreciate help with the following questions:

pn> 1) what am I possibly doing wrong ?
see below.

pn> 2) Is there a problem with my usage of the foreach
pn> loop in this case.
no.
pn> 3) Is there a better way to accomplish what I am doing
pn> ?
depends of your needs. may be you want File::Copy?

pn> PS:  Here is the source code:

pn> --------- Perl Source Code ------------

pn> #!/usr/bin/perl -w
pn> use strict;
pn> use POSIX;

pn> ##########################
pn> ###    MAIN PROGRAM    ###
pn> ##########################


pn> # set environment variables.

pn> $ENV{WORK_AREA} = "/tmp/regr_tests";

$ENV{'WORK_AREA'} = "/tmp/regr_tests";

pn> # forward declarations, required due to the use
pn> # of the "strict" pragma.

pn> # variable declarations
pn> my $dir_name;
pn> my $cal_date = POSIX::strftime("%m%d%Y",localtime());
pn> my $proc_id = getpid();

pn> # function declarations
pn> sub myMkDir;

pn> # loop through the command line arguments and
pn> # check for the existence of the named directories.
pn> # Create new directories ONLY if they are not already
pn> # existing.

pn>    foreach $dir_name (@ARGV){

pn> # create unique directory names per each run.
pn>    $dir_name= "$dir_name" . "_". "$cal_date" . "_" .
pn> "$proc_id" ;
if you really need random number, do not use process id.
look at perldoc -f rand.
pn>         myMkDir "$ENV{WORK_AREA}/$dir_name";
myMkDir "$ENV{'WORK_AREA'}/$dir_name";

pn> # recursively copy files from DB area to TEST area.
pn> system("/usr/bin/cp -rf \$HOME/samples \$ENV{WORK_AREA}/\$dir_name");

you should not escape $ sign on variables; try
print "/usr/bin/cp -rf \$HOME/samples \$ENV{WORK_AREA}/\$dir_name";
to feel difference.

and your $HOME undefined. may be you mean
system("/usr/bin/cp -rf $ENV{'HOME'}/samples $ENV{'WORK_AREA'}/$dir_name");

pn> }

pn> ### end of main

pn> ##########################
pn> ####  Sub myMkDir     ####
pn> ##########################

pn> sub myMkDir {
pn>   my $dir = shift;

pn>   if (-e $dir) {
pn>     if (!(-d $dir)) {
pn>       # Exist, but not a directory, it's a file
pn>       die "Error: can't create directory $dir, a file
pn> of that name already exist
pn> s\n"; }
pn>      else { die "Error: can't create directory $dir, a
pn> directory of that name al
pn> ready exists\n"; }
pn>   } else {
pn>     mkdir $dir, 0755;
pn>   }
pn> }


Best wishes,
 Maxim                            mailto:[EMAIL PROTECTED]



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to