6.1 Using Objects

     

You can declare a class in one of two ways. The most familiar syntax for class declaration ends in a semicolon. The rest of the file after the declaration defines the class. With this syntax there can be only one class or module declaration in a file.

 class Heart::Gold; # class definition follows  . . . 

The other syntax for class declaration wraps the definition in a block. You can have as many of these as you like in a file, and even embed one class within the block of another.

 class Heart::Gold {     # class definition enclosed      . . .  } 

With a file definition, all code that follows the class declaration is defined in the Heart::Gold namespace. With a block definition, everything within the block is defined in the class's namespace.

To create a new object from a class, simply call its new method. The default new method in the universal base class Object creates and initializes an object.

 $ship = Heart::Gold.new(length => 150); 

There's a shortcut for typed variables so you don't have to give the name of the class twice:

 my Heart::Gold $ship .= new(length => 150); 



Perl 6 and Parrot Essentials
Perl 6 and Parrot Essentials, Second Edition
ISBN: 059600737X
EAN: 2147483647
Year: 2003
Pages: 116

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net