Defining and Using Objects

A class usually provides a method for creating an object as well as methods for defining the behavior of an object. Although you may name the creation method whatever you wish in most Perl dialects, in .NET it must be named new in honor of both C++ and Java, where it is likewise mandatory.

The data contained in an object is composed of many pieces. A Student object, for example, might contain a NAME, a MAJOR, and a set of COURSES. One way to model this in Perl is to create an anonymous hash and then retrieve items from it. See the folder Object.

 % type object.pl # # object.pl # $student = { NAME => Mike, MAJOR => Math, COURSES => [Calc, Trig, DiffE ] }; print $student->{NAME},"\n"; # the name print $student->{MAJOR},"\n"; ...

Get Programming PERL in the .NET Environment now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.