PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_Main/Runtime/Tests/LinqDlrTests/testenv/perl/lib/Fatal.pm

#
Perl | 186 lines | 166 code | 20 blank | 0 comment | 25 complexity | 3fd066bc17830816bb962c4b917da79f MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. package Fatal;
  2. use 5.005_64;
  3. use Carp;
  4. use strict;
  5. our($AUTOLOAD, $Debug, $VERSION);
  6. $VERSION = 1.02;
  7. $Debug = 0 unless defined $Debug;
  8. sub import {
  9. my $self = shift(@_);
  10. my($sym, $pkg);
  11. my $void = 0;
  12. $pkg = (caller)[0];
  13. foreach $sym (@_) {
  14. if ($sym eq ":void") {
  15. $void = 1;
  16. }
  17. else {
  18. &_make_fatal($sym, $pkg, $void);
  19. }
  20. }
  21. };
  22. sub AUTOLOAD {
  23. my $cmd = $AUTOLOAD;
  24. $cmd =~ s/.*:://;
  25. &_make_fatal($cmd, (caller)[0]);
  26. goto &$AUTOLOAD;
  27. }
  28. sub fill_protos {
  29. my $proto = shift;
  30. my ($n, $isref, @out, @out1, $seen_semi) = -1;
  31. while ($proto =~ /\S/) {
  32. $n++;
  33. push(@out1,[$n,@out]) if $seen_semi;
  34. push(@out, $1 . "{\$_[$n]}"), next if $proto =~ s/^\s*\\([\@%\$\&])//;
  35. push(@out, "\$_[$n]"), next if $proto =~ s/^\s*([*\$&])//;
  36. push(@out, "\@_[$n..\$#_]"), last if $proto =~ s/^\s*(;\s*)?\@//;
  37. $seen_semi = 1, $n--, next if $proto =~ s/^\s*;//; # XXXX ????
  38. die "Unknown prototype letters: \"$proto\"";
  39. }
  40. push(@out1,[$n+1,@out]);
  41. @out1;
  42. }
  43. sub write_invocation {
  44. my ($core, $call, $name, $void, @argvs) = @_;
  45. if (@argvs == 1) { # No optional arguments
  46. my @argv = @{$argvs[0]};
  47. shift @argv;
  48. return "\t" . one_invocation($core, $call, $name, $void, @argv) . ";\n";
  49. } else {
  50. my $else = "\t";
  51. my (@out, @argv, $n);
  52. while (@argvs) {
  53. @argv = @{shift @argvs};
  54. $n = shift @argv;
  55. push @out, "$ {else}if (\@_ == $n) {\n";
  56. $else = "\t} els";
  57. push @out,
  58. "\t\treturn " . one_invocation($core, $call, $name, $void, @argv) . ";\n";
  59. }
  60. push @out, <<EOC;
  61. }
  62. die "$name(\@_): Do not expect to get ", scalar \@_, " arguments";
  63. EOC
  64. return join '', @out;
  65. }
  66. }
  67. sub one_invocation {
  68. my ($core, $call, $name, $void, @argv) = @_;
  69. local $" = ', ';
  70. if ($void) {
  71. return qq/(defined wantarray)?$call(@argv):
  72. $call(@argv) || croak "Can't $name(\@_)/ .
  73. ($core ? ': $!' : ', \$! is \"$!\"') . '"'
  74. } else {
  75. return qq{$call(@argv) || croak "Can't $name(\@_)} .
  76. ($core ? ': $!' : ', \$! is \"$!\"') . '"';
  77. }
  78. }
  79. sub _make_fatal {
  80. my($sub, $pkg, $void) = @_;
  81. my($name, $code, $sref, $real_proto, $proto, $core, $call);
  82. my $ini = $sub;
  83. $sub = "${pkg}::$sub" unless $sub =~ /::/;
  84. $name = $sub;
  85. $name =~ s/.*::// or $name =~ s/^&//;
  86. print "# _make_fatal: sub=$sub pkg=$pkg name=$name void=$void\n" if $Debug;
  87. croak "Bad subroutine name for Fatal: $name" unless $name =~ /^\w+$/;
  88. if (defined(&$sub)) { # user subroutine
  89. $sref = \&$sub;
  90. $proto = prototype $sref;
  91. $call = '&$sref';
  92. } elsif ($sub eq $ini) { # Stray user subroutine
  93. die "$sub is not a Perl subroutine"
  94. } else { # CORE subroutine
  95. $proto = eval { prototype "CORE::$name" };
  96. die "$name is neither a builtin, nor a Perl subroutine"
  97. if $@;
  98. die "Cannot make a non-overridable builtin fatal"
  99. if not defined $proto;
  100. $core = 1;
  101. $call = "CORE::$name";
  102. }
  103. if (defined $proto) {
  104. $real_proto = " ($proto)";
  105. } else {
  106. $real_proto = '';
  107. $proto = '@';
  108. }
  109. $code = <<EOS;
  110. sub$real_proto {
  111. local(\$", \$!) = (', ', 0);
  112. EOS
  113. my @protos = fill_protos($proto);
  114. $code .= write_invocation($core, $call, $name, $void, @protos);
  115. $code .= "}\n";
  116. print $code if $Debug;
  117. {
  118. no strict 'refs'; # to avoid: Can't use string (...) as a symbol ref ...
  119. $code = eval("package $pkg; use Carp; $code");
  120. die if $@;
  121. no warnings; # to avoid: Subroutine foo redefined ...
  122. *{$sub} = $code;
  123. }
  124. }
  125. 1;
  126. __END__
  127. =head1 NAME
  128. Fatal - replace functions with equivalents which succeed or die
  129. =head1 SYNOPSIS
  130. use Fatal qw(open close);
  131. sub juggle { . . . }
  132. import Fatal 'juggle';
  133. =head1 DESCRIPTION
  134. C<Fatal> provides a way to conveniently replace functions which normally
  135. return a false value when they fail with equivalents which raise exceptions
  136. if they are not successful. This lets you use these functions without
  137. having to test their return values explicitly on each call. Exceptions
  138. can be caught using C<eval{}>. See L<perlfunc> and L<perlvar> for details.
  139. The do-or-die equivalents are set up simply by calling Fatal's
  140. C<import> routine, passing it the names of the functions to be
  141. replaced. You may wrap both user-defined functions and overridable
  142. CORE operators (except C<exec>, C<system> which cannot be expressed
  143. via prototypes) in this way.
  144. If the symbol C<:void> appears in the import list, then functions
  145. named later in that import list raise an exception only when
  146. these are called in void context--that is, when their return
  147. values are ignored. For example
  148. use Fatal qw/:void open close/;
  149. # properly checked, so no exception raised on error
  150. if(open(FH, "< /bogotic") {
  151. warn "bogo file, dude: $!";
  152. }
  153. # not checked, so error raises an exception
  154. close FH;
  155. =head1 AUTHOR
  156. Lionel.Cons@cern.ch
  157. prototype updates by Ilya Zakharevich ilya@math.ohio-state.edu
  158. =cut