| 1 | # Copyright 2008, 2009, 2010, 2011, 2012, 2015 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of constant-defer. | ||
| 4 | # | ||
| 5 | # constant-defer is free software; you can redistribute it and/or modify it | ||
| 6 | # under the terms of the GNU General Public License as published by the Free | ||
| 7 | # Software Foundation; either version 3, or (at your option) any later | ||
| 8 | # version. | ||
| 9 | # | ||
| 10 | # constant-defer is distributed in the hope that it will be useful, but | ||
| 11 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| 12 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 13 | # for more details. | ||
| 14 | # | ||
| 15 | # You should have received a copy of the GNU General Public License along | ||
| 16 | # with constant-defer. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | package constant::defer; | ||
| 19 | use strict; | ||
| 20 | |||
| 21 | use vars '$VERSION'; | ||
| 22 | $VERSION = 6; | ||
| 23 | |||
| 24 | sub import { | ||
| 25 | my $class = shift; | ||
| 26 | $class->_create_for_package (scalar(caller), @_); | ||
| 27 | } | ||
| 28 | sub _create_for_package { | ||
| 29 | my $class = shift; | ||
| 30 | my $target_package = shift; | ||
| 31 | while (@_) { | ||
| 32 | my $name = shift; | ||
| 33 | if (ref $name eq 'HASH') { | ||
| 34 | unshift @_, %$name; | ||
| 35 | next; | ||
| 36 | } | ||
| 37 | unless (@_) { | ||
| 38 | require Carp; | ||
| 39 | Carp::croak ("Missing value sub for $name"); | ||
| 40 | } | ||
| 41 | my $subr = shift; | ||
| 42 | |||
| 43 | ### $constant::defer::DEBUG_LAST_SUBR = $subr; | ||
| 44 | |||
| 45 | my ($fullname, $basename); | ||
| 46 | if ($name =~ /::([^:]*)$/s) { | ||
| 47 | $fullname = $name; | ||
| 48 | $basename = $1; | ||
| 49 | } else { | ||
| 50 | $basename = $name; | ||
| 51 | $fullname = "${target_package}::$name"; | ||
| 52 | } | ||
| 53 | ## print "constant::defer $arg -- $fullname $basename $old\n"; | ||
| 54 | $class->_validate_name ($basename); | ||
| 55 | $class->_create_fullname ($fullname, $subr); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | sub _create_fullname { | ||
| 60 | my ($class, $fullname, $subr) = @_; | ||
| 61 | my $run = sub { | ||
| 62 | unshift @_, $fullname, $subr; | ||
| 63 | goto &_run; | ||
| 64 | }; | ||
| 65 | my $func = sub () { | ||
| 66 | unshift @_, \$run; | ||
| 67 | goto $run; | ||
| 68 | }; | ||
| 69 | no strict 'refs'; | ||
| 70 | *$fullname = $func; | ||
| 71 | |||
| 72 | ### $constant::defer::DEBUG_LAST_RUNNER = $run; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub _run { | ||
| 76 | my $fullname = shift; | ||
| 77 | my $subr = shift; | ||
| 78 | my $run_ref = shift; | ||
| 79 | ### print "_run() $fullname $subr\n"; | ||
| 80 | |||
| 81 | my @ret = &$subr(@_); | ||
| 82 | if (@ret == 1) { | ||
| 83 | # constant.pm has an optimization to make a constant by storing a scalar | ||
| 84 | # value directly into the %{Foo::Bar::} hash if there's no typeglob for | ||
| 85 | # the name yet. But that doesn't apply here, there's always a glob from | ||
| 86 | # having converted a function. | ||
| 87 | # | ||
| 88 | # The function created only has name __ANON__ in its coderef GV (as | ||
| 89 | # fetched by Sub::Identify for instance). This is the same as most | ||
| 90 | # function creating modules, including Memoize.pm. Plain constant.pm | ||
| 91 | # likewise, except when it uses the scalar ref in symbol table | ||
| 92 | # optimization, in that case a later upgrade to a function gets a name. | ||
| 93 | # | ||
| 94 | my $value = $ret[0]; | ||
| 95 | $subr = sub () { $value }; | ||
| 96 | |||
| 97 | } elsif (@ret == 0) { | ||
| 98 | $subr = \&_nothing; | ||
| 99 | |||
| 100 | } else { | ||
| 101 | $subr = sub () { @ret }; | ||
| 102 | } | ||
| 103 | |||
| 104 | $$run_ref = $subr; | ||
| 105 | { no strict 'refs'; | ||
| 106 | local $^W = 0; # no warnings 'redefine'; | ||
| 107 | eval { *$fullname = $subr } or die $@; | ||
| 108 | } | ||
| 109 | goto $subr; | ||
| 110 | } | ||
| 111 | |||
| 112 | # not as strict as constant.pm | ||
| 113 | sub _validate_name { | ||
| 114 | my ($class, $name) = @_; | ||
| 115 | if ($name =~ m{[()] # no parens like CODE(0x1234) if miscounted args | ||
| 116 | |^[0-9] # no starting with a number | ||
| 117 | |^$ # not empty | ||
| 118 | }x) { | ||
| 119 | require Carp; | ||
| 120 | Carp::croak ("Constant name '$name' is invalid"); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | sub _nothing () { } ## no critic (ProhibitSubroutinePrototypes) | ||
| 125 | |||
| 126 | 1; | ||
| 127 | __END__ |