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