Perl FAQ 1.3: What features does perl5 provide over perl4?
Perl FAQ 1.3
What features does perl5 provide over perl4?
If you get the newest source (from any of the main FTP sites), you will
find a directory full of man pages (possibly to be installed as section
1p and 3pm) that discuss the differences, new features, old
incompatibilies and much more. Here, however, are some highlights as
to the new features and old incompatibilities.
Enhanced Usability: Perl code can now be written in a much more
legible style. Regular expressions have been enhanced to allow
minimal matches, conditionals, and much more. Cryptic variable
names (although still supported) have been aliased to new
nmemonics, using the ``English'' module, allowing old scripts to run
and new scripts to be readable. Error messages and optional
warnings are more informative and will catch many common mistakes.
See the
perldiag(1)
man page, which contains pithy prose from Larry Wall* on each and every possible muttering perl might spout at you.
Simplified Grammar: The new yacc grammar is one half the size of
the old one. Many of the arbitrary grammar rules have been
regularized. The number of reserved words has been cut by 2/3.
Lexical Scoping: Perl variables may now be declared within a
lexical scope, similar to C's ``auto'' variables. This is a
great improvement on efficiency and contributes to better
privacy. See the my() entry in
perlfunc(1)
.
Arbitrarily nested data structures: Full fledged multidimensional
arrays. Any scalar value, including an array element, may now
contain a reference to any other variable or subroutine.
Easily created anonymous variables and subroutines. See
perlref(1)
.
Modularity and Reusability: The Perl library is now defined in
terms of modules which can be easily shared among various
packages. Packages can import any or all of a module's published
interface. See perlmod(1), perlsub(1), and Exporter(3pm) .
Object-oriented programming: A package can function as a class.
Dynamic multiple inheritance and virtual methods are supported
in a straight-forward manner with little new syntax. Filehandles
are now treated as objects. See
perlobj(1),
perlmod(1), and
FileHandle(3pm)
.
Embeddable and Extensible: Perl can be easily embedded in C/C++
applications, and can either call or be called by your routines
through a documented interface. The XS preprocessor is provided to
make it easy to glue your C/C++ routines into Perl. Dynamic
loading of modules is supported. See
perlapi(1),
perlcall(1), and
DynaLoader(3pm)
.
POSIX compliant: A major new module is the POSIX module, which
provides access to all available POSIX routines and definitions.
See
POSIX(3pm).
Package constructors and destructors: The new BEGIN and END blocks
provide means to capture control as a package is being compiled and
after the program exits. As a degenerate case, they work just like
awk's BEGIN and END when you use the -p or -n switches. See
perlmod(1).
Multiple simultaneous DBM implementations: A perl program now has
access to DBM, NDBM, SDBM, GDBM and Berkeley DB files in the same
script. The dbmopen interface has been generalized to allow any
variable to be tied to an object class which defines its access
methods. tie/untie now preferable to dbmopen/dbmclose. See the
tie() entry in
perlfunc(1)
and the
DB_File(3pm)
man pages.
Subroutine definitions may now be autoloaded: The AUTOLOAD mechanism
allows any arbitrary semantics to undefined subroutine calls. See
the section on
Autoloading in the
perlsub(1)
manpage.
Regular Expression Enhancements: Qualifiers may be followed by a ``?''
to signify that they should be non-greedy. A ``?'' directly after
an opening paren indicates non backreference grouping and the next
character determines the purpose of the match (?:a|b|c) will match
any of a b or c without producing a backreference, (?=stuff) does
a non-eating look ahead to assure that the next thing is stuff,
(?!nonsense) looks ahead to assure that the next thing must not
be ``nonsense''. Embedded whitespace and comments for readability.
A consistent extensibility mechanism has been added that is
upwardly compatible with all old regexps. Variables may now be
interpolated literally into a pattern with \Q or the quotemeta
function, which works like \U but backwhacks non-alphanumerics.
New m and s ``flags'' for pattern matching force multi- or
single-line matching. The /s makes `.' match `\n'. \A and
\Z anchor matches to the beginning and end of a string and ignore
multiline semantics. \G matches where the previous m//g or s///g
left off.
The -w (warnings) switch is much more informative.
References and Objects (see t/op/ref.t) for examples.
=> is a synonym for comma and helps group paired arguments, such
as initializers for associative arrays and named arguments to
subroutines.
All functions, even predeclared subroutines, are treated as list
operators or unary operators. Parens are optional.
Flattened interpreter: Compare perl4's eval.c with perl5's pp.c.
Compare perl4's 900 line interpreter look with perl5's one line.
eval is now treated like a subroutine call, meaning (among other
things) you can return from it.
format value lists may be spread over multiple lines with a do {}
block.
flags on the #! line are interpreted even if the script wasn't
invoked directly.
?: is now an lvalue.
list context now propogates to the right side of && and ||, and
as the 2nd and 3rd arguments of ?:.
preferred package delimiter now :: rather than '.
new and and or operators, like && and || but with a lower
precedence than comma, so they work better with list operators.
New functions abs(), chr(), uc(), ucfirst(), lc(), and lcfirst()
require(number) checks to see that the version is at least that
version
qw//, which is equivalent to split(' ', q//)
assignment of a reference to a glob replaces the single element
of the glob corresponding to the reference type:
*foo = \$bar;
*foo = \&bletch;
filehandle methods are supported:
output_autoflush STDOUT 1;
Autoload stubs can now call the replacement subroutine with
goto &realsub.
Subroutines can be defined lazily in any package by declaring
an AUTOLOAD routine, which will be called if a non-existent
subroutine is called in that package.
use and no subsume many features. use Module LIST is
short for BEGIN { require Module; import Module LIST }no is identical, except that it calls unimport instead.
use integer and variations of use strict [vars,refs,subs]
were implemented through new modules.