summaryrefslogtreecommitdiff
path: root/Master/tlpkg/tlperl/lib/overloading.pm
diff options
context:
space:
mode:
authorNorbert Preining <preining@logic.at>2010-05-12 16:54:37 +0000
committerNorbert Preining <preining@logic.at>2010-05-12 16:54:37 +0000
commit661c41a09e39a182865e0b51e34cc995a0dc96e8 (patch)
tree2f79bb1406e22fdcb2587be8ffda6c0c609d7932 /Master/tlpkg/tlperl/lib/overloading.pm
parentb645030efc22e13c2498a1522083634ab91b2de1 (diff)
move tlperl.straw to tlperl
git-svn-id: svn://tug.org/texlive/trunk@18210 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/tlpkg/tlperl/lib/overloading.pm')
-rwxr-xr-xMaster/tlpkg/tlperl/lib/overloading.pm101
1 files changed, 101 insertions, 0 deletions
diff --git a/Master/tlpkg/tlperl/lib/overloading.pm b/Master/tlpkg/tlperl/lib/overloading.pm
new file mode 100755
index 00000000000..d1ca5664b6c
--- /dev/null
+++ b/Master/tlpkg/tlperl/lib/overloading.pm
@@ -0,0 +1,101 @@
+package overloading;
+use warnings;
+
+use Carp ();
+
+our $VERSION = '0.01';
+
+my $HINT_NO_AMAGIC = 0x01000000; # see perl.h
+
+require 5.010001;
+
+sub _ops_to_nums {
+ require overload::numbers;
+
+ map { exists $overload::numbers::names{"($_"}
+ ? $overload::numbers::names{"($_"}
+ : Carp::croak("'$_' is not a valid overload")
+ } @_;
+}
+
+sub import {
+ my ( $class, @ops ) = @_;
+
+ if ( @ops ) {
+ if ( $^H{overloading} ) {
+ vec($^H{overloading} , $_, 1) = 0 for _ops_to_nums(@ops);
+ }
+
+ if ( $^H{overloading} !~ /[^\0]/ ) {
+ delete $^H{overloading};
+ $^H &= ~$HINT_NO_AMAGIC;
+ }
+ } else {
+ delete $^H{overloading};
+ $^H &= ~$HINT_NO_AMAGIC;
+ }
+}
+
+sub unimport {
+ my ( $class, @ops ) = @_;
+
+ if ( exists $^H{overloading} or not $^H & $HINT_NO_AMAGIC ) {
+ if ( @ops ) {
+ vec($^H{overloading} ||= '', $_, 1) = 1 for _ops_to_nums(@ops);
+ } else {
+ delete $^H{overloading};
+ }
+ }
+
+ $^H |= $HINT_NO_AMAGIC;
+}
+
+1;
+__END__
+
+=head1 NAME
+
+overloading - perl pragma to lexically control overloading
+
+=head1 SYNOPSIS
+
+ {
+ no overloading;
+ my $str = "$object"; # doesn't call stringification overload
+ }
+
+ # it's lexical, so this stringifies:
+ warn "$object";
+
+ # it can be enabled per op
+ no overloading qw("");
+ warn "$object";
+
+ # and also reenabled
+ use overloading;
+
+=head1 DESCRIPTION
+
+This pragma allows you to lexically disable or enable overloading.
+
+=over 6
+
+=item C<no overloading>
+
+Disables overloading entirely in the current lexical scope.
+
+=item C<no overloading @ops>
+
+Disables only specific overloads in the current lexical scope.
+
+=item C<use overloading>
+
+Reenables overloading in the current lexical scope.
+
+=item C<use overloading @ops>
+
+Reenables overloading only for specific ops in the current lexical scope.
+
+=back
+
+=cut