This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
b429778507b991548257c5e99901b3e04299b8b3
[perl5.git] / dist / Locale-Maketext / lib / Locale / Maketext.pm
1
2 package Locale::Maketext;
3 use strict;
4 use vars qw( @ISA $VERSION $MATCH_SUPERS $USING_LANGUAGE_TAGS
5 $USE_LITERALS $MATCH_SUPERS_TIGHTLY);
6 use Carp ();
7 use I18N::LangTags ();
8 use I18N::LangTags::Detect ();
9
10 #--------------------------------------------------------------------------
11
12 BEGIN { unless(defined &DEBUG) { *DEBUG = sub () {0} } }
13 # define the constant 'DEBUG' at compile-time
14
15 # turn on utf8 if we have it (this is what GutsLoader.pm used to do essentially )
16 #    use if (exists $INC{'utf8.pm'} || eval 'use utf8'), 'utf8';
17 BEGIN {
18
19     # if we have it || we can load it
20     if ( exists $INC{'utf8.pm'} || eval { local $SIG{'__DIE__'};require utf8; } ) {
21         utf8->import();
22         DEBUG and warn " utf8 on for _compile()\n";
23     }
24     else {
25         DEBUG and warn " utf8 not available for _compile() ($INC{'utf8.pm'})\n$@\n";
26     }
27 }
28
29
30 $VERSION = '1.18';
31 @ISA = ();
32
33 $MATCH_SUPERS = 1;
34 $MATCH_SUPERS_TIGHTLY = 1;
35 $USING_LANGUAGE_TAGS  = 1;
36 # Turning this off is somewhat of a security risk in that little or no
37 # checking will be done on the legality of tokens passed to the
38 # eval("use $module_name") in _try_use.  If you turn this off, you have
39 # to do your own taint checking.
40
41 $USE_LITERALS = 1 unless defined $USE_LITERALS;
42 # a hint for compiling bracket-notation things.
43
44 my %isa_scan = ();
45
46 ###########################################################################
47
48 sub quant {
49     my($handle, $num, @forms) = @_;
50
51     return $num if @forms == 0; # what should this mean?
52     return $forms[2] if @forms > 2 and $num == 0; # special zeroth case
53
54     # Normal case:
55     # Note that the formatting of $num is preserved.
56     return( $handle->numf($num) . ' ' . $handle->numerate($num, @forms) );
57     # Most human languages put the number phrase before the qualified phrase.
58 }
59
60
61 sub numerate {
62     # return this lexical item in a form appropriate to this number
63     my($handle, $num, @forms) = @_;
64     my $s = ($num == 1);
65
66     return '' unless @forms;
67     if(@forms == 1) { # only the headword form specified
68         return $s ? $forms[0] : ($forms[0] . 's'); # very cheap hack.
69     }
70     else { # sing and plural were specified
71         return $s ? $forms[0] : $forms[1];
72     }
73 }
74
75 #--------------------------------------------------------------------------
76
77 sub numf {
78     my($handle, $num) = @_[0,1];
79     if($num < 10_000_000_000 and $num > -10_000_000_000 and $num == int($num)) {
80         $num += 0;  # Just use normal integer stringification.
81         # Specifically, don't let %G turn ten million into 1E+007
82     }
83     else {
84         $num = CORE::sprintf('%G', $num);
85         # "CORE::" is there to avoid confusion with the above sub sprintf.
86     }
87     while( $num =~ s/^([-+]?\d+)(\d{3})/$1,$2/s ) {1}  # right from perlfaq5
88     # The initial \d+ gobbles as many digits as it can, and then we
89     #  backtrack so it un-eats the rightmost three, and then we
90     #  insert the comma there.
91
92     $num =~ tr<.,><,.> if ref($handle) and $handle->{'numf_comma'};
93     # This is just a lame hack instead of using Number::Format
94     return $num;
95 }
96
97 sub sprintf {
98     no integer;
99     my($handle, $format, @params) = @_;
100     return CORE::sprintf($format, @params);
101     # "CORE::" is there to avoid confusion with myself!
102 }
103
104 #=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
105
106 use integer; # vroom vroom... applies to the whole rest of the module
107
108 sub language_tag {
109     my $it = ref($_[0]) || $_[0];
110     return undef unless $it =~ m/([^':]+)(?:::)?$/s;
111     $it = lc($1);
112     $it =~ tr<_><->;
113     return $it;
114 }
115
116 sub encoding {
117     my $it = $_[0];
118     return(
119         (ref($it) && $it->{'encoding'})
120         || 'iso-8859-1'   # Latin-1
121     );
122 }
123
124 #--------------------------------------------------------------------------
125
126 sub fallback_languages { return('i-default', 'en', 'en-US') }
127
128 sub fallback_language_classes { return () }
129
130 #--------------------------------------------------------------------------
131
132 sub fail_with { # an actual attribute method!
133     my($handle, @params) = @_;
134     return unless ref($handle);
135     $handle->{'fail'} = $params[0] if @params;
136     return $handle->{'fail'};
137 }
138
139 #--------------------------------------------------------------------------
140
141 sub failure_handler_auto {
142     # Meant to be used like:
143     #  $handle->fail_with('failure_handler_auto')
144
145     my $handle = shift;
146     my $phrase = shift;
147
148     $handle->{'failure_lex'} ||= {};
149     my $lex = $handle->{'failure_lex'};
150
151     my $value ||= ($lex->{$phrase} ||= $handle->_compile($phrase));
152
153     # Dumbly copied from sub maketext:
154     return ${$value} if ref($value) eq 'SCALAR';
155     return $value    if ref($value) ne 'CODE';
156     {
157         local $SIG{'__DIE__'};
158         eval { $value = &$value($handle, @_) };
159     }
160     # If we make it here, there was an exception thrown in the
161     #  call to $value, and so scream:
162     if($@) {
163         # pretty up the error message
164         $@ =~ s{\s+at\s+\(eval\s+\d+\)\s+line\s+(\d+)\.?\n?}
165                  {\n in bracket code [compiled line $1],}s;
166         #$err =~ s/\n?$/\n/s;
167         Carp::croak "Error in maketexting \"$phrase\":\n$@ as used";
168         # Rather unexpected, but suppose that the sub tried calling
169         # a method that didn't exist.
170     }
171     else {
172         return $value;
173     }
174 }
175
176 #==========================================================================
177
178 sub new {
179     # Nothing fancy!
180     my $class = ref($_[0]) || $_[0];
181     my $handle = bless {}, $class;
182     $handle->init;
183     return $handle;
184 }
185
186 sub init { return } # no-op
187
188 ###########################################################################
189
190 sub maketext {
191     # Remember, this can fail.  Failure is controllable many ways.
192     Carp::croak 'maketext requires at least one parameter' unless @_ > 1;
193
194     my($handle, $phrase) = splice(@_,0,2);
195     Carp::confess('No handle/phrase') unless (defined($handle) && defined($phrase));
196
197     # backup $@ in case it it's still being used in the calling code.
198     # If no failures, we'll re-set it back to what it was later.
199     my $at = $@;
200
201     # Copy @_ case one of its elements is $@.
202     @_ = @_;
203
204     # Look up the value:
205
206     my $value;
207     if (exists $handle->{'_external_lex_cache'}{$phrase}) {
208         DEBUG and warn "* Using external lex cache version of \"$phrase\"\n";
209         $value = $handle->{'_external_lex_cache'}{$phrase};
210     }
211     else {
212         foreach my $h_r (
213             @{  $isa_scan{ref($handle) || $handle} || $handle->_lex_refs  }
214         ) {
215             DEBUG and warn "* Looking up \"$phrase\" in $h_r\n";
216             if(exists $h_r->{$phrase}) {
217                 DEBUG and warn "  Found \"$phrase\" in $h_r\n";
218                 unless(ref($value = $h_r->{$phrase})) {
219                     # Nonref means it's not yet compiled.  Compile and replace.
220                     if ($handle->{'use_external_lex_cache'}) {
221                         $value = $handle->{'_external_lex_cache'}{$phrase} = $handle->_compile($value);
222                     }
223                     else {
224                         $value = $h_r->{$phrase} = $handle->_compile($value);
225                     }
226                 }
227                 last;
228             }
229             # extending packages need to be able to localize _AUTO and if readonly can't "local $h_r->{'_AUTO'} = 1;"
230             # but they can "local $handle->{'_external_lex_cache'}{'_AUTO'} = 1;"
231             elsif($phrase !~ m/^_/s and ($handle->{'use_external_lex_cache'} ? ( exists $handle->{'_external_lex_cache'}{'_AUTO'} ? $handle->{'_external_lex_cache'}{'_AUTO'} : $h_r->{'_AUTO'} ) : $h_r->{'_AUTO'})) {
232                 # it's an auto lex, and this is an autoable key!
233                 DEBUG and warn "  Automaking \"$phrase\" into $h_r\n";
234                 if ($handle->{'use_external_lex_cache'}) {
235                     $value = $handle->{'_external_lex_cache'}{$phrase} = $handle->_compile($phrase);
236                 }
237                 else {
238                     $value = $h_r->{$phrase} = $handle->_compile($phrase);
239                 }
240                 last;
241             }
242             DEBUG>1 and print "  Not found in $h_r, nor automakable\n";
243             # else keep looking
244         }
245     }
246
247     unless(defined($value)) {
248         DEBUG and warn "! Lookup of \"$phrase\" in/under ", ref($handle) || $handle, " fails.\n";
249         if(ref($handle) and $handle->{'fail'}) {
250             DEBUG and warn "WARNING0: maketext fails looking for <$phrase>\n";
251             my $fail;
252             if(ref($fail = $handle->{'fail'}) eq 'CODE') { # it's a sub reference
253                 $@ = $at; # Put $@ back in case we altered it along the way.
254                 return &{$fail}($handle, $phrase, @_);
255                 # If it ever returns, it should return a good value.
256             }
257             else { # It's a method name
258                 $@ = $at; # Put $@ back in case we altered it along the way.
259                 return $handle->$fail($phrase, @_);
260                 # If it ever returns, it should return a good value.
261             }
262         }
263         else {
264             # All we know how to do is this;
265             Carp::croak("maketext doesn't know how to say:\n$phrase\nas needed");
266         }
267     }
268
269     if(ref($value) eq 'SCALAR'){
270         $@ = $at; # Put $@ back in case we altered it along the way.
271         return $$value ;
272     }
273     if(ref($value) ne 'CODE'){
274         $@ = $at; # Put $@ back in case we altered it along the way.
275         return $value ;
276     }
277
278     {
279         local $SIG{'__DIE__'};
280         eval { $value = &$value($handle, @_) };
281     }
282     # If we make it here, there was an exception thrown in the
283     #  call to $value, and so scream:
284     if ($@) {
285         # pretty up the error message
286         $@ =~ s{\s+at\s+\(eval\s+\d+\)\s+line\s+(\d+)\.?\n?}
287                  {\n in bracket code [compiled line $1],}s;
288         #$err =~ s/\n?$/\n/s;
289         Carp::croak "Error in maketexting \"$phrase\":\n$@ as used";
290         # Rather unexpected, but suppose that the sub tried calling
291         # a method that didn't exist.
292     }
293     else {
294         $@ = $at; # Put $@ back in case we altered it along the way.
295         return $value;
296     }
297     $@ = $at; # Put $@ back in case we altered it along the way.
298 }
299
300 ###########################################################################
301
302 sub get_handle {  # This is a constructor and, yes, it CAN FAIL.
303     # Its class argument has to be the base class for the current
304     # application's l10n files.
305
306     my($base_class, @languages) = @_;
307     $base_class = ref($base_class) || $base_class;
308     # Complain if they use __PACKAGE__ as a project base class?
309
310     if( @languages ) {
311         DEBUG and warn 'Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
312         if($USING_LANGUAGE_TAGS) {   # An explicit language-list was given!
313             @languages =
314             map {; $_, I18N::LangTags::alternate_language_tags($_) }
315             # Catch alternation
316             map I18N::LangTags::locale2language_tag($_),
317             # If it's a lg tag, fine, pass thru (untainted)
318             # If it's a locale ID, try converting to a lg tag (untainted),
319             # otherwise nix it.
320             @languages;
321             DEBUG and warn 'Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
322         }
323     }
324     else {
325         @languages = $base_class->_ambient_langprefs;
326     }
327
328     @languages = $base_class->_langtag_munging(@languages);
329
330     my %seen;
331     foreach my $module_name ( map { $base_class . '::' . $_ }  @languages ) {
332         next unless length $module_name; # sanity
333         next if $seen{$module_name}++        # Already been here, and it was no-go
334         || !&_try_use($module_name); # Try to use() it, but can't it.
335         return($module_name->new); # Make it!
336     }
337
338     return undef; # Fail!
339 }
340
341 ###########################################################################
342
343 sub _langtag_munging {
344     my($base_class, @languages) = @_;
345
346     # We have all these DEBUG statements because otherwise it's hard as hell
347     # to diagnose ifwhen something goes wrong.
348
349     DEBUG and warn 'Lgs1: ', map("<$_>", @languages), "\n";
350
351     if($USING_LANGUAGE_TAGS) {
352         DEBUG and warn 'Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
353         @languages     = $base_class->_add_supers( @languages );
354
355         push @languages, I18N::LangTags::panic_languages(@languages);
356         DEBUG and warn "After adding panic languages:\n",
357         ' Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
358
359         push @languages, $base_class->fallback_languages;
360         # You are free to override fallback_languages to return empty-list!
361         DEBUG and warn 'Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
362
363         @languages =  # final bit of processing to turn them into classname things
364         map {
365             my $it = $_;  # copy
366             $it =~ tr<-A-Z><_a-z>; # lc, and turn - to _
367             $it =~ tr<_a-z0-9><>cd;  # remove all but a-z0-9_
368             $it;
369         } @languages
370         ;
371         DEBUG and warn "Nearing end of munging:\n",
372         ' Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
373     }
374     else {
375         DEBUG and warn "Bypassing language-tags.\n",
376         ' Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
377     }
378
379     DEBUG and warn "Before adding fallback classes:\n",
380     ' Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
381
382     push @languages, $base_class->fallback_language_classes;
383     # You are free to override that to return whatever.
384
385     DEBUG and warn "Finally:\n",
386     ' Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
387
388     return @languages;
389 }
390
391 ###########################################################################
392
393 sub _ambient_langprefs {
394     return  I18N::LangTags::Detect::detect();
395 }
396
397 ###########################################################################
398
399 sub _add_supers {
400     my($base_class, @languages) = @_;
401
402     if (!$MATCH_SUPERS) {
403         # Nothing
404         DEBUG and warn "Bypassing any super-matching.\n",
405         ' Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
406
407     }
408     elsif( $MATCH_SUPERS_TIGHTLY ) {
409         DEBUG and warn "Before adding new supers tightly:\n",
410         ' Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
411         @languages = I18N::LangTags::implicate_supers( @languages );
412         DEBUG and warn "After adding new supers tightly:\n",
413         ' Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
414
415     }
416     else {
417         DEBUG and warn "Before adding supers to end:\n",
418         ' Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
419         @languages = I18N::LangTags::implicate_supers_strictly( @languages );
420         DEBUG and warn "After adding supers to end:\n",
421         ' Lgs@', __LINE__, ': ', map("<$_>", @languages), "\n";
422     }
423
424     return @languages;
425 }
426
427 ###########################################################################
428 #
429 # This is where most people should stop reading.
430 #
431 ###########################################################################
432
433 my %tried = ();
434 # memoization of whether we've used this module, or found it unusable.
435
436 sub _try_use {   # Basically a wrapper around "require Modulename"
437     # "Many men have tried..."  "They tried and failed?"  "They tried and died."
438     return $tried{$_[0]} if exists $tried{$_[0]};  # memoization
439
440     my $module = $_[0];   # ASSUME sane module name!
441     { no strict 'refs';
442         no warnings 'once';
443         return($tried{$module} = 1)
444         if %{$module . '::Lexicon'} or @{$module . '::ISA'};
445         # weird case: we never use'd it, but there it is!
446     }
447
448     DEBUG and warn " About to use $module ...\n";
449
450     local $SIG{'__DIE__'};
451     local $@;
452     eval "require $module"; # used to be "use $module", but no point in that.
453
454     if($@) {
455         DEBUG and warn "Error using $module \: $@\n";
456         return $tried{$module} = 0;
457     }
458     else {
459         DEBUG and warn " OK, $module is used\n";
460         return $tried{$module} = 1;
461     }
462 }
463
464 #--------------------------------------------------------------------------
465
466 sub _lex_refs {  # report the lexicon references for this handle's class
467     # returns an arrayREF!
468     no strict 'refs';
469     no warnings 'once';
470     my $class = ref($_[0]) || $_[0];
471     DEBUG and warn "Lex refs lookup on $class\n";
472     return $isa_scan{$class} if exists $isa_scan{$class};  # memoization!
473
474     my @lex_refs;
475     my $seen_r = ref($_[1]) ? $_[1] : {};
476
477     if( defined( *{$class . '::Lexicon'}{'HASH'} )) {
478         push @lex_refs, *{$class . '::Lexicon'}{'HASH'};
479         DEBUG and warn '%' . $class . '::Lexicon contains ',
480             scalar(keys %{$class . '::Lexicon'}), " entries\n";
481     }
482
483     # Implements depth(height?)-first recursive searching of superclasses.
484     # In hindsight, I suppose I could have just used Class::ISA!
485     foreach my $superclass (@{$class . '::ISA'}) {
486         DEBUG and warn " Super-class search into $superclass\n";
487         next if $seen_r->{$superclass}++;
488         push @lex_refs, @{&_lex_refs($superclass, $seen_r)};  # call myself
489     }
490
491     $isa_scan{$class} = \@lex_refs; # save for next time
492     return \@lex_refs;
493 }
494
495 sub clear_isa_scan { %isa_scan = (); return; } # end on a note of simplicity!
496
497 #--------------------------------------------------------------------------
498
499 sub _compile {
500     # This big scary routine compiles an entry.
501     # It returns either a coderef if there's brackety bits in this, or
502     #  otherwise a ref to a scalar.
503
504     my $string_to_compile = $_[1]; # There are taint issues using regex on @_ - perlbug 60378,27344
505
506     # The while() regex is more expensive than this check on strings that don't need a compile.
507     # this op causes a ~2% speed hit for strings that need compile and a 250% speed improvement
508     # on strings that don't need compiling.
509     return \"$string_to_compile" if($string_to_compile !~ m/[\[~\]]/ms); # return a string ref if chars [~] are not in the string
510
511     my $target = ref($_[0]) || $_[0];
512
513     my(@code);
514     my(@c) = (''); # "chunks" -- scratch.
515     my $call_count = 0;
516     my $big_pile = '';
517     {
518         my $in_group = 0; # start out outside a group
519         my($m, @params); # scratch
520
521         while($string_to_compile =~  # Iterate over chunks.
522             m/(
523                 [^\~\[\]]+  # non-~[] stuff (Capture everything else here)
524                 |
525                 ~.       # ~[, ~], ~~, ~other
526                 |
527                 \[          # [ presumably opening a group
528                 |
529                 \]          # ] presumably closing a group
530                 |
531                 ~           # terminal ~ ?
532                 |
533                 $
534             )/xgs
535         ) {
536             DEBUG>2 and warn qq{  "$1"\n};
537
538             if($1 eq '[' or $1 eq '') {       # "[" or end
539                 # Whether this is "[" or end, force processing of any
540                 #  preceding literal.
541                 if($in_group) {
542                     if($1 eq '') {
543                         $target->_die_pointing($string_to_compile, 'Unterminated bracket group');
544                     }
545                     else {
546                         $target->_die_pointing($string_to_compile, 'You can\'t nest bracket groups');
547                     }
548                 }
549                 else {
550                     if ($1 eq '') {
551                         DEBUG>2 and warn "   [end-string]\n";
552                     }
553                     else {
554                         $in_group = 1;
555                     }
556                     die "How come \@c is empty?? in <$string_to_compile>" unless @c; # sanity
557                     if(length $c[-1]) {
558                         # Now actually processing the preceding literal
559                         $big_pile .= $c[-1];
560                         if($USE_LITERALS and (
561                                 (ord('A') == 65)
562                                 ? $c[-1] !~ m/[^\x20-\x7E]/s
563                                 # ASCII very safe chars
564                                 : $c[-1] !~ m/[^ !"\#\$%&'()*+,\-.\/0-9:;<=>?\@A-Z[\\\]^_`a-z{|}~\x07]/s
565                                 # EBCDIC very safe chars
566                             )) {
567                             # normal case -- all very safe chars
568                             $c[-1] =~ s/'/\\'/g;
569                             push @code, q{ '} . $c[-1] . "',\n";
570                             $c[-1] = ''; # reuse this slot
571                         }
572                         else {
573                             push @code, ' $c[' . $#c . "],\n";
574                             push @c, ''; # new chunk
575                         }
576                     }
577                     # else just ignore the empty string.
578                 }
579
580             }
581             elsif($1 eq ']') {  # "]"
582                 # close group -- go back in-band
583                 if($in_group) {
584                     $in_group = 0;
585
586                     DEBUG>2 and warn "   --Closing group [$c[-1]]\n";
587
588                     # And now process the group...
589
590                     if(!length($c[-1]) or $c[-1] =~ m/^\s+$/s) {
591                         DEBUG>2 and warn "   -- (Ignoring)\n";
592                         $c[-1] = ''; # reset out chink
593                         next;
594                     }
595
596                     #$c[-1] =~ s/^\s+//s;
597                     #$c[-1] =~ s/\s+$//s;
598                     ($m,@params) = split(/,/, $c[-1], -1);  # was /\s*,\s*/
599
600                     # A bit of a hack -- we've turned "~,"'s into DELs, so turn
601                     #  'em into real commas here.
602                     if (ord('A') == 65) { # ASCII, etc
603                         foreach($m, @params) { tr/\x7F/,/ }
604                     }
605                     else {              # EBCDIC (1047, 0037, POSIX-BC)
606                         # Thanks to Peter Prymmer for the EBCDIC handling
607                         foreach($m, @params) { tr/\x07/,/ }
608                     }
609
610                     # Special-case handling of some method names:
611                     if($m eq '_*' or $m =~ m/^_(-?\d+)$/s) {
612                         # Treat [_1,...] as [,_1,...], etc.
613                         unshift @params, $m;
614                         $m = '';
615                     }
616                     elsif($m eq '*') {
617                         $m = 'quant'; # "*" for "times": "4 cars" is 4 times "cars"
618                     }
619                     elsif($m eq '#') {
620                         $m = 'numf';  # "#" for "number": [#,_1] for "the number _1"
621                     }
622
623                     # Most common case: a simple, legal-looking method name
624                     if($m eq '') {
625                         # 0-length method name means to just interpolate:
626                         push @code, ' (';
627                     }
628                     elsif($m =~ /^\w+(?:\:\:\w+)*$/s
629                             and $m !~ m/(?:^|\:)\d/s
630                         # exclude starting a (sub)package or symbol with a digit
631                     ) {
632                         # Yes, it even supports the demented (and undocumented?)
633                         #  $obj->Foo::bar(...) syntax.
634                         $target->_die_pointing(
635                             $string_to_compile, q{Can't use "SUPER::" in a bracket-group method},
636                             2 + length($c[-1])
637                         )
638                         if $m =~ m/^SUPER::/s;
639                         # Because for SUPER:: to work, we'd have to compile this into
640                         #  the right package, and that seems just not worth the bother,
641                         #  unless someone convinces me otherwise.
642
643                         push @code, ' $_[0]->' . $m . '(';
644                     }
645                     else {
646                         # TODO: implement something?  or just too icky to consider?
647                         $target->_die_pointing(
648                             $string_to_compile,
649                             "Can't use \"$m\" as a method name in bracket group",
650                             2 + length($c[-1])
651                         );
652                     }
653
654                     pop @c; # we don't need that chunk anymore
655                     ++$call_count;
656
657                     foreach my $p (@params) {
658                         if($p eq '_*') {
659                             # Meaning: all parameters except $_[0]
660                             $code[-1] .= ' @_[1 .. $#_], ';
661                             # and yes, that does the right thing for all @_ < 3
662                         }
663                         elsif($p =~ m/^_(-?\d+)$/s) {
664                             # _3 meaning $_[3]
665                             $code[-1] .= '$_[' . (0 + $1) . '], ';
666                         }
667                         elsif($USE_LITERALS and (
668                                 (ord('A') == 65)
669                                 ? $p !~ m/[^\x20-\x7E]/s
670                                 # ASCII very safe chars
671                                 : $p !~ m/[^ !"\#\$%&'()*+,\-.\/0-9:;<=>?\@A-Z[\\\]^_`a-z{|}~\x07]/s
672                                 # EBCDIC very safe chars
673                             )) {
674                             # Normal case: a literal containing only safe characters
675                             $p =~ s/'/\\'/g;
676                             $code[-1] .= q{'} . $p . q{', };
677                         }
678                         else {
679                             # Stow it on the chunk-stack, and just refer to that.
680                             push @c, $p;
681                             push @code, ' $c[' . $#c . '], ';
682                         }
683                     }
684                     $code[-1] .= "),\n";
685
686                     push @c, '';
687                 }
688                 else {
689                     $target->_die_pointing($string_to_compile, q{Unbalanced ']'});
690                 }
691
692             }
693             elsif(substr($1,0,1) ne '~') {
694                 # it's stuff not containing "~" or "[" or "]"
695                 # i.e., a literal blob
696                 $c[-1] .= $1;
697
698             }
699             elsif($1 eq '~~') { # "~~"
700                 $c[-1] .= '~';
701
702             }
703             elsif($1 eq '~[') { # "~["
704                 $c[-1] .= '[';
705
706             }
707             elsif($1 eq '~]') { # "~]"
708                 $c[-1] .= ']';
709
710             }
711             elsif($1 eq '~,') { # "~,"
712                 if($in_group) {
713                     # This is a hack, based on the assumption that no-one will actually
714                     # want a DEL inside a bracket group.  Let's hope that's it's true.
715                     if (ord('A') == 65) { # ASCII etc
716                         $c[-1] .= "\x7F";
717                     }
718                     else {              # EBCDIC (cp 1047, 0037, POSIX-BC)
719                         $c[-1] .= "\x07";
720                     }
721                 }
722                 else {
723                     $c[-1] .= '~,';
724                 }
725
726             }
727             elsif($1 eq '~') { # possible only at string-end, it seems.
728                 $c[-1] .= '~';
729
730             }
731             else {
732                 # It's a "~X" where X is not a special character.
733                 # Consider it a literal ~ and X.
734                 $c[-1] .= $1;
735             }
736         }
737     }
738
739     if($call_count) {
740         undef $big_pile; # Well, nevermind that.
741     }
742     else {
743         # It's all literals!  Ahwell, that can happen.
744         # So don't bother with the eval.  Return a SCALAR reference.
745         return \$big_pile;
746     }
747
748     die q{Last chunk isn't null??} if @c and length $c[-1]; # sanity
749     DEBUG and warn scalar(@c), " chunks under closure\n";
750     if(@code == 0) { # not possible?
751         DEBUG and warn "Empty code\n";
752         return \'';
753     }
754     elsif(@code > 1) { # most cases, presumably!
755         unshift @code, "join '',\n";
756     }
757     unshift @code, "use strict; sub {\n";
758     push @code, "}\n";
759
760     DEBUG and warn @code;
761     my $sub = eval(join '', @code);
762     die "$@ while evalling" . join('', @code) if $@; # Should be impossible.
763     return $sub;
764 }
765
766 #--------------------------------------------------------------------------
767
768 sub _die_pointing {
769     # This is used by _compile to throw a fatal error
770     my $target = shift; # class name
771     # ...leaving $_[0] the error-causing text, and $_[1] the error message
772
773     my $i = index($_[0], "\n");
774
775     my $pointy;
776     my $pos = pos($_[0]) - (defined($_[2]) ? $_[2] : 0) - 1;
777     if($pos < 1) {
778         $pointy = "^=== near there\n";
779     }
780     else { # we need to space over
781         my $first_tab = index($_[0], "\t");
782         if($pos > 2 and ( -1 == $first_tab  or  $first_tab > pos($_[0]))) {
783             # No tabs, or the first tab is harmlessly after where we will point to,
784             # AND we're far enough from the margin that we can draw a proper arrow.
785             $pointy = ('=' x $pos) . "^ near there\n";
786         }
787         else {
788             # tabs screw everything up!
789             $pointy = substr($_[0],0,$pos);
790             $pointy =~ tr/\t //cd;
791             # make everything into whitespace, but preserving tabs
792             $pointy .= "^=== near there\n";
793         }
794     }
795
796     my $errmsg = "$_[1], in\:\n$_[0]";
797
798     if($i == -1) {
799         # No newline.
800         $errmsg .= "\n" . $pointy;
801     }
802     elsif($i == (length($_[0]) - 1)  ) {
803         # Already has a newline at end.
804         $errmsg .= $pointy;
805     }
806     else {
807         # don't bother with the pointy bit, I guess.
808     }
809     Carp::croak( "$errmsg via $target, as used" );
810 }
811
812 1;