develooper Front page | perl.perl5.porters | Postings from July 2001

C<package;> is broken. Should it be deprecated?

Thread Next
Robin Houston
July 9, 2001 15:47
C<package;> is broken. Should it be deprecated?
Message ID:
Not many people know that you can say C<package;>, so that there's
no current package, even though it's clearly documented in L<perlfunc>.

At the moment it is broken in ever so many ways. We have not been inundated
with complaints from the masses.  I suggest that it be deprecated with a
warning, and removed from a future release. We could even fix some of the
worst brokenness as well, if we were feeling expansive.

I suppose I ought to give some examples of ways in which it is broken
at this point...

  perl -e 'package; sub {}'

will cause perl 5.6 to segfault; under bleadperl (and 5.6.1, I've heard)
it merely dies with the baffling warning:

Global symbol "__ANON__" requires explicit package name at -e line 1.

(baffling, that is, to a Perl programmer who isn't familiar with perl's
internals - such people are presumably our target audience.)

Since insofar as an anonymous sub has a package at all, that package could
reasonably be null ("no package"), this is an unreasonable and presumably
unintended restriction.

BEGIN blocks suffer from similar problems, and while that can be worked around
by writing
  sub main::BEGIN { ... }
that fact is undocumented and deeply wrong. Again, the BEGIN block ought to
be evaluated in "no package". 

use declarations suffer from the same problem, except that the workaround
is verging on the insane.

  sub main::BEGIN { require strict; strict->import(); }

And the error message is startlingly confusing to anyone who hasn't memorised
L<perlfunc>: Global symbol "BEGIN" requires explicit package name at -e line 1.

Furthermore, even supposing that this problem were fixed, importing symbols
into the current package is clearly impossible when there isn't one. How
could that be made to fail in an explanatory way? "Can't use an undefined
value as a symbol reference" would seem to be the simplest failure mode.
Again, utterly baffling.

I haven't embarked on a systematic attempt to find bits of perl's internals
which assume that there _is_ a current package. The examples above I or
others found by accident in a few minutes. Knowing about C<package;> is 
at best a way to impress enemies with your knowledge of Perl trivia.  Its
practical utility is extremely limited, its implementation is broken
and its very existence imposes a needless burden on all of perl's carers.

Let us live in anticipation of the day that we can say with pride
  PL_curstash != Nullhv
and in the meantime let us deprecate this mess.

Thanks for listening,

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About