File: demo_library_AUTOLOAD.pl

package info (click to toggle)
libclass-std-perl 0.011-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 352 kB
  • ctags: 122
  • sloc: perl: 871; makefile: 2
file content (91 lines) | stat: -rw-r--r-- 2,604 bytes parent folder | download | duplicates (4)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package Library::OReilly;
use Class::Std;
{
    my %book;   # Not an attribute hash; shared storage for class

    sub add_book {
        my ($class, $title, $arg_ref) = @_; 
        $book{$title} = { Title=>$title, Publisher=>q{O'Reilly}, %{$arg_ref} };
    }

    # Book titles accumulate throughout the libraries...
    sub titles :CUMULATIVE {
        return map { "$_ (O'Reilly)"} keys %book;
    }

    # Treat every undefined method call as a request for books
    # with titles containing the method name...
    sub AUTOLOAD {

        # Fully qualified name of the desired method
        # is passed in the $AUTOLOAD package variable...
        use vars qw( $AUTOLOAD );               # Placate 'use strict'
        my ($book_title_keyword) =              # Extract book title keyword
            $AUTOLOAD =~ m/ .* :: (.*) /xms;    #    by extracting method name

        # If that name matches any of the book titles, return those titles...
        if (my @matches = grep { /$book_title_keyword/ixms } keys %book) {
            return @book{@matches};
        }

        # Otherwise return no titles...
        return;
    }
    
}

package Library::Manning;
use Class::Std;
{
    my %book;

    sub add_book {
        my ($class, $title, $arg_ref) = @_; 
        $book{$title} = { Title=>$title, Publisher=>q{Manning}, %{$arg_ref} };
    }

    sub titles :CUMULATIVE {
        return map { "$_ (Manning)"} keys %book;
    }
 
    # Treat every undefined method call as a request for books
    # with titles containing the method name...
    sub AUTOLOAD {

        # Fully qualified name of the desired method
        # is passed in the $AUTOLOAD package variable...
        use vars qw( $AUTOLOAD );               # Placate 'use strict'
        my ($book_title_keyword) =              # Extract book title keyword
            $AUTOLOAD =~ m/ .* :: (.*) /xms;    #    by extracting method name

        # If that name matches any of the book titles, return those titles...
        if (my @matches = grep { /$book_title_keyword/ixms } keys %book) {
            return @book{@matches};
        }

        # Otherwise return no titles...
        return;
    }
}

package Library;
use base qw( Library::OReilly  Library::Manning);

package main;

Library::OReilly->add_book(
    'Programming Perl' => { ISBN=>596000278, year=>2000 }
);

Library::Manning->add_book(
    'Object Oriented Perl' => { ISBN=>1884777791, year=>2000 }
);

# print join "\n", Library->titles();

print "\n-----------------\n";

use Data::Dumper 'Dumper';
print Dumper( Library->Perl() );
print "\n-----------------\n";