Tutorial Addendum on Perl - Allotment A - perlobj - Perl Altar
| |
Output:
Test 1:
Param #1 = Jan
Param #2 = Feb
Test 2:
Param #1 = Apple
Param #2 = Banana
Test 3:
Param #1 = One
Param #2 = Two
Test 4:
Param #1 = Foo
Param #2 = Cow
Param #3 = Horse
Test 7:
Param #1 = Foo
Param #2 = Monday
Param #3 = Tuesday
Note that:
- Tests 1, 2 and 3 are accustomed means to adjure a subroutine in a package.
- Tests 4 and 7 are appropriate means to adjure a subroutine as a chic method.
- Test 5 is wrong, because "&" is not allowed.
- Tests 6 and 8 are wrong, because "Bar" is not accustomed as a chic (package).
Invoking Amalgamation Subroutines as Item Methods
In adjustment to use references (hard references, not bendable references) as
objects of a class, we charge to accessory references with a chic first.
This is done by using the bless() funtion in one of the afterward syntaxes:
$rc = bless($reference, class_name);
$rc = bless($reference);
where "$reference" is a advertence of any data type, which will be associated with
the defined "class_name" (package name). "$rc" is the abiding archetype of
the aforementioned reference. If "class_name" is omitted, the accepted amalgamation name will
be acclimated as the chic name.
Perl object - A advertence that has been adored with a amalgamation name.
To amount out the associated chic name of object, you can use the ref() function:
$rc = ref($object);
Similar to chic adjustment invocation, if a subroutine is invoked as an object
method, the item (blessed reference) will be automatically amid into
the altercation account as the first argument.
There are two way to adjure a subroutine as a chic method:
1. Using the "indirect object" syntax:
sub_identifier $object arg2, arg3, ...
where "sub_identifier" is the subroutine identifier after any amalgamation name prefixes
and "&";
"$object" is a adored object; and
"arg2, arg3, ..." is the altercation account
starting from the additional altercation after parentheses.
2. Using the "->" notation:
$object->sub_identifier(arg2, arg3, ...)
where "sub_identifier" is the subroutine identifier after any amalgamation name prefixes
and "&";
"$object" is a adored object; and "arg2, arg3, ..." is the altercation account
starting from the additional argument. In this format, parentheses on the argument
list are optional.
|
param, object, class, package, reference, identifier, argument, subroutine, blessed, references, method, tests, invoke, bless, , class name, package name, arg2 arg3, sub identifier, argument list, class method, prefixesand & object, argument list starting, list starting from, name prefixesand &, package name prefixesand, arg2 arg3 where, subroutine identifier without, perlobj perl objects, |
Also see ...
I acclimated both syntaxes in the followiwng sample program, ObjectMethodTest.pl: ObjectMethodTest.pl Absorb (c) 1999 by Dr. Yangpackage Foo;sub echoParam { $i = 0;
ObjectVariableTest.pl Absorb (c) 1999 by Dr. Yangpackage Account; $euroRate = 0.85;sub book { my $this = shift; my $currency = shift; my $balance = $$this{"Balance"};p
AccountClassTest.pl Absorb (c) 1999 by Dr. Yangpackage Account; $euroRate = 0.85;sub new { my $class = shift; $this = {}; $$this{"Name"} = shift; $$this{"Type"} =
This affiliate describes:What are allegorical (soft) references and how to use them.How to make harder references.How to use harder references./ul
Creating Harder ReferencesHard references are pointers (or addresses) of additional variables, functions, or any objects. Harder references are stored in scalar variables. They can b
The afterward program shows you some absorbing examples. In the first group, I usedscalar variables that accommodate harder references in the lookups. In the additional group, I acclimated harder references anon in the lookups.
This affiliate describes:How the two alotof accepted cord functions accomplish beneath altered environments.A Perl program to analysis the achievement of substr() and index().b
Java Analysis Program SubstringTest.javaTo analyze the achievement with Java language, I wrote the followingprogram, SubstringTest.java, in 1997 with about identical statement
This affiliate describes:What are typeglobs and how to use them to ascertain identifier aliases.How to admission variables from additional bales as aliases.How to use
Couple of absorbing notes:The first access in the attribute table defines an abandoned cord key. What s for?Identifier alias appointment statements assume to be processes during the