Приглашаем посетить
Маяковский (mayakovskiy.lit-info.ru)

7.8 Perl 5.005

Previous Table of Contents Next

7.8 Perl 5.005

Threads were added, but perl had to be compiled with a special option to use them; nevertheless lock became a reserved word. Regular expressions added look-behind, code evaluation, and non-backtracking matches. Pseudo-hashes were added (later to be regretted), and <> could read fixed-length records (look for $/ being set to a reference to an integer). It became possible to create compiled regular expression objects with qr//, and foreach was allowed as a suffix on a statement (e.g., $_++ for @stocks). The $^S variable (true inside eval() blocks) was added.

New modules in the core included the attrs pragma, base classes for tieing arrays and hashes, and Data::Dumper. The new fields pragma plus some internal magic makes it possible to catch references to invalid object attributes if objects were "typed"; for example, my Kangaroo $skippy.

7.8.1 Upgrading to Perl 5.005

Again, it is unlikely that you can quickly spot code that would be improved by the use of look-behind or the other new features in regular expressions. If the code contains a subroutine called lock, it should be renamed.

Where a foreach loop contains a single statement and the loop variable is either $_ or a variable scoped just to that loop, it can be turned into the postfixed form:


foreach ($text =~ /((?:\d+\.){3}\d+)/g)

{

  $ip{$1}++;

}

becomes:


$ip{$1}++ for $text =~ /((?:\d+\.){3}\d+)/g;

You can even extend this to multiple loop statements under the right circumstances:


foreach my $rock (@rocks)

{

  return "Paydirt" if is_gem($rock);

}

becomes:


is_gem($_) and return "Paydirt" for @rocks;

Note that you cannot chain postfixed control modifiers:


return "Paydirt" if is_gem($_) for @rocks;  # WRONG

The new object-oriented features in the next version of Perl are more useful for new applications than for retrofitting onto existing code.

    Previous Table of Contents Next