in reply to Re: constants within regular expression?
in thread constants within regular expression?

Constants are transformed into subroutines (at least in some cases, I'm not too familiar with them at all).

Constants created with constant.pm are indeed subs (in all cases.) They are made by aliasing the constant's name to an anonymous sub with an empty prototype the body of which is simply the supplied value. The empty prototype is a hint to perl that the sub is a candidate for inlining. In other words, you could create your own "constants" with code like

sub MY_CONSTANT () { 42 }
if you wanted.

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

Replies are listed 'Best First'.
Re: Re: Re: constants within regular expression?
by zengargoyle (Deacon) on Nov 26, 2003 at 07:00 UTC

    i don't know what the empty prototype implies for inlining, but it also keeps the sub from swallowing arguments.

    sub A () {1}; sub B {2}; print A + 1,$/; print B + 1,$/; __END__ prompt$ perl test.pl 2 2prompt$

    without the empty prototype subs wouldn't work very well for constants.

      i don't know what the empty prototype implies for inlining

      Read this and you will (from perldoc perlsub):

      Constant Functions Functions with a prototype of "()" are potential candidates for inlin- ing. If the result after optimization and constant folding is either +a constant or a lexically-scoped scalar which has no other references, then it will be used in place of function calls made without "&". Calls made using "&" are never inlined. (See constant.pm for an easy way to declare most constants.)