Variables are objects that are given memory space and cpu time upon execution. They generally hold values for later, or multiple use.
Functions are a set of instructions that never change and can be called from anywhere in your program. They are generally designed to process some input values and return an output value.
Classes and methods get you into OOP (Object Oriented Programming).
Classes are simply an organizational methods used by the programmer to represent an entity (a group of related objects). For example, a class that represents the entity of members at voids BBS could be represented by their attributes such as, NAME, ID, POST COUNT, JOIN DATE. If you are familiar with perl, most modules you will see are OOP modules that use classes. You can also use a class to represent or reference a group of related functions.
Example:
- Code: Select all
package DBClass;
use strict; # Strict.pm, used to keep the programmer in line
use DBI; # Database perl module
sub new {
my $pkg = shift;
my $obj = { '_output' => undef, error => '' };
bless $obj, $pkg;
return $obj;
}
sub Connect {
# Connection Code Here
}
sub AddTable {
# Table Addition Code Here
}
Sub DeleteTable {
# Table Deletion Code Here
}
Now the above example is your class (or in perl called package or module).
Now to call the class:
- Code: Select all
#!/usr/bin/perl -w
use strict;
use DBClass;
# Create the reference to the package for later use in calling the functions/subroutines.
my $dbref = DBClass->new();
# Create db connection handle
my $con = $dbref->Connect();
# Add Table To Database
$dbref->AddTable($con, 'TABLE_NAME');
# Delete Table From Database
$dbref->DeleteTable($con, 'TABLE_NAME');
As you may be able to tell, after we create the reference to the package or class we can use that reference when calling subroutines or functions in the class. The last two calls to the class, I send the connection handle to the functions because that is the way the function needs to work when dealing with the database. If I were to write out the support code to those functions you could see why.
Classes are a great way to do "modular programming". It allows you to seperate your code into numerous files and be able to reach them in the scope of your main() function. They allow for greater future expansion to the application and allow for easier troubleshooting. If you can write a set of instructions once and call them many time, that is better than writing it many time throughout the program each time you want to use it.
The difference between classes and functions are functions have a specific set of tasks or instructions and are limited in what they can do as far as receive data, perform some instructions on it, and return it. A class can be a set of funtions, a set of variables, etc.
In this next example I will show you a completely different class setup and usage.
- Code: Select all
package UserData;
sub new {
my $pkg = shift;
my $obj = {
'NAME' => q!ZiaTioN!,
'EMAIL' => q!ziation@email.com!,
'AGE' => q!97!,
'LOCATION' => q!Mars!,
'GENDER' => q!Female!,
};
bless $obj, $pkg;
return $obj;
}
1;
Now to call it:
- Code: Select all
#!/usr/bin/perl -w
use strict;
use UserData;
my $ud = UserData->new();
print qq~
My name is $ud->{'NAME'}.
My email address is $ud->{'EMAIL'}.
I am $ud->{'AGE'} years old.
I live on $ud->{'LOCATION'}.
I am of the $ud->{'GENDER'} gender.
~;
Now the class I wrote above is not a set of functions, but rather a set of objects or variables that can now be referenced from anywhere in the application regardless of scope. This is all done in perl with anonamys hashes and arrays and whatnot, but in other languages is done a little more cleanly (Java, C++, ADA, etc). Perl has to tweak it a bit to allow for OOP because perl is based on C and C does not have OOP capabilities.
It is all a lot to deal with but if you can get the hang of it your code will thank you. If anyone can better my explanation or find faults in my explanation please point it out. I am not the best teacher. I am a doer and not a teacher.