
japhy
Enthusiast
Jun 2, 2000, 7:12 AM
Post #4 of 4
(473 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).]
|