On Tue, 15 May 2001, Peter Lemus wrote:

> How can I use a while loop to map a drive letter to
> multiple servers.  I would like to map an nt drive,
> copy a file then unmap the drive and use the same
> drive letter to map to a different server, copy the
> file, disconnect and so on....
>
> system ("net use f: \\\\la_sys01\\cmsopen");
> system ("copy *.* $path");
> system ("net use f: /delete");

Do you need to map the drive?  You can't copy files directly using the UNC
path?  (My ignorance of NT is showing here, I'm sure).

> Also, what other option do I have for executing system
> commands besides using system?? I heard system is not
> the best way to do it.

You can use backticks: my $files = `la -la`;

This runs the command inside the backticks and dumps its output into the
variable (or none at all).  Probably the least safe way of executing
system commands.  'system' is better because you have more control over
what you are doing with the command, the arguments you pass to it, and
error codes returned.  There is also exec, but this differs from exec
because system waits for the called external command to return whereas
exec does not.  You should read the relevant perldoc pages on these
commands.

Another trick you see a lot of in Perl is a command being run with its
stdout being piped, and this is opened as a file handle, or having stdout
piped into a command (you see the latter done with sendmail, for
instance).

I actually saw this done recently:

open (WHOAMI, "whoami |");
while(<WHOAMI>) {
        $user = $_;
}
close(WHOAMI);

One thing to be careful of whenever passing data to external commands is
that you should properly validate any variable data that is passed.  This
can be a serious security hole and a bad guy could send " ; rm -fr *"
where you might just be expecting "ls $files".  Any data that is coming
from outside your control (i.e., a user) is untrusted data.

-- Brett

Reply via email to