File: Subclass.pm

package info (click to toggle)
libmath-bigint-perl 2.005003-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,144 kB
  • sloc: perl: 14,389; pascal: 6,476; makefile: 2
file content (70 lines) | stat: -rw-r--r-- 1,505 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- mode: perl; -*-

# test subclassing Math::BigInt

package Math::BigInt::Subclass;

use strict;
use warnings;

use Math::BigInt;

our @ISA = qw(Math::BigInt);

our $VERSION = "0.08";

use overload;                   # inherit overload

# Global variables. The values can be specified explicitly or obtained from the
# superclass.

our $accuracy   = undef;        # or Math::BigInt::Subclass -> accuracy();
our $precision  = undef;        # or Math::BigInt::Subclass -> precision();
our $round_mode = "even";       # or Math::BigInt::Subclass -> round_mode();
our $div_scale  = 40;           # or Math::BigInt::Subclass -> div_scale();

BEGIN {
    *objectify = \&Math::BigInt::objectify;
}

# We override new().

sub new {
    my $proto = shift;
    my $class = ref($proto) || $proto;

    my $self = $class -> SUPER::new(@_);
    $self->{'_custom'} = 1;     # attribute specific to this subclass
    bless $self, $class;
}

# We override import(). This is just for a sample for demonstration.

sub import {
    my $self  = shift;
    my $class = ref($self) || $self;

    my @a;                      # unrecognized arguments
    while (@_) {
        my $param = shift;

        # The parameter "this" takes an option.

        if ($param eq 'something') {
            $self -> {$param} = shift;
            next;
        }

        push @a, $_;
    }

    $self -> SUPER::import(@a);         # need it for subclasses
}

# Any other methods to override can go here:

# sub method {
#     ...
# }

1;