Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I set up Class variables in my object

Perl Class Objects

How do I set up Class variables in my object

by  cdlvj  Posted    (Edited  )
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;
[b]
    ## define a structure to contain necessary private vars
[/b]
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");
[b]
              ## this sets the data to the object	
              $self->{FTPDATA} = $f;
[/b]
	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) = @_;
[b]
            ## this line retrieves the structure
            my $f = $self->ftpdata();
            ## this line sets the parm in the data structure
            $f->debug($r);
[/b]
   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;

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

   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;
[b]
        ## 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;
[/b]
   ### 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;
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top