CommonClear.pm


Code Index:


NAME

package Net::FTP::CommonClear


DESCRIPTION

Core functions needed by package Net::FTP::Etilem_free.

Fast get/put/del of filenames (not dirs) on FTP server defined in the above package.


COPYRIGHT

Copyright 2010 Etienne LEMEE <coding AT etilem DOT net>

This piece of code is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


#!/usr/bin/perl -w

package Net::FTP::CommonClear;

use strict;
use vars qw!@EXPORT @ISA!;
use Net::FTP;
$|=1;

@ISA = qw!Net::FTP!;
@EXPORT = qw!ftp_connect ftp_changedir ftp_get ftp_put ftp_del ftp_quit!;

sub ftp_connect {
    my $self = shift;
    print "Connecting to $self->{FTP_SERVER} : "
        unless($self->{QUIET});
    my $ftp = Net::FTP->new($self->{FTP_SERVER}, Debug => 0)
        or die "Cannot connect to $self->{FTP_SERVER} : $@";
    $self->{FTP} = $ftp;
    print "OK\n"
        unless($self->{QUIET});
    print "Logging to $self->{FTP_SERVER} as $self->{FTP_USER} : "
        unless($self->{QUIET});
    $self->{FTP}->login($self->{FTP_USER},
                        $self->{FTP_PASS})
        or $self->die_nicely("Cannot login : ",
                             $self->{FTP}->message);
    print "OK\n"
        unless($self->{QUIET});
    $self->{FTP}->binary;
}

sub ftp_changedir {
    my $self = shift;
    my $remote_dir = shift;
    print "Changing dir to $remote_dir : "
        unless($self->{QUIET});
    $self->{FTP}->cwd($remote_dir)
        or $self->die_nicely("Cannot change working dir to $remote_dir : ",
                             $self->{FTP}->message);
    print "OK\n" unless($self->{QUIET});
    $self->{CURDIR} = $remote_dir;
}

sub ftp_get {
    my $self = shift;
    my $file = shift;
    print "Downloading $file : "
        unless($self->{QUIET});
    $self->{FTP}->get($file)
        or $self->die_nicely("Cannot get $file : ",
                             $self->{FTP}->message);
    print "OK\n"
        unless($self->{QUIET});
}

sub ftp_put {
    my $self = shift;
    my $file = shift;
    my $tmp_file = $file . "-" . time;
    print "Uploading $file : "
        unless($self->{QUIET});
    $self->{FTP}->put($file, $tmp_file)
        or $self->die_nicely("Cannot put $file : ",
                             $self->{FTP}->message);
    $self->{FTP}->rename($tmp_file, $file)
        or $self->die_nicely("Cannot rename $tmp_file to $file : ",
                             $self->{FTP}->message);
    print "OK\n"
        unless($self->{QUIET});
}

sub ftp_del {
    my $self = shift;
    my $file = shift;
    print "Deleting $file : "
        unless($self->{QUIET});
    $self->{FTP}->delete($file)
        or $self->die_nicely("Cannot delete $file : ",
                             $self->{FTP}->message);
    print "OK\n"
        unless($self->{QUIET});
}

sub ftp_quit {
    my $self = shift;
    print "Closing connection : "
        unless($self->{QUIET});
    $self->{FTP}->quit
        or die("Cannot close connection to $self->{FTP_SERVER} : ",
               $self->{FTP}->message);
    print "OK\n"
        unless($self->{QUIET});
    undef $self->{CURDIR};
    undef $self->{FTP};
}

sub die_nicely {
    my $self = shift;
    print @_;
    $self->ftp_quit;
    exit;
}
1;