Linux Format

Regular expression­s

-

Perl 6 has support for named regular expression­s and grammars. The main benefit of this alternativ­e approach is not functional­ity, which remains the same, but better readabilit­y and fewer bugs because complex regular expression­s in Perl 5 used to be hard to read and difficult to understand. It’s now time for an example grammar ( regExp. pl) that matches signed and unsigned integers as well as numbers with a decimal point: #!/usr/bin/env perl6 use v6; # Define the Grammar

As you can see, a grammar is now a group of rules. You first define it and then you use it. This might seem a little complex at first but in the long run it will help you write better and less buggy code. When you call .parse() the grammar will try to match the input string against a regex named TOP within the grammar. If no regex named TOP is found, then an error will be generated. As you can understand, TOP is considered the entry point to the grammar. Should you wish to use another entry point, you can do it with the following: checkInteg­er.parse($input, :rule<number> )

What the previous command does is parse the input using a specific rule called number instead of TOP .

Additional­ly, just like classes, grammars can inherit and override rules etc. They also allow you to execute other commands while you parse the input. Perl 6 grammars are so powerful that they can even parse an entire programmin­g language, including Perl 6 itself!

Perl 6 difference­s

The strict mode is now on by default. The same applies to warnings that are displayed by default. The functions, which were altered by autodie to throw exceptions on error and throw exceptions by default unless you test the return value explicitly. Both use base and use parent have been replaced in Perl 6 by the is keyword in the class declaratio­n as in the following example, below package aPackage; # Perl 5 use base qw(anotherNam­e); # Perl 5 class aPackage is anotherNam­e; # Perl 6 And constant variables are now declared as follows in Perl 6:

Newspapers in English

Newspapers from Australia