This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re-add a test deleted by the recent Locale::Maketext/$@ change
[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.16';
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         return($tried{$module} = 1)
443         if %{$module . '::Lexicon'} or @{$module . '::ISA'};
444         # weird case: we never use'd it, but there it is!
445     }
446
447     DEBUG and warn " About to use $module ...\n";
448
449     local $SIG{'__DIE__'};
450     local $@;
451     eval "require $module"; # used to be "use $module", but no point in that.
452
453     if($@) {
454         DEBUG and warn "Error using $module \: $@\n";
455         return $tried{$module} = 0;
456     }
457     else {
458         DEBUG and warn " OK, $module is used\n";
459         return $tried{$module} = 1;
460     }
461 }
462
463 #--------------------------------------------------------------------------
464
465 sub _lex_refs {  # report the lexicon references for this handle's class
466     # returns an arrayREF!
467     no strict 'refs';
468     no warnings 'once';
469     my $class = ref($_[0]) || $_[0];
470     DEBUG and warn "Lex refs lookup on $class\n";
471     return $isa_scan{$class} if exists $isa_scan{$class};  # memoization!
472
473     my @lex_refs;
474     my $seen_r = ref($_[1]) ? $_[1] : {};
475
476     if( defined( *{$class . '::Lexicon'}{'HASH'} )) {
477         push @lex_refs, *{$class . '::Lexicon'}{'HASH'};
478         DEBUG and warn '%' . $class . '::Lexicon contains ',
479             scalar(keys %{$class . '::Lexicon'}), " entries\n";
480     }
481
482     # Implements depth(height?)-first recursive searching of superclasses.
483     # In hindsight, I suppose I could have just used Class::ISA!
484     foreach my $superclass (@{$class . '::ISA'}) {
485         DEBUG and warn " Super-class search into $superclass\n";
486         next if $seen_r->{$superclass}++;
487         push @lex_refs, @{&_lex_refs($superclass, $seen_r)};  # call myself
488     }
489
490     $isa_scan{$class} = \@lex_refs; # save for next time
491     return \@lex_refs;
492 }
493
494 sub clear_isa_scan { %isa_scan = (); return; } # end on a note of simplicity!
495
496 #--------------------------------------------------------------------------
497
498 sub _compile {
499     # This big scary routine compiles an entry.
500     # It returns either a coderef if there's brackety bits in this, or
501     #  otherwise a ref to a scalar.
502
503     my $string_to_compile = $_[1]; # There are taint issues using regex on @_ - perlbug 60378,27344
504
505     # The while() regex is more expensive than this check on strings that don't need a compile.
506     # this op causes a ~2% speed hit for strings that need compile and a 250% speed improvement
507     # on strings that don't need compiling.
508     return \"$string_to_compile" if($string_to_compile !~ m/[\[~\]]/ms); # return a string ref if chars [~] are not in the string
509
510     my $target = ref($_[0]) || $_[0];
511
512     my(@code);
513     my(@c) = (''); # "chunks" -- scratch.
514     my $call_count = 0;
515     my $big_pile = '';
516     {
517         my $in_group = 0; # start out outside a group
518         my($m, @params); # scratch
519
520         while($string_to_compile =~  # Iterate over chunks.
521             m/(
522                 [^\~\[\]]+  # non-~[] stuff (Capture everything else here)
523                 |
524                 ~.       # ~[, ~], ~~, ~other
525                 |
526                 \[          # [ presumably opening a group
527                 |
528                 \]          # ] presumably closing a group
529                 |
530                 ~           # terminal ~ ?
531                 |
532                 $
533             )/xgs
534         ) {
535             DEBUG>2 and warn qq{  "$1"\n};
536
537             if($1 eq '[' or $1 eq '') {       # "[" or end
538                 # Whether this is "[" or end, force processing of any
539                 #  preceding literal.
540                 if($in_group) {
541                     if($1 eq '') {
542                         $target->_die_pointing($string_to_compile, 'Unterminated bracket group');
543                     }
544                     else {
545                         $target->_die_pointing($string_to_compile, 'You can\'t nest bracket groups');
546                     }
547                 }
548                 else {
549                     if ($1 eq '') {
550                         DEBUG>2 and warn "   [end-string]\n";
551                     }
552                     else {
553                         $in_group = 1;
554                     }
555                     die "How come \@c is empty?? in <$string_to_compile>" unless @c; # sanity
556                     if(length $c[-1]) {
557                         # Now actually processing the preceding literal
558                         $big_pile .= $c[-1];
559                         if($USE_LITERALS and (
560                                 (ord('A') == 65)
561                                 ? $c[-1] !~ m/[^\x20-\x7E]/s
562                                 # ASCII very safe chars
563                                 : $c[-1] !~ m/[^ !"\#\$%&'()*+,\-.\/0-9:;<=>?\@A-Z[\\\]^_`a-z{|}~\x07]/s
564                                 # EBCDIC very safe chars
565                             )) {
566                             # normal case -- all very safe chars
567                             $c[-1] =~ s/'/\\'/g;
568                             push @code, q{ '} . $c[-1] . "',\n";
569                             $c[-1] = ''; # reuse this slot
570                         }
571                         else {
572                             push @code, ' $c[' . $#c . "],\n";
573                             push @c, ''; # new chunk
574                         }
575                     }
576                     # else just ignore the empty string.
577                 }
578
579             }
580             elsif($1 eq ']') {  # "]"
581                 # close group -- go back in-band
582                 if($in_group) {
583                     $in_group = 0;
584
585                     DEBUG>2 and warn "   --Closing group [$c[-1]]\n";
586
587                     # And now process the group...
588
589                     if(!length($c[-1]) or $c[-1] =~ m/^\s+$/s) {
590                         DEBUG>2 and warn "   -- (Ignoring)\n";
591                         $c[-1] = ''; # reset out chink
592                         next;
593                     }
594
595                     #$c[-1] =~ s/^\s+//s;
596                     #$c[-1] =~ s/\s+$//s;
597                     ($m,@params) = split(/,/, $c[-1], -1);  # was /\s*,\s*/
598
599                     # A bit of a hack -- we've turned "~,"'s into DELs, so turn
600                     #  'em into real commas here.
601                     if (ord('A') == 65) { # ASCII, etc
602                         foreach($m, @params) { tr/\x7F/,/ }
603                     }
604                     else {              # EBCDIC (1047, 0037, POSIX-BC)
605                         # Thanks to Peter Prymmer for the EBCDIC handling
606                         foreach($m, @params) { tr/\x07/,/ }
607                     }
608
609                     # Special-case handling of some method names:
610                     if($m eq '_*' or $m =~ m/^_(-?\d+)$/s) {
611                         # Treat [_1,...] as [,_1,...], etc.
612                         unshift @params, $m;
613                         $m = '';
614                     }
615                     elsif($m eq '*') {
616                         $m = 'quant'; # "*" for "times": "4 cars" is 4 times "cars"
617                     }
618                     elsif($m eq '#') {
619                         $m = 'numf';  # "#" for "number": [#,_1] for "the number _1"
620                     }
621
622                     # Most common case: a simple, legal-looking method name
623                     if($m eq '') {
624                         # 0-length method name means to just interpolate:
625                         push @code, ' (';
626                     }
627                     elsif($m =~ /^\w+(?:\:\:\w+)*$/s
628                             and $m !~ m/(?:^|\:)\d/s
629                         # exclude starting a (sub)package or symbol with a digit
630                     ) {
631                         # Yes, it even supports the demented (and undocumented?)
632                         #  $obj->Foo::bar(...) syntax.
633                         $target->_die_pointing(
634                             $string_to_compile, q{Can't use "SUPER::" in a bracket-group method},
635                             2 + length($c[-1])
636                         )
637                         if $m =~ m/^SUPER::/s;
638                         # Because for SUPER:: to work, we'd have to compile this into
639                         #  the right package, and that seems just not worth the bother,
640                         #  unless someone convinces me otherwise.
641
642                         push @code, ' $_[0]->' . $m . '(';
643                     }
644                     else {
645                         # TODO: implement something?  or just too icky to consider?
646                         $target->_die_pointing(
647                             $string_to_compile,
648                             "Can't use \"$m\" as a method name in bracket group",
649                             2 + length($c[-1])
650                         );
651                     }
652
653                     pop @c; # we don't need that chunk anymore
654                     ++$call_count;
655
656                     foreach my $p (@params) {
657                         if($p eq '_*') {
658                             # Meaning: all parameters except $_[0]
659                             $code[-1] .= ' @_[1 .. $#_], ';
660                             # and yes, that does the right thing for all @_ < 3
661                         }
662                         elsif($p =~ m/^_(-?\d+)$/s) {
663                             # _3 meaning $_[3]
664                             $code[-1] .= '$_[' . (0 + $1) . '], ';
665                         }
666                         elsif($USE_LITERALS and (
667                                 (ord('A') == 65)
668                                 ? $p !~ m/[^\x20-\x7E]/s
669                                 # ASCII very safe chars
670                                 : $p !~ m/[^ !"\#\$%&'()*+,\-.\/0-9:;<=>?\@A-Z[\\\]^_`a-z{|}~\x07]/s
671                                 # EBCDIC very safe chars
672                             )) {
673                             # Normal case: a literal containing only safe characters
674                             $p =~ s/'/\\'/g;
675                             $code[-1] .= q{'} . $p . q{', };
676                         }
677                         else {
678                             # Stow it on the chunk-stack, and just refer to that.
679                             push @c, $p;
680                             push @code, ' $c[' . $#c . '], ';
681                         }
682                     }
683                     $code[-1] .= "),\n";
684
685                     push @c, '';
686                 }
687                 else {
688                     $target->_die_pointing($string_to_compile, q{Unbalanced ']'});
689                 }
690
691             }
692             elsif(substr($1,0,1) ne '~') {
693                 # it's stuff not containing "~" or "[" or "]"
694                 # i.e., a literal blob
695                 $c[-1] .= $1;
696
697             }
698             elsif($1 eq '~~') { # "~~"
699                 $c[-1] .= '~';
700
701             }
702             elsif($1 eq '~[') { # "~["
703                 $c[-1] .= '[';
704
705             }
706             elsif($1 eq '~]') { # "~]"
707                 $c[-1] .= ']';
708
709             }
710             elsif($1 eq '~,') { # "~,"
711                 if($in_group) {
712                     # This is a hack, based on the assumption that no-one will actually
713                     # want a DEL inside a bracket group.  Let's hope that's it's true.
714                     if (ord('A') == 65) { # ASCII etc
715                         $c[-1] .= "\x7F";
716                     }
717                     else {              # EBCDIC (cp 1047, 0037, POSIX-BC)
718                         $c[-1] .= "\x07";
719                     }
720                 }
721                 else {
722                     $c[-1] .= '~,';
723                 }
724
725             }
726             elsif($1 eq '~') { # possible only at string-end, it seems.
727                 $c[-1] .= '~';
728
729             }
730             else {
731                 # It's a "~X" where X is not a special character.
732                 # Consider it a literal ~ and X.
733                 $c[-1] .= $1;
734             }
735         }
736     }
737
738     if($call_count) {
739         undef $big_pile; # Well, nevermind that.
740     }
741     else {
742         # It's all literals!  Ahwell, that can happen.
743         # So don't bother with the eval.  Return a SCALAR reference.
744         return \$big_pile;
745     }
746
747     die q{Last chunk isn't null??} if @c and length $c[-1]; # sanity
748     DEBUG and warn scalar(@c), " chunks under closure\n";
749     if(@code == 0) { # not possible?
750         DEBUG and warn "Empty code\n";
751         return \'';
752     }
753     elsif(@code > 1) { # most cases, presumably!
754         unshift @code, "join '',\n";
755     }
756     unshift @code, "use strict; sub {\n";
757     push @code, "}\n";
758
759     DEBUG and warn @code;
760     my $sub = eval(join '', @code);
761     die "$@ while evalling" . join('', @code) if $@; # Should be impossible.
762     return $sub;
763 }
764
765 #--------------------------------------------------------------------------
766
767 sub _die_pointing {
768     # This is used by _compile to throw a fatal error
769     my $target = shift; # class name
770     # ...leaving $_[0] the error-causing text, and $_[1] the error message
771
772     my $i = index($_[0], "\n");
773
774     my $pointy;
775     my $pos = pos($_[0]) - (defined($_[2]) ? $_[2] : 0) - 1;
776     if($pos < 1) {
777         $pointy = "^=== near there\n";
778     }
779     else { # we need to space over
780         my $first_tab = index($_[0], "\t");
781         if($pos > 2 and ( -1 == $first_tab  or  $first_tab > pos($_[0]))) {
782             # No tabs, or the first tab is harmlessly after where we will point to,
783             # AND we're far enough from the margin that we can draw a proper arrow.
784             $pointy = ('=' x $pos) . "^ near there\n";
785         }
786         else {
787             # tabs screw everything up!
788             $pointy = substr($_[0],0,$pos);
789             $pointy =~ tr/\t //cd;
790             # make everything into whitespace, but preseving tabs
791             $pointy .= "^=== near there\n";
792         }
793     }
794
795     my $errmsg = "$_[1], in\:\n$_[0]";
796
797     if($i == -1) {
798         # No newline.
799         $errmsg .= "\n" . $pointy;
800     }
801     elsif($i == (length($_[0]) - 1)  ) {
802         # Already has a newline at end.
803         $errmsg .= $pointy;
804     }
805     else {
806         # don't bother with the pointy bit, I guess.
807     }
808     Carp::croak( "$errmsg via $target, as used" );
809 }
810
811 1;