CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
difference btw. module, package, object, subroutine, method, etc.

 



zipflip
Deleted

May 31, 2000, 5:38 PM

Post #1 of 4 (870 views)
difference btw. module, package, object, subroutine, method, etc. Can't Post

heya! i am very confused. is there a glossary or tutorial that explains OO terms in ways that don't require quite a bit of perl knowledge to begin with? i need to know what is a:

subroutine
method
package
object
module
bless
hash
constructor
reference
etc...

i have an idea about some of these just from tinkering but i don't have a deep enough knowledge of the technical terms. unfortunately most tutorials i have seen don't help me because they define things like: "a method is just a package that has a reference to a constructor that blesses a hash within a module which is an object blah blah blah...."


perlkid
stranger

Jun 1, 2000, 12:40 AM

Post #2 of 4 (870 views)
Re: difference btw. module, package, object, subroutine, method, etc. [In reply to] Can't Post

 Hi Zip,

I went looking for a tutorial that explains the basics for your list at seeki.com and I came up with this link. I think the first result is the one that explains subroutines pretty well. But the best advice I can give to you for this question is to check at your local Comp Usa or Computer store/book store and check for a book called "Perl Core Language Little Black Book" by Steven Holzner. I bought it at my local Comp Usa for $19.95. I also bought "Perl Cookbook" and "Mastering Perl 5" and I think that the little black book is the best one. I Think that you will learn all of the things on your list by looking them up in that book. It's a very good book.

But If you do want to..

Try those tutorials and read on..


I'll explain as much as I can and then I'm sure someone else will take over.

A Subroutine is like an isolated block of code that you can use when ever you want in your main code without it getting in the way. I don't fully know the capabilities of a subroutine though. But You can use them like this.

sub The Name Of The Sub
{
open(file, "<test.db");
@all=<file>;
close(file);
}

Then to execute that code use

&The Name Of The Sub;

That's it as for the basic sub.

I use subs whenever I'm parsing a form, url, or more that 2 data bases at one time. Like just recently I did a script that counts the lines in a series of files, I opened all 10+ data bases and assigned the total number of the file to an individual scalar.

sub additup
{
open(file, "<test.db");
@all=<file>;
close(file);
foreach $line (@all)
{
$count++;
}

open(file2, "<test2.db");
@all=<file2>;
close(file2);
foreach $line2 (@all2)
{
$count2++;
}
}

Then I used them like this.

&additup;
$master-count = $count + $count2;
print "$master-count";

Then I was able to use that sub sveral times without having to redo the code.

As for a module,

A module use another perl file that makes your life a little easier. For example,

To read the url's values instead of using a long sub I can just use a module.

To do that I'd just,

use CGI;

CGI is the name of the module.

Then to use that mod the code is

q = new CGI;
$tag = $q->param(tag);
print "$tag";

If you were to use the lwp module there would be a different set of commands in order to envoke the mod as there would be for all mods.

A good list of downloadable modules is at http://www.cpan.org

As For A hash,

%hash =
(
fruit , apple,
perl , power,
);

print "$hash{'perl'}"

power

I hope this helps but I'm running short on time.

Good Luck,

perlkid

[This message has been edited by perlkid (edited 06-01-2000).]


japhy
Enthusiast

Jun 1, 2000, 10:32 PM

Post #3 of 4 (870 views)
Re: difference btw. module, package, object, subroutine, method, etc. [In reply to] Can't Post

I also suggest "Randal's Object-Oriented Tutorial", the perlroot doc, available on my web site.


japhy
Enthusiast

Jun 2, 2000, 7:12 AM

Post #4 of 4 (870 views)
Re: difference btw. module, package, object, subroutine, method, etc. [In reply to] Can't Post

