http://qs321.pair.com?node_id=178204

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

### See if they wanna move user files if(param('move') eq "yes") { $old = param('old'); ### Start a foreach loop for all users foreach $line (@list) { opendir(DIR, "$old$line"); my @userfiles = sort grep !/^\.\.?\z/, readdir DIR; close DIR; ### Need put each direcotry in a new one } }
Alright see that, well I have a couple direcotries in $list and I wanna put them to a new direcotry called new. Now is this possible to transer files and directories. Can I do it with no modules

Replies are listed 'Best First'.
Re: moving files
by thunders (Priest) on Jun 29, 2002 at 05:20 UTC
    Yes you can but you should not. Your best bet is File::Copy. It is a standard module on all modern Perl versions, you do not have to install it. You may want to use a system call to cp or mv. DON'T this could open open huge security holes where web user can directly execute code on your server.
Re: moving files
by rjray (Chaplain) on Jun 29, 2002 at 07:19 UTC

    As a previous reply points out, File::Copy is a good place to start, for a truly portable approach to the problem. If you are just working within a very localized directory structure (i.e., not planning on crossing any device boundaries, etc.) you might get away with just using the rename system-call. Check perlport to see if there are any issues on your platform. I personally would recommend using File::Copy since it is part of the Perl core.

    --rjray

Re: moving files
by erikharrison (Deacon) on Jun 29, 2002 at 16:42 UTC

    Security! You don't check the value of $old for dangerous characters (shell metacharacters, etc) and you do no untainting, which tells me that you don't have untaint mode on. For your safety, I encourage you to enable taint mode and check your params for "safe" characters. I suggest the following regex to start, until you can accurately pin down the dangers.

    $old =~ s/[^\w]//g; #or possibly throw a warning

    As for the need to move files, File::Copy is probably your best bet. If you insist on not using modules (in this or other pursuits) the module is still the best place for and answer to the question "how do I do it". After all, it's just as valid as asking us for help, and the module is the most tested solution to the problem. Study prior art! :-)

    Cheers,
    Erik

    Light a man a fire, he's warm for a day. Catch a man on fire, and he's warm for the rest of his life. - Terry Pratchet