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

Dear Gods over Earth, Water, Fire, Water and PERL!

I seek your help for on this little problem.

Executing the code below gives error "Use of unitialized value in subtraction at line 70". This is line 70:

if (time - $ftp->mdtm($_) > $maxage) { push(@return, $_); }

I have looked at the code and it didn't helped. So I tried to find what is unitilized but I haven't found anything:

Please, I feel completely blind and I don't want to remove use strict just because it is complaining.

Execution:

[pcsson@psr-iccdrift op5]$ ./ftp.pl -u loollippop -P pitamikabyta -m m +y_machine -d hippohumpa -a 10 -s hej Use of uninitialized value in subtraction (-) at ./ftp.pl line 70. 2 stuck files stuck on hej

Code:

#!/usr/bin/perl use warnings; use strict; use Net::FTP; use Getopt::Long; # Todo: Installera #use Getopt::Long::Configure ("bundling"); use Pod::Usage; # Parameters that user can supply my ($user, $pwd, $machine, $dir, $port, $filter, $maxage, $name); my ($ftp, @stuck); # Exit codes. Fetched from # http://www.op5.com/support/documentation/plugin-development my ($OK, $WARNING, $CRITICAL) = (0, 1, 2); # Resonable defaults where we can. ($port, $dir, $filter, $maxage) = (21, ".", ".*", 3600); sub err { print @_, "\n"; exit $CRITICAL } sub parse_options { my %options = (); $options{'h|help'} = sub { pod2usage(-verbose => 2, -output => \*S +TDERR); exit -1; }; $options{'u|user=s'} = \$user; $options{'P|password=s'} = \$pwd; $options{'m|machine=s'} = \$machine; $options{'p|port'} = \$port; $options{'d|dir=s'} = \$dir; $options{'f|filter=s'} = \$filter; $options{'a|age=i'} = \$maxage; $options{'s|servicename=s'} = \$name; GetOptions %options; # Do validation. Port, dir, pwd, machine, user are all mandatory $user or err "Must specify username\n"; $pwd or err "Must specify password\n"; $machine or err "Must specify machine\n"; if (!$name) { $name="ftp://$user:$pwd@$machine:$port/$dir/$filter" +; } } sub retrieve_listing { $ftp = Net::FTP->new($machine, Port=>$port) or err "Cannot connect to $machine:$port\n"; $ftp->login($user,$pwd) or err "Cannot authenticate $user\n"; my @return = $ftp->ls($dir); @return; } sub filter_name { # Filters directory listing by name specified by the filter flag o +ption grep /$filter/, @_; } sub filter_time { # Filters out files based on the my @return = (); for (@_) { if (time - $ftp->mdtm($_) > $maxage) { push(@return, $_); } } @return; } parse_options; @stuck = filter_time filter_name retrieve_listing; $ftp->quit; if (@stuck) { err scalar(@stuck) . " stuck files stuck on $name"; } else { exit 0; } __END__

Replies are listed 'Best First'.
Re: Use of unitialized, why?
by choroba (Cardinal) on Apr 06, 2011 at 12:47 UTC
    Use Data::Dumper. Some of your variables do not contain what you think (or some of your functions/methods do not return what you think).
Re: Use of uninitialized, why?
by moritz (Cardinal) on Apr 06, 2011 at 12:51 UTC
    $ftp->mdtm($_): function, $ftp is initilized and connected to server before

    If $ftp was undef, you'd get "Can't call method "mdtm" on an undefined value at ... line 70.

    What's more interesting is the value that $ftp->mdtm($_) returns (because that's what's used in the subtraction)

Re: Use of unitialized, why?
by tospo (Hermit) on Apr 06, 2011 at 12:56 UTC
    Since you are getting "Use of unitialized value in subtraction" the prime suspect is $ftp->mdtm($_). You say it has a value before but at what point do you check? Are you using the debugger (perl -d) or have you tried to add a warn statement just before that line to print out what this variable holds?
Re: Use of unitialized, why?
by jellisii2 (Hermit) on Apr 06, 2011 at 14:00 UTC
    Set Debug=>3 on your FTP connection and start looking at that.
Re: Use of unitialized, why?
by ww (Archbishop) on Apr 06, 2011 at 20:56 UTC

    Per the OP (for easy ref since we're this far down the thread):

    if (time - $ftp->mdtm($_) > $maxage) { push(@return, $_); }

    I have looked at the code and it didn't helped. So I tried to find what is unitilized but I haven't found anything:

    • time: function
    • $ftp->mdtm($_): function,...

    Are you quite sure that "time" -- which looks (to me, YMMV) a lot like a bareword -- is a function rather than a typo for time() or something similar?