I will do my best to explain these terms, but I STRONGLY suggest you look at the glossary at the back of the "Programming Perl" book (by O'Reilly) for many of them. Perhaps a Perl Glossary is needed in the documentation... you should check the Perl documentation as well, by the way, which can be seen online by clicking the "Perl Documentation" link on this web site.

Let me know if there is interest in a Perl Glossary, and I'll get to work in creating one.

function -- an executable portion of code, that can receive 0 or more arguments (values passed to the subroutine) and return 0 or more return values (values returned to your main program). also called a "subroutine".

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


sub add {
my $sum = 0;
for (@_) { # @_ holds the arguments to a function
$sum += $_;
}
return $sum; # $sum is the return value
}

$carl_friedrich_gauss = add(1 .. 100);
# shorthand for the list of numbers from 1 to 100
</pre><HR></BLOCKQUOTE>


method -- a function in a class that expects either the class or an instance of that class (called an object) as its first argument. Methods can be called in indirect or direct notation.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


use CGI;

# class method
$query = CGI->new; # direct
$query = new CGI; # indirect

# instance method
@fields = $query->param("alpha","beta"); # direct
@fields = param $query "alpha", "beta"; # indirect
</pre><HR></BLOCKQUOTE>


package -- a namespace in Perl. If you are in package Foo, and you refer to the global variable $bar, the variable is really $Foo::bar.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


package Foo;
$bar = 20;
$main::bar = 30;
package main;
print $bar; # 30
print $Foo::bar; # 20
print $main::bar; # 30
</pre><HR></BLOCKQUOTE>


object -- an instance of a class created via a constructor.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


use CGI;
$query = new CGI; # $query is an object
</pre><HR></BLOCKQUOTE>


class -- a package that supports objects.


module -- a file that contains Perl code in its own namespace (called a package). A module does not have to be object-oriented. Modules have the suffix of '.pm'.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


use CGI; # <-- compile-time inclusion of a module;
require CGI::Cookie; # <-- run-time inclusion of a module
require "config_stuff"; # <-- run-time inclusion of a library file (NOT a module)
</pre><HR></BLOCKQUOTE>


bless -- a Perl function to indicate a reference belongs to a certain class, so that it inherits the methods of that class.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


package Foo;
sub new { # this is a typical constructor
my $class = shift; # probably 'Foo'
my $obj = { # a hash reference
NAME => "Jeff",
AGE => shift | | 0,
};
return bless $obj, $class;
}

# later, in the main program...
use Foo;
$my_object = new Foo 18;
$other_obj = Foo->new(19); # next year
</pre><HR></BLOCKQUOTE>


hash -- a basic Perl data type.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


%age = (
Jonathan => 14,
Jeff => 18,
Jennifer => 21,
);
print $age{Jeff};
$age{Jennifer}++;
$age{Katie} = 6;
</pre><HR></BLOCKQUOTE>


constructor -- a method (usually a class method) that creates an object. This function is usually named "new".

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


package Foo;
sub new { # this is a typical constructor
my $class = shift; # probably 'Foo'
my $obj = { # a hash reference
NAME => "Jeff",
AGE => shift | | 0,
};
return bless $obj, $class;
}
</pre><HR></BLOCKQUOTE>


reference -- a data type in Perl that contains information about another data type. A reference must be dereferenced to get at the data it refers to. References, in double-quoted context, return a string like "SCALAR(0x12345)" or "ARRAY(0x23456)" (or "ClassName=TYPE(0x65432)" for objects) that state what data is being referred to, and what the location in memory of that data is.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$name = "jeff";
$ref_to_name = \$name;
print $$ref_to_name; # "jeff"
$$ref_to_name = "Jeff";
print $name; # "Jeff";

@nums = (1,4,9,16);
$ref_to_nums = \@nums;
print $ref_to_nums->[2]; # 9
print $$ref_to_nums[2]; # 9
push @$ref_to_nums, 25;
print "@nums"; # "1 4 9 16 25"
</pre><HR></BLOCKQUOTE>

[This message has been edited by japhy (edited 06-02-2000).]

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives