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

Dear Monks, I'm seeking your wisdom. I'm facing with two issues. 1.- I'm passing a variable from a sub routine call main () to another one. The variable is call $all_directories. This variable has a list of paths that I will use to search .htm files for example:
c:\inetpub\test1 c:\inetpub\test2 c:\inetpub\test3
Every path is different than the other, so I'm trying to pass it one by one to search for .htm files in each different directory. I get an error "useless use of private variable in void at test.pl line 37 Global symbol "$all_directories" requires explicit package name" My second issue is how do I rename files that are being copy to another directory dynamically without open and closing the directory that are being copy to?? Please Help!!! Here is my code
use File::Find; use File::Copy; my @directory = $all_directories; my @sourcefiles; my $srcfile; find(\&sourcefind, @directory); sub sourcefind { open(FL, $File::Find::name); push @sourcefiles, $_ if -f and /\.htm*/ ; close(FL); } foreach $srcfile (@sourcefiles) { copy( sub {print "$_ target found\n" if -f and /\Q$srcfile\E/;}, 'C:\temp'); }

Replies are listed 'Best First'.
Re: rename files dynamically
by CountZero (Bishop) on Feb 24, 2003 at 16:16 UTC

    Your question is very similar to Find, copy & rename. Therefore the same replies apply .

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      Is there a possible way to rename files dynamically with opening and closing a directory? Each file will have a different name since its name is drawn from a file content. Please advise...
Re: rename files dynamically
by hardburn (Abbot) on Feb 24, 2003 at 16:12 UTC
    my @directory = $all_directories;

    Where are you intitlizing $all_directories?

    push @sourcefiles, $_ if -f and /\.htm*/ ;

    That regex is safer (and possibly faster) if you wrote it as /\.html?\z/. As written, it will match ".htmmmmm" and even ".ht", which I don't think is what you want. The '*' matches the last character zero or more times (in your case, 'm'). I think what you meant to use was /\.htm.*/, which says "match '.htm' and then any character zero or more times". But even that isn't as good as the one suggested above.

    In the suggested regex (/\.html?\z/), we are using the '?' modifier (which says "match the last character zero or one times") to make the 'l' optional. The '\z' anchors the match to the end of the string (so it won't match 'file.htm.txt', for instance).

    ----
    Reinvent a rounder wheel.

    Note: All code is untested, unless otherwise stated

      I appreciate your response. I define $all_directories in another sub routine. I'm passing the vairable to the sub routine that it is going to find, copy and rename the files. I'm not familiar with passing variables between sub routines but I'll appreciate your suggestions.

        In Perl, you need to explicity pull variables off of @_ when passing params in:

        sub arg_test1 { my $arg1 = shift; # shift() defaults to @_ my $arg2 = shift; return $arg1; } # Another way sub arg_test2 { my ($arg1, $arg2) = @_; return $arg1; } my $test1 = arg_test1(1, 2); my $test2 = arg_test2(1, 2);

        ----
        Reinvent a rounder wheel.

        Note: All code is untested, unless otherwise stated

Re: rename files dynamically
by Anonymous Monk on Feb 24, 2003 at 21:17 UTC
    I didn't realize that there was static way to rename files ? WTF are you talking about? rename files dynamically indeed BAH gibberish!!