diff options
author | Ilya Zakharevich <ilya@math.berkeley.edu> | 1998-06-26 19:28:41 -0400 |
---|---|---|
committer | Gurusamy Sarathy <gsar@cpan.org> | 1998-06-29 06:01:35 +0000 |
commit | b3ac6de7f0c7a63b73f1cf3ea9e371470f7d1cb0 (patch) | |
tree | 564cec3756b2fdc36f8885a6017a9b0eed22dca1 /lib/overload.pm | |
parent | dde527fc6256d3b4a78a8a6187a9b8048cc76da5 (diff) | |
download | perl-b3ac6de7f0c7a63b73f1cf3ea9e371470f7d1cb0.tar.gz |
added patch for overloading constants, made PERL_OBJECT-aware
Message-Id: <199806270328.XAA21088@monk.mps.ohio-state.edu>
p4raw-id: //depot/perl@1259
Diffstat (limited to 'lib/overload.pm')
-rw-r--r-- | lib/overload.pm | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/lib/overload.pm b/lib/overload.pm index c9044db0dc..dfcdb02b1e 100644 --- a/lib/overload.pm +++ b/lib/overload.pm @@ -100,6 +100,32 @@ sub mycan { # Real can would leave stubs. return undef; } +%constants = ( + 'integer' => 0x1000, + 'float' => 0x2000, + 'binary' => 0x4000, + 'q' => 0x8000, + 'qr' => 0x10000, + ); + +sub constant { + # Arguments: what, sub + while (@_) { + $^H{$_[0]} = $_[1]; + $^H |= $constants{$_[0]} | 0x20000; + shift, shift; + } +} + +sub remove_constant { + # Arguments: what, sub + while (@_) { + delete $^H{$_[0]}; + $^H &= ~ $constants{$_[0]}; + shift, shift; + } +} + 1; __END__ @@ -522,6 +548,72 @@ Returns C<undef> or a reference to the method that implements C<op>. =back +=head1 Overloading constants + +For some application Perl parser mangles constants too much. It is possible +to hook into this process via overload::constant() and overload::remove_constant() +functions. + +These functions take a hash as an argument. The recognized keys of this hash +are + +=over 8 + +=item integer + +to overload integer constants, + +=item float + +to overload floating point constants, + +=item binary + +to overload octal and hexadecimal constants, + +=item q + +to overload C<q>-quoted strings, constant pieces of C<qq>- and C<qx>-quoted +strings and here-documents, + +=item qr + +to overload constant pieces of regular expressions. + +=back + +The corresponding values are references to functions which take three arguments: +the first one is the I<initial> string form of the constant, the second one +is how Perl interprets this constant, the third one is how the constant is used. +Note that the initial string form does not +contain string delimiters, and has backslashes in backslash-delimiter +combinations stripped (thus the value of delimiter is not relevant for +processing of this string). The return value of this function is how this +constant is going to be interpreted by Perl. The third argument is undefined +unless for overloaded C<q>- and C<qr>- constants, it is C<q> in single-quote +context (comes from strings, regular expressions, and single-quote HERE +documents), it is C<tr> for arguments of C<tr>/C<y> operators, +it is C<s> for right-hand side of C<s>-operator, and it is C<qq> otherwise. + +Since an expression C<"ab$cd,,"> is just a shortcut for C<'ab' . $cd . ',,'>, +it is expected that overloaded constant strings are equipped with reasonable +overloaded catenation operator, otherwise absurd results will result. +Similarly, negative numbers are considered as negations of positive constants. + +Note that it is probably meaningless to call the functions overload::constant() +and overload::remove_constant() from anywhere but import() and unimport() methods. +From these methods they may be called as + + sub import { + shift; + return unless @_; + die "unknown import: @_" unless @_ == 1 and $_[0] eq ':constant'; + overload::constant integer => sub {Math::BigInt->new(shift)}; + } + +B<BUGS> Currently overloaded-ness of constants does not propagate +into C<eval '...'>. + =head1 IMPLEMENTATION What follows is subject to change RSN. @@ -597,6 +689,8 @@ C<fallback> is present (possibly undefined). This may create interesting effects if some package is not overloaded, but inherits from two overloaded packages. +Barewords are not covered by overloaded string constants. + This document is confusing. =cut |