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

Hello All, I discovered this site some time ago as a fledgling perl scripter and immediately tagged it as my favorite perl site! Now, I am advancing to the next stage of my perl career by posting my first question. I hope I am using this site correctly. I have been toiling over the following code:
use Win32API::File 0.08 qw( :Func ); use Net::Ping; if( $#ARGV > 1 || ! $ARGV[0] ) { usage(); exit 1; } $source = '\path\to\local\file(s)'; $destination = '\path\to\remote\host'; $hostname = $ARGV[0]; $p = Net::Ping->new("icmp"); if( $p->ping($hostname )) { print "\u$hostname is alive.\n"; copy($source, $destination); } else { print "\u$hostname is not reachable\n"; $p->close(); } sub usage { print "Please enter either an IP address or a Hostname\n"; print "Usage: Copied file/s to specified Hostname or IP addres +s"; }
My objective is to copy a file or files from my Win2k machine to one or more remote Win2k machines. I have been working with the Win32API::File module, but haven't been successful. It complains that it can't find the sub copy in my main package."Undefined subroutine &main::copy called at copynetcoolfiles.pl". Why is it only looking at my main symbol table when I have loaded the Win32API module? Could use some pointers. Thanks.

Replies are listed 'Best First'.
Re: File Copy
by demerphq (Chancellor) on Mar 13, 2002 at 15:39 UTC
Re: File Copy
by VSarkiss (Monsignor) on Mar 13, 2002 at 15:53 UTC

    I actually think you should follow the advice from demerphq above and use File::Copy. Nonetheless, the direct answer to your question about why your code is failing is that Win32API::File doesn't have a routine called copy -- I think the one you're after is CopyFile.

    HTH

Re: File Copy
by Juerd (Abbot) on Mar 13, 2002 at 15:56 UTC

    Why is it only looking at my main symbol table when I have loaded the Win32API module?

    When you don't specify a package yourself, Perl always assumes the current package, and when it doesn't find it, it looks in the packages in the @ISA array, but use (?!base) doesn't alter that array. For example, when you use LWP::Simple;, the module will export it's get() to the current package, and thus insert it in its symbol table. Even though @ISA isn't modified, Perl can find the get() routine, because it's in the same symbol table!

    Apparently, the Win32API::File module you're talking about, doesn't have a copy() function in the :Func tag. I have just searched in File.pm (I'm not a Windows user anymore, so I'm not familiar with Win32API-modules), and think the function you want is called "CopyFile".

    Good luck!

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk