diff options
author | David Golden <dagolden@cpan.org> | 2009-10-06 06:48:48 -0400 |
---|---|---|
committer | David Golden <dagolden@cpan.org> | 2009-10-06 12:41:09 -0400 |
commit | 6fa4d285bff5644bebb95aff09143322042282cc (patch) | |
tree | 3e353ca1e3a17967f6dd473bcb49a7269c880355 /op.c | |
parent | cc4c9faad0767bbb62e32c96638b5ce02dde234e (diff) | |
download | perl-6fa4d285bff5644bebb95aff09143322042282cc.tar.gz |
Add 'package NAME VERSION' syntax
This patch adds support for setting the $VERSION of a namespace
when the namespace is declared with 'package'. It eliminates the
need for 'our $VERSION = ...' and similar constructs. E.g.
package Foo::Bar 1.23;
# $Foo::Bar::VERSION == 1.23
There are several advantages to this:
* VERSION is parsed in *exactly* the same way as 'use NAME VERSION'
* $VERSION is set at compile time
* Eliminates '$VERSION = ...' and 'eval $VERSION' clutter
* As it requires VERSION to be a numeric literal or v-string
literal, it can be statically parsed by toolchain modules
without 'eval' the way MM->parse_version does for '$VERSION = ...'
* Alpha versions with underscores do not need to be quoted; static
parsing will preserve the underscore, but during compilation, Perl
will remove underscores as it does for all numeric literals
During development of this, there was discussion on #corehackers and
elsewhere that this should also allow other metadata to be set such as
"status" (stable/alpha) or "author/authority". On reflection, those
metadata are not very well defined yet and likely should never be
encoded into Perl core parsing so they can be freely changed in the
future. (They could perhaps be achieved via a comment on the same line
as 'package NAME VERSION'.)
Version numbers, however, already have a very specific definition and
use defined in the core through 'use NAME VERSION'. This patch merely
provides appropriate symmetry for setting $VERSION with the exact same
parsing and semantics as 'use'.
It does not break old code with only 'package NAME', but code that
uses 'package NAME VERSION' will need to be restricted to perl 5.11.X.
This is analogous to the change to open() from two-args to three-args.
Users requiring the latest Perl will benefit, and perhaps N years from
now it will become standard practice when Perl 5.12 is targeted the
way that 5.6 is today.
The patch does not prevent 'package NAME VERSION' from being used
multiple times for the same package with different version numbers, but
nothing prevents $VERSION from being modified arbitrarily at runtime,
either, so I see no urgen reason to add limitations or warnings so
long as Perl uses a global $VERSION variable for package version
numbers.
I am posting this patch to the p5p list for discussion and review. If
there seems to be general assent (or lack of dissent), I will go ahead
and commit the patch to blead.
Diffstat (limited to 'op.c')
-rw-r--r-- | op.c | 12 |
1 files changed, 12 insertions, 0 deletions
@@ -3816,6 +3816,18 @@ Perl_package(pTHX_ OP *o) #endif } +void +Perl_package_version( pTHX_ OP *v ) +{ + dVAR; + PERL_ARGS_ASSERT_PACKAGE_VERSION; + SV *const version = cSVOPx(v)->op_sv; + SV *const pkgname = sv_mortalcopy(PL_curstname); + sv_catpv(pkgname, "::VERSION"); + sv_setsv( get_sv(SvPV_nolen(pkgname),TRUE), version ); + op_free(v); +} + #ifdef PERL_MAD OP* #else |