• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1156
  • Last Modified:

How do i know the perl 'Can't call method "login" on an undefined value at' error cause


I have problem with my perl script.

This perl script i added on my crontab that will run every hour.
But sometimes when i checked the log, the ftp is not running and got message :

Can't call method "login" on an undefined value at

But the thing is, this is intermeatten. Let's say at time 01.00, 02.00 it works then at 03.00, it is not working
then 04.00 it's working again, 05.0 it's not working again.

I was wondering what is the cause of it ?
I understand that this is because the perl is unable to call Net::Ftp module.
But what i don't understand is what is the cause of the ftp can not call the module or login ?

I try to add some ping command, but unfortunately i don't have any root password, so i can not use ping command.

use Net::FTP;

&log ("Trying to get new FTP connection");
$ftp=Net::FTP->new($host,Debug => 1) or $newerr=1;
  push (@ERRORS, "Can't ftp to $host: $!\n") if $newerr;
  &log ("Error: $newerr ", @ERRORS) if $newerr;
&log ("Connected");

&log("Login FTP server, host = $host, user = $user, password not shown");
$ftp->login($user,$passwd) or $newerr=1;

2 Solutions
Net::FTP->new returns a Net::FTP object if it can connect to the server, but returns "undef" if no connection is established.

"undef" does not have a "login" method, because it is not a valid Net::FTP object.

use something like

if ( defined $ftp ) {
$ftp->login($user,$passwd) or $newerr=1;
} else {
&log("no server connection - cannot login to FTP server, host = $host");
budihartonoAuthor Commented:
HI Don,

Thank you for your help.
But if i checked on the script, it doesnt say anything much about the failed reason.
I guess if the script is failed, it will only log no server connection.

The things that i want to know is why it's failed ?Is it network connection or failed port? or something else? So actually we wan to retrieve information what is this 'undef' ?

Since sometimes it works, sometimes it doesnt work.

Thank you.
"If the constructor fails undef will be returned and an error message will be in $@"

&log("no server connection - cannot login to FTP server, host = $host, reason: $@");


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

try setting debug to 1 when you make the connection, and make sure to watch STDERR

    $ftp = Net::FTP->new("some.host.name", Debug => 1)
      or die "Cannot connect to some.host.name: $@";
budihartonoAuthor Commented:
Hi all,

now i can see it why ,
2011-02-17 12:03:00 [20770] Trying to get new FTP connection
2011-02-17 12:05:00 [20770] Cannot connect to : Net::FTP: Timeout
2011-02-17 12:05:00 [20770] no server connection - cannot login to FTP server, host =, reason: Net::FTP: Timeout

But i am teriblle sory, my next question is, whether this is a network problem ? How do we check it? Can we add some ping command ?
 But if i check while the script is trying to get FTP connection, manually i check using ping, i got result that the ip is alive.

I am so confused.
Thank you.
budihartonoAuthor Commented:
secondly..right after the crontab is running the perl script, and the result is Time Out, then i mannually running the script, it works ...!!

I was wondering why from crontab is failed.
try increasing the Timeout parameter

$ftp=Net::FTP->new($host,Debug => 1, Timout => 300) or $newerr=1;
oops - Timeout it is :-)

$ftp=Net::FTP->new($host,Debug => 1, Timeout => 300) or $newerr=1;
budihartonoAuthor Commented:

I try to add longer timeout as suggested, the result, stil the same, sometimes its working, sometimes it is not working.
THe log is
2011-02-17 15:07:44 [3091] Trying to get new FTP connection
2011-02-17 15:11:29 [3091] Cannot connect to SGJKT4 : Net::FTP: Timeout
2011-02-17 15:11:29 [3091] no server connection - cannot login to FTP server, host = SGJKT4, reason: Net::FTP: Timeout

2011-02-17 16:03:00 [16029] Trying to get new FTP connection
2011-02-17 16:06:45 [16029] Cannot connect to : Net::FTP: Timeout
2011-02-17 16:06:45 [16029] no server connection - cannot login to FTP server, host =, reason: Net::FTP: Timeout

As your information, i have 4 line of the FTP script that executed by crontab.

But still i don't understand, sometimes it works, sometimes it's not.
even when it's trying to get ftp connection , mannualy i try to ping, i can reach the server.
Also when i run it manually, its always working.


Try using the Net::Ping module with (with the tcp or udp option if you cannot run as root)
before you try to FTP.


use Net::Ping;

$host = "";
$p = Net::Ping->new();
print "$host is alive.\n" if $p->ping($host);

Open in new window

Suhas .QA ManagerCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now