- Perl 6 Deep Dive
- Andrew Shitov
- 471字
- 2021-07-03 00:05:41
Classes
To make the user experience better, let's take a look at another important example of where the syntax of Perl changes in Perl 6.
Traditionally, object-oriented programming is done in Perl 5 with the help of the so-called blessed hashes. Data members of an object are elements of the hash, and the blessed reference to that hash may be used to call a method on an instance of the class. The following example shows you what to do to define a class and create an instance of it in Perl 5:
package MyClass; sub new { my ($class) = @_; my $this = { counter => 0 }; bless $this, $class; return $this; } sub inc { my ($this) = @_; $this->{counter}++; return $this->{counter}; } 1;
So far, the class named MyClass has two methods—new, to create a new instance, and inc, to increment the counter and return the new value. When dealing with Perl 5's classes, don't forget to return a true value at the end of the module, and that is the goal of the 1 in the last line of the file.
In the main program, you can use MyClass by creating an instance and calling methods on the variable as follows:
use MyClass; my $var = MyClass->new; print $var->inc, "\n"; print $var->inc, "\n"; print $var->inc, "\n";
The implementation of the object-oriented things in Perl 5 was another obstacle for newcomers who may have had an experience of working with classes in other languages but were confused by the way Perl 5 created them.
Classes in Perl 6 are way more familiar to developers who have worked with other object-oriented programming languages.
This is how you define the same class, as shown in the preceding example, in Perl 6:
class MyClass { has $!counter; method inc() { $!counter++; return $!counter; } }
As you see, the whole class is defined within the pair of braces. Its data members are explicitly declared with the has keyword, and there's no need to return 1 at the end of the file.
Now, create an object of the class and increment the internal counter three times, like we did in the Perl 5 example earlier. This is how you do it in Perl 6:
my $var = MyClass.new; say $var.inc; say $var.inc; say $var.inc;
Do not focus on the details yet because it will all be explained in later chapters.
So far, we've seen three examples of where it was desired to improve the syntax of Perl 5.
To see more examples of changes between Perl 5 and Perl 6, you may refer to a few articles grouped under the title 'Perl 5 to Perl 6 guide' in the documentation of Perl 6 at https://docs.perl6.org/language.html, which is dedicated to that specific topic:

- Raspberry Pi for Python Programmers Cookbook(Second Edition)
- R語言游戲數(shù)據(jù)分析與挖掘
- 數(shù)據(jù)結(jié)構(gòu)(Python語言描述)(第2版)
- Vue.js 3.0源碼解析(微課視頻版)
- C語言程序設(shè)計(jì)學(xué)習(xí)指導(dǎo)與習(xí)題解答
- Haskell Data Analysis Cookbook
- 深度學(xué)習(xí):Java語言實(shí)現(xiàn)
- PLC應(yīng)用技術(shù)(三菱FX2N系列)
- Node.js:來一打 C++ 擴(kuò)展
- Java零基礎(chǔ)實(shí)戰(zhàn)
- 寫給程序員的Python教程
- IDA Pro權(quán)威指南(第2版)
- JavaScript+jQuery網(wǎng)頁特效設(shè)計(jì)任務(wù)驅(qū)動(dòng)教程
- Go語言從入門到精通
- .NET 4.0面向?qū)ο缶幊搪劊簯?yīng)用篇