1754284 Members
3016 Online
108813 Solutions
New Discussion юеВ

Re: perl error in ftp.pl

 
SOLVED
Go to solution
Tapas Jha
Valued Contributor

perl error in ftp.pl

Hi,

I am newbie in perl and trying to learn perl.

Just reading today's forum reagrding ftp script in perl i tried to run on my server but it is giving below error.
Hope procura have answer. Pl. tell me why erorr is coming.
Output of perl -MNet::FTP -le'print $Net::FTP::VERSION'
is 2.65
and perl -v shows:
This is perl, v5.8.0 built for PA-RISC1.1-thread-multi
(with 1 registered patch, see perl -V for more detail)
Copyright 1987-2002, Larry Wall
Binary build 806 provided by ActiveState Corp. http://www.ActiveState.com
Built 21:44:05 May 2 2003

Error message:
Global symbol "$ftp" requires explicit package name at ftp.pl line 16.
Global symbol "$destination" requires explicit package name at ftp.pl line 19.
Global symbol "$destination" requires explicit package name at ftp.pl line 19.
Execution of ftp.pl aborted due to compilation errors.
Below is the sfript:
#!/usr/local/bin/perl
use strict;
use warnings;
use Net::FTP;
use Net::Netrc;
my $server="x.x.x.x";
my $file="/home/tapas/test.txt";my $ftp = Net::FTP->new ($server, Timeout => 9000,Debug => 3) or die "$server: cannot connect: " .$ftp->message;$ftp->login or die "login: " .$ftp->message;
$ftp->binary or die "binary: ".$ftp->message;
$ftp->cwd ($destination) or die "cd $destination: ".$ftp->message;
open my $fh, "<:utf8", $file or die "open $file: $!";
$ftp->put ($fh, $file); # must give remote name.
close $fh or die "Cannot close file\n";
$ftp-quit;

Rgds
Tapas
Tapas Jha
7 REPLIES 7
Mark Grant
Honored Contributor

Re: perl error in ftp.pl

Specifically removing the "use strict" will get rid of the problem but better people than I will tell you why it should remain there.
Never preceed any demonstration with anything more predictive than "watch this"
H.Merijn Brand (procura
Honored Contributor
Solution

Re: perl error in ftp.pl

I've split on ';'

1. You've not set $destination anywhere
2. The $ftp assignment was erroneously using the $ftp in the error message. If the connectr fails, you don't have an $ftp set, and hence cannot use it's methods.

#!/usr/local/bin/perl
use strict;
use warnings;
use Net::FTP;
use Net::Netrc;
my $server="x.x.x.x";
my $file="/home/tapas/test.txt";
my $destination = "/pub/foo/bar/baz";
my $ftp = Net::FTP->new ($server, Timeout => 9000,Debug => 3);
$ftp die "$server: cannot connect: $@";
$ftp->login or die "login: " .$ftp->message;
$ftp->binary or die "binary: ".$ftp->message;
$ftp->cwd ($destination) or die "cd $destination: ".$ftp->message;
open my $fh, "<:utf8", $file or die "open $file: $!";
$ftp->put ($fh, $file); # must give remote name.
close $fh or die "Cannot close file\n";
$ftp-quit;

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: perl error in ftp.pl

Mark, getting rid of 'use strict' is never ever going to solve the problem. It just hides the errors and will die on run-time.

_I_ am the one going to say: Never ever write scripts that should last without use strict and use warnings, and never remove these to get rid of (annoying) messages. It is far more likely that you made an error^Wtypo.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Fred Ruffet
Honored Contributor

Re: perl error in ftp.pl

If you have an error of connection in this line :
my $ftp = Net::FTP->new ($server, Timeout => 9000,Debug => 3) or die "$server: cannot connect: " .$ftp->message;
You are trying to die with a message including $ftp->message, where $ftp is undefined...

This is the first thing I see, but it can be something else.

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Mark Grant
Honored Contributor

Re: perl error in ftp.pl

procura,

Indeed you were the person I was referring to and hoped that it would be you explaining the error of my ways :)

See you later should beepz ever return!
Never preceed any demonstration with anything more predictive than "watch this"
Tapas Jha
Valued Contributor

Re: perl error in ftp.pl

Hi Procura,

I am getting below error message while compiling. Line 17 is $ftp die "$server: cannot connect:$@";

syntax error at ftp.pl line 17, near "$ftp die"
Execution of ftp.pl aborted due to compilation errors.

Rgds
Tapas
Tapas Jha
H.Merijn Brand (procura
Honored Contributor

Re: perl error in ftp.pl

Stupid cut-n-paste ...

$ftp or die "FTP: cannot connect";

or something like that. The or was dropped somehow.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn