Call it an indulgance :)

I call it "confusing" because, well, it confuses me. :-)

Afterall, they aren't really subs, right? You can have more than one BEGIN (et al) block without concern for redefining previous ones. And, my $arg = shift; will shift off @ARGV rather than @_ as you would expect in a sub. Heck, you can't even explicitly call it!¹ And in those ways, they differ significantly from TIE* and family.

Frankly, and maybe I shouldn't admit this, I was surprised putting sub in front even worked. I always think of BEGIN, INIT, CHECK, and END as "special blocks" in a category of their own. I think I've been led that way by the documentation which uses the term "block" to describe them.

1. And trying to opened up a real can of worms. Apparently, even

sub main::BEGIN { print "X\n" }
is treated as a BEGIN block. You can jump through hoops to get a sub called BEGIN if you try:
*main::BEGIN = sub { print "X\n" }; &BEGIN;
but try
*main::BEGIN = sub { print "X\n" }; BEGIN();
Prototype mismatch? Ok, so put the prototype in...
*main::BEGIN = sub () { print "X\n" }; BEGIN();
That clears the complaint up just fine but our sub is never called. And while we are trying dumb things, take a look at this:
perl -e 'BEGIN' # Syntax error. perl -e 'BEGIN; # No error. perl -e 'BEGIN()' # Syntax error. perl -e 'BEGIN();' # No error.
Blech! Blech! BLECH!

After all this, I'm inclined to think that sub BEGIN should be treated as an error if not a regular subroutine. (Of course, given the current state of affairs, we couldn't exactly start treating it as a regular subroutine now, could we? So I think it should be an error.)

-sauoq
"My two cents aren't worth a dime.";

In reply to Re: Re: Re: Re: Finding dependancies of a script by sauoq
in thread Finding dependancies of a script by fletcher_the_dog

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.