in reply to __END__ without __BEGIN__?

For one thing, it would make the compiler slower and less stable. Consider:
BEGIN { my $var = do_some_processing(); } __BEGIN__ # start of real processing.. but the compiler has already processed th +e BEGIN block! # ..real code here..
The compiler would have to forget everything it did in the BEGIN block, and if there were side effects (for example, writing to files), all of that would have to be undone.

I suppose your hypothetical __BEGIN__ tag could cause the compiler to simply ignore everything until such a tag is seen.. but what if the tag doesn't exist? the compiler would have to either re-read the file (breaking STDIN, as per merlyn's comment Re: __END__ without __BEGIN__? above), or would have to store a potentially enormous file (plus dependencies) in memory before compilation could truly begin*. This issue doesn't exist with __END__ because the compiler can fall off the end of the file and thus not need to see an __END__ tag.

Of course this could all be complete hooey. Larry may have just left out __BEGIN__ because to include it would be extra work ;)

* - It might do this already, but without the __BEGIN__ tag compilation can begin as soon as perl figures out it has something to work with, which is at least potentially faster.

Update: changed 'more unstable' to 'less stable' - did not mean to imply the compiler was already unstable ;)

Replies are listed 'Best First'.
Re^2: __END__ without __BEGIN__?
by Anonymous Monk on Mar 30, 2005 at 12:12 UTC
    the compiler would have to either re-read the file (breaking STDIN, as per merlyn's comment
    Why is this an argument? If "this feature can't be used on STDIN", why does perl have a -i option? That can't be applied to STDIN either - and -i is then silently ignored. If one were to implement such a __BEGIN__ token (not that I'm advocating this), it would be rather simple to decide what should be done in case of a program passed from STDIN. Heck, one can't seek the DATA file handle from a program found on STDIN either.
    would have to store a potentially enormous file (plus dependencies) in memory before compilation could truly begin
    Considering that the optree + perl binary + data for perl itself usually measures at least a couple of megabytes, this isn't a real argument, is it? While it's not impossible to create a file of a few megabytes that, when compiled, actually takes less memory, it's not something to worry about.

    I think the only reason this feature isn't there is because it's not useful enough to implement it. Besides, there's already 'perl -x'. Breaking STDIN and "takes too much memory" aren't real arguments.

      I think the only reason this feature isn't there is because it's not useful enough to implement it.

      Maybe you should have read the part of my comment where I said that maybe the feature wasn't implemented because it was too much work.