xref: /openbsd-src/gnu/usr.bin/perl/cpan/Math-BigInt/t/Math/BigInt/Subclass.pm (revision 3d61058aa5c692477b6d18acfbbdb653a9930ff9)
1# -*- mode: perl; -*-
2
3# test subclassing Math::BigInt
4
5package Math::BigInt::Subclass;
6
7use strict;
8use warnings;
9
10use Math::BigInt;
11
12our @ISA = qw(Math::BigInt);
13
14our $VERSION = "0.08";
15
16use overload;                   # inherit overload
17
18# Global variables. The values can be specified explicitly or obtained from the
19# superclass.
20
21our $accuracy   = undef;        # or Math::BigInt::Subclass -> accuracy();
22our $precision  = undef;        # or Math::BigInt::Subclass -> precision();
23our $round_mode = "even";       # or Math::BigInt::Subclass -> round_mode();
24our $div_scale  = 40;           # or Math::BigInt::Subclass -> div_scale();
25
26BEGIN {
27    *objectify = \&Math::BigInt::objectify;
28}
29
30# We override new().
31
32sub new {
33    my $proto = shift;
34    my $class = ref($proto) || $proto;
35
36    my $self = $class -> SUPER::new(@_);
37    $self->{'_custom'} = 1;     # attribute specific to this subclass
38    bless $self, $class;
39}
40
41# We override import(). This is just for a sample for demonstration.
42
43sub import {
44    my $self  = shift;
45    my $class = ref($self) || $self;
46
47    my @a;                      # unrecognized arguments
48    while (@_) {
49        my $param = shift;
50
51        # The parameter "this" takes an option.
52
53        if ($param eq 'something') {
54            $self -> {$param} = shift;
55            next;
56        }
57
58        push @a, $_;
59    }
60
61    $self -> SUPER::import(@a);         # need it for subclasses
62}
63
64# Any other methods to override can go here:
65
66# sub method {
67#     ...
68# }
69
701;
71