CGI and Perl

Other New Features in Perl5

Now let's take a look at some other new features that you get with Perl5. Newer ones are being considered on an ongoing basis, and may not be mentioned, but the ones that follow are now formally part of the language. BEGIN and END Routines The BEGIN and END statements provide the scriptor with a means to implement certain functionality in the Perl program as it is being compiled or just after it exits. Anything you place within the BEGIN{} block is guaranteed to be executed before any other statement in your program. It executes at the time the program is being compiled. The following gives a simple example:

print "done that\n";
 BEGIN { print "been there, "; }
 # prints: been there, done that

Likewise, anything you place in an END{} block is executed after every other statement in the program has executed and just before Perl exits. Multiple END{} blocks are executed in the reverse order of execution, as follows:

END { print "I\'ll see you in my dreams\n"; }
 END { print "Irene.  "; }
 print "Goodnight, ";
 # prints: Goodnight, Irene.  I'll see you in my dreams.

use() and no() Statements The use() statement imports symbols and/or semantics from the named package into your program by aliasing subroutines and/or variable names from the package into the current package's namespace. When you say

use Module;

in your program, it's the same as saying

BEGIN{ require Module; import Module List};

where Module has exported some List of methods (subroutines) via its @EXPORT or @EXPORT_OK
arrays, and you're making them part of your current package namespace (main, in this case). Note that the import method is not a builtin, but is actually a method itself, from the Exporter module. Much more on this later in this chapter.

You can also specify that nothing be imported by giving an empty list to use(), like this:

use SomeModule ();

Then you have access to all the methods of the package but only via the full name of the method or a blessed reference to the package. I discuss these details later in the chapter.

When you use one or more of the pragmatic modules, the use() statement imports semantics instead of symbols. For instance,

use strict subs;

This statement causes a compile-time error if you try to use a bare word identifier that isn't a subroutine, unless it appears in curly braces or on the left side of the => operator. These pragmas are then in effect through the end of the file or until the corresponding no() statement turns them off, as in the following:

no strict subs;

See PERLMOD and PERLDSC for more details on the pragmatic modules, which were previously mentioned when describing the new warnings and stricture, and PERLVAR for the official definition of use() and no(). There's also the POD within strict.pm for full documentation of the strict pragmas. The strict.pm module is installed with the rest of the Perl modules in @INC. The New :: Operator and the -> Operator These :: and -> operators are provided as a means for invoking the methods within a given package. You should use the Package::subroutine() syntax instead of the older, deprecated Package'subroutine() syntax; this new syntax provides the same semantics. This operator also works when you're accessing variables within a package, such as $Package::scalar, @Package::array, and %Package::hash, just like the older, single tick operator does. Again though, the single tick operator may not be part of the language forever. It's better to use the :: operator in all new code.

The -> operator provides a means to invoke the methods of a class or package, along with any methods of any of its parent classes, using a blessed reference to the package as well as serving as a post-fix dereference operator for any general reference. You will learn about this operator in detail later in this chapter. \U, \L, uc(), lc(), ucfirst(), and lcfirst() Operators The \U, \L, uc(), lc(), ucfirst(), and lcfirst() operators enable you to operate on strings, modifying them to be all uppercase, all lowercase, or have just the first letter of the string be uppercase. The \U and \L operators work within the double-quoted string, and the others take strings as arguments and return modified copies. For example,

$ucstring = "FOO";
 $lcstring = lc($ucstring);
 print $ucstring, ` `, $lcstring, ` `, ucfirst($lcstring),"\n"
 #prints:
 FOO foo Foo

Closures A closure is implemented as an anonymous subroutine or a reference to a subroutine. It is generally declared using a reference, which is then used to invoke the closure. Consider the following example:

sub newanon{
 my $foo = shift;
 my $anonsub = sub {
     my $arg = shift;
     print "Hey, I'm in an $foo $arg\n";
 };
 return $anonsub;
 }
 $closure = &newanon(`anonymous');
 { # new lexical scope
 &$closure(`subroutine');
 }
 # prints: Hey, I'm in an anonymous subroutine.

A lexical variable declared within a closure remains intact in future invocations of the subroutine, even if invoked outside the lexical scope of the declaration. See PERLSUB and PERLREF for detailed examples and descriptions of closures. Multiple Simultaneous DBM Implementations The Perl programmer can now access a number of DBM implementations simultaneously within a program. Perl is now shipped with SDBM, and consideration is being given to including the Berkeley DB implementation by default with Perl, but this has not been implemented yet. GDBM, NDBM, and ODBM are also available, if you have them on your machine. Having simultaneous DBM implementations makes it easy to convert from one DBM format to another within the same program.

You should note that the older dbmopen() function has been deprecated in favor of the tie() function. See PERLFUNC for more details on implementing a tie()'d DBM hash. Flags on #! Line Any regular Perl command-line options (flags) appended after the

#!/usr/bin/perl

line in a program are now correctly interpreted, even if the script isn't executed directly. The startup line:

#!/usr/bin/perl -d

will, for instance, invoke the debugger each time the script is run.

by BrainBellupdated
Advertisement: