in reply to which use for any Perl elements?

In almost every case, if you don't know why you should use it, you probably shouldn't be using it. I know this sounds glib, but it's really the truth.
  1. BEGIN/END ... use these when you ABSOLUTELY HAVE TO have something occur before literally anything else or after literally everything else. In 10 years, I have used BEGIN and END maybe 10 times. I have used INIT once and I have never used CHECK. In practice, most Perl programmers will never use these phase things. Ever.

    The only exception to that is I will often put this in certain scripts:

    my $dbh = DBI->connect( ... ) or die $DBI::errstr; END { $dbh->disconnect if $dbh }
  2. globs ... In Perl4, they were the only ways to create multi-dimensional data structures. Now, we have references. Also, in early Perl5s, they were the only ways to pass filehandles to subroutines. Now, we have lexical filehandles.

    Now, the only thing I use them for is to create subroutines on the fly, and I do this quite a bit. Something like *foo = sub { print "foo\n" };. In every other case, you will use $a / @a / %a.

  3. AUTOLOAD ... This is probably the feature of Perl that is least needed and most overused. I have used AUTOLOAD exactly once in production code, and that was in the same CPAN module I used INIT. Some people love it, some people don't. I'm in the camp that says "If I need a catchall bucket, I probably didn't design right." So, using AUTOLOAD is a red flag to me saying I probably need to do more brainwork and less fingerwork.

Does that help?

Being right, does not endow the right to be rude; politeness costs nothing.
Being unknowing, is not the same as being stupid.
Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Replies are listed 'Best First'.
Re^2: which use for any Perl elements?
by ryantate (Friar) on Dec 03, 2004 at 23:25 UTC

    Something like *foo = sub { print "foo\n" };

    Sorry, I have never understood typeglobs either: why would this be preferable to my $foosub = sub { print "foo\n" };?

      $a = sub { ... }; # called: $a->(); *b = sub { ... }; # called: b();

      Ted Young

      ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)
Re^2: which use for any Perl elements?
by daggerquill (Initiate) on Dec 07, 2004 at 04:58 UTC
    This thread seems fairly dead, but I can't believe nobody mentioned what is probably the most frequent use of BEGIN: calling CGI::Carp qw{ fatalsToBrowser } in a BEGIN allows compile-time errors to be sent to the browser, which is an incredibly useful feature for debugging. Otherwise, compile-time errors simply generate a 500 Internal Server Error, and debugging requires digging through the the logs.