On Saturday 05 June 2004 HH:28:05, Edward Ludlow wrote:
> I'm a newbie to Perl - I'm having to dip in and try and do some coding 
> as I need a script that will move a file from one server to another.  I 
> believe this can be done with the Net::FTP object.

> I've played around whit this a fair bit, and I think this script should
> work..

Hi Edward,

the script looks good to me - some small annotations below:

>
> #!/usr/bin/perl

Normally I use a standard header like this:

  #!/usr/bin/perl -w
  use strict;

The "-w" switch enables some quite helpful warnings, while
"use strict" forces you to write "clean" code, i.e. you've got to
declare variables etc. You've got to get used to this, but in the
long run, it makes your life much easier.

> print "Content-type: text/plain","\n\n";
>
> use Net::FTP;
> $ftp = Net::FTP->new("www.slbsu.org.uk", Debug => 0)
> or die "Cannot connect to some.host.name: $@";

If you enable "use strict", you've got to declare the $ftp variable
like this:

  my $ftp = Net::FTP -> new(....)

>
> $ftp->login("username",'password')
> or die "Cannot login ", $ftp->message;
>
> $ftp->quit;
>
>
>
> But it doesn't!  If I upload that script as it is, it just returns a
> blank page with no errors, when I know that the username and password
> are wrong - so why does it not tell me?

The problem that you've got here is that error messages (e.g. everything 
produced by "die") are generally written to the web server's error log, but 
not to the screen.
If you execute your script on the command line, you'll see the error message.

You can change this behaviour by putting something like

  use CGI::Carp 'fatalsToBrowser';

at the beginning of your script. This will print all fatal messages to the 
browser.

While you're at it, you might want to take a look at the module CGI - it helps 
you to deal with many common CGI problems (type "perldoc CGI" on your command 
line to read more about it).

> PS If anyone fancies outlining how I'd code a script to transfer one
> file to another location, that would be much appreciated...:)

"perldoc Net::FTP" is your friend - there you'll find documentation for the 
put() method, which should be what you want.
Basically, you just need to extend your script a bit - something like: 

my $ftp = Net::FTP->new(...);
$ftp->login("username", "password");

# change the working directory on the server
$ftp->cwd("dir/where/you/want/to/put/your/file");

$ftp->put("name/of/your/local/file");

$ftp->quit();

If you are behind a firewall/proxy/something like this, you might also want to 
set the passive property in the constructor - this did the trick for me once 
after searching for some hours:

my $ftp = Net::FTP->new($ftp_address, Debug => 0, Passive => 1)
  or die "cannot connect to $ftp_address : $@";

HTH,

Philipp



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to