Perl Packages - Part 4 (Functions)package street; sub new { my $self = {}; # create a hash reference
} $self = shift; } } The bless statement 'renames' the variable referenced by the $self variable. Originally, the $self variable referred to a HASH. Since the $class variable contains the string street, blessing the $self variable makes the HASH referred by the $self variable become a street variable. Now, if you execute the script, the new function returns a reference to a street package instead of a HASH variable. This is seen by the output printed from the main package. The last line of the script executes the printstreet function of the street package. For functions within a package, the first parameter is ALWAYS the reference to the hash it created on the new function. In our example, the printstreet function prints out the street element of the hash it created in the new function. When you run this script, the result will be: Package Name: street For more information on Packages, you can read: perltoot from the Perl documentation or my Self-Help Guide on Packages and Object Oriented Programming in Perl
The copyright of the article Perl Packages - Part 4 (Functions) in Perl is owned by Philip Yuson. Permission to republish Perl Packages - Part 4 (Functions) in print or online must be granted by the author in writing.
Go To Page: 1 2 Articles in this Topic Discussions in this Topic |