in reply to which use for any Perl elements?

Hello,

BEGIN blocks ensure that their contents are executed during compilation. It turns out that the "use" statement is really a shorthand for:

BEGIN { require Foo; Foo->import(@LIST) }

The BEGIN block should be used for initialization that needs to occur early in your program. In most cases, initializing your variables, etc, at the top of your package is just fine.

END is a little easier to provide some examples for. END is used mostly for tear down and clean up. If you module needs to do something at the end of the program's life, an END block is a good choice. For example, this can be used to close database handles or file handles that reamin open throughout the life of your program.

BEGIN and END are quite often used in one-liners that take advantage of perl's -n and -p options. -p effectivly wraps your code in the following:

while (<>) { # ... your code here ... print $_; }

(Note the -n is the same as -p without the final print statement). So, if you are writing a one-line like:

perl -p -e "$_ = reverse $_" f1

This would print each line in f1 reversed (eg. ABC would be printed CBA). Now, if you needed to do some initialization ahead of time, or print a summary, the BEGIN and END blocks come into play:

print -p -e "$_ = reverse $_; $i ++; END { print qq'Total lines $i.' } +" f1

This would print your reversed lines and then tell you how many lines were in the file afterwards.

Typeglobs can be used to mess with the symbol table, a magical hash table that stores all global variables (not those declared with my). By far the most common use is to create an alias. An alias is a variable, that when updated also updates its source. For example:

$a = 6; *b = \ $a; $b = 5; print $a; # prints 5 @a = (1, 2, 3); *b = \ @a; pop @b; print "@a"; # prints 1 2

AUTOLOAD is used in a most to catch undeclared method/sub invocations in a package. For example (perhapse the most commonly sited example, let's say we wanted to make a package that gave us a bunch of shell commands as perl subs. For example:

use Shell; $a = Shell::cat("file.text"); Shell::cp("a.txt", "b.txt");

Well, you could impliment every single shell command as a sub that calls the appropriate shell command:

package Shell; sub cat { `cat @_` } sub cp { `cp @_` }

But that would be a nightmare. Instead, we can use an AUTOLOAD to trap all calls to Shell package:

package Shell; sub AUTOLOAD { $AUTOLOAD =~ s/^.*:://; # Strip out the package name from the sub +'s name (in this case Shell::) `$AUTOLOAD @_` }

Well, these examples are a little contrived and seriously simplified to show you where you might use them.

I will update this reply if I can think of any better examples.

Ted Young

($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)

Replies are listed 'Best First'.
Re^2: which use for any Perl elements?
by Anonymous Monk on Dec 03, 2004 at 15:23 UTC
    (post is a little bit irrelevant to the questions asked)

    -p (and -n) wraps your code in:

    while (<>) { # ... your code here ... } continue { print $_; }