Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

About Class::ObjectTemplate

Status
Not open for further replies.

skymj

Technical User
Apr 10, 2008
1
US
Hi,I just start to learn the OOP part of perl. I saw there is some package ObjectTemplate that could help you to construct a class. and in the package that it generates, some Syntax just confuses me, could you give me some detailed explanation, thanks:
sub one { # Accessor ...
my $name = ref($_[0]) . "::_one";
@_ > 1 ? $name->[${$_[0]}] = $_[1] # set
: $name->[${$_[0]}]; # get
}
especially what dose it mean by "set"&"get".
Thanks
 
The syntax for Class::ObjectTemplate may look different than most object code because that module uses anonymous arrays internally (keeping track of slots in the array emptied out thru deletions and reusing them) It has a nice long section in Advanced Perl Programming by Sriram Srinivasam.

Perldoc perlobj, perldoc perltoot, and perldoc perlref are all good document points to start, not to mention the Advanced Perl Programming book around pg 124.

The code snippet above, checks the arguements passed to it to see if there's a second arguement after the instance-method's name and if there is, it is assigned to that attribute, otherwise the attribute is returned.

Without the object wrapper, using $foo instead of PACKAGE_NAME->one, we could re-write it like this:

Code:
sub one
  {
  if (@_ > 1)
    { $foo=$_[1]; } # set $foo
  else
    { $foo; } # return value
  }

or using ternary operator

Code:
sub one
  { @_ > 1 ? $foo=$_[1] : $foo; }

Kordaff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top