×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Perl Class Objects

How do I set up Class variables in my object by cdlvj
Posted: 27 Feb 07

Any global variables defined by the 1st instatiation will be overwritten on subsequent instatiations of your object.

These variables need to be private. One way is to define a struct to be manipulated in your methods.

CODE

use Net::FTP;

package FB::myFTP;
use Class::Struct;

    ## define a structure to contain necessary private vars

struct ftpdata => {
    servers    => '@',
    myftpuser  => '$',
    myftpcode  => '$',
    retries    => '$',
    ftp        => '$',
    connection => '$',
    mode       => '$',    ## 1 is ascii 2 is binary
    ftplog     => '$',
    timeout    => '$',
    passive    => '$',
    port       => '$',
    debug      => '$',
    sleeptime  => '$'
};
    

#####################################################
# Purpose: CONSTRUCTOR
#####################################################
#   Here load default values
sub new
{
    my $class = shift;
    my $self = {};
    my $f = ftpdata->new();
    ## set general ftpdata area - undef the rest
    $f->retries(20);
    $f->mode(1);       ## default to ascii - 1
    $f->sleeptime(120);
    $f->servers(0,"151.1.152.11");
    $f->servers(1,"151.1.152.15");

              ## this sets the data to the object    
              $self->{FTPDATA} = $f;

    bless($self,$class);
    return $self;
}

#####################################################
# Purpose: DESTRUCTOR
#####################################################
#   
sub DESTROY
{
    my $self = shift;
    $self->logoff();
}

#####################################################
# Purpose: Method to return saved data structure
#####################################################
#   
sub ftpdata
{
    my $self = shift;
    return $self->{FTPDATA};
}


sub setDebugLevel
{
#####################################################
# Purpose: Method to set the debug level
#####################################################
# Accept Parameters
#   
   my $self = shift;
   my ($r) = @_;

            ## this line retrieves the structure
            my $f = $self->ftpdata();
            ## this line sets the parm in the data structure
            $f->debug($r);

   return "OK";
}

sub setPort
{
#####################################################
# Purpose: Method to set the port
#####################################################
# Accept Parameters
#   
   my $self = shift;
   my ($r) = @_;
   my $f = $self->ftpdata();
   $f->port($r);
   return "OK";
}

sub setPassiveON
{
#####################################################
# Purpose: Method to set Passive mode
#####################################################
# Accept Parameters
#   
   my $self = shift;
   my $f = $self->ftpdata();
   $f->passive(1);
   return "OK";
}

sub setPassiveOFF
{
#####################################################
# Purpose: Method to set active mode
#####################################################
# Accept Parameters
#   
   my $self = shift;
   my $f = $self->ftpdata();
   $f->passive(0);
   return "OK";
}


sub setTimeOut
{
#####################################################
# Purpose: Method to set timeout
#####################################################
# Accept Parameters
#   
   my $self = shift;
   my ($r) = @_;
   my $f = $self->ftpdata();
   $f->timeout($r);
   return "OK";
}

sub setRetries
{
#####################################################
# Purpose: Method to set retries
#####################################################
# Accept Parameters
#   
   my $self = shift;
   my ($r) = @_;
   my $f = $self->ftpdata();
   $f->retries($r);
   return "OK";
}

sub setSleepTime
{
#####################################################
# Purpose: Method to set sleep between connection try
#####################################################
# Accept Parameters
#   
   my $self = shift;
   my ($r) = @_;
   my $f = $self->ftpdata();
   $f->sleeptime($r);
   return "OK";
}

sub setModeBinary
{
#####################################################
# Purpose: Method to set Mode Binary
#####################################################
#   
   my $self = shift;
   my $f = $self->ftpdata();
   $f->mode(2);
   return "OK";
}

sub setModeAscii
{
#####################################################
# Purpose: Method to set Mode Ascii
#####################################################
#   
   my $self = shift;
   my $f = $self->ftpdata();
   $f->mode(1);
   return "OK";
}


#####################################################
# Purpose: Method to logoff from connection
#####################################################
#   
sub logoff
{
   my $rc;
   my $self = shift;


           ## retrieve the object for the code to operate on
           my $f = $self->ftpdata();
           ## set local variable with the ftp object
           my $ftp = $f->ftp;


   if ($ftp)
   {
        $rc=$ftp->quit;
   }
   $f->ftp(undef);
}

#####################################################
# Purpose: Method to connect to a server in list
#####################################################
#   
sub connect
{
   my $self = shift;
   my $tryAgain = 1;
   my %args;
   my $ftp;

        ## retrieve the private vars
        my $f = $self->ftpdata();
        ### set local vars as necessary
        my $retries = $f->retries;
        my @s = $f->servers;
        my @servers = @{$s[0]};
        my $stime = $f->sleeptime;

   ### build any arguments for connection
   undef %args;
   if ( $f->timeout )
   {
   $args { 'TIMEOUT' } = $f->timeout;
   }
   if ( $f->passive )
   {
   $args { 'PASSIVE' } = $f->passive;
   }
   if ( $f->port )
   {
   $args { 'PORT' } = $f->port;
   }
   if ( $f->debug )
   {
   $args { 'DEBUG' } = $f->debug;
   }
   while($retries)
   {
      foreach my $s (@servers)
      {
          if ($ftp = Net::FTP->new($s,%args))
          {
             $f->connection($s);
         $f->ftp($ftp);
                  return "OK";
              }
          }
          $retries--;
          sleep $stime;                 
                    
   }
   return "Connection error to Server";
}

return 1;

Back to Perl FAQ Index
Back to Perl Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close