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.


In reply to Re: which use for any Perl elements? by dragonchild
in thread which use for any Perl elements? by mat21

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.