WrapTie.pm

filter: Statementstmt 62.7% 37 / 59 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 45.4% 10 / 22 Podpod 50.0% 1 / 2 totaltotal 57.8% 48 / 83 CC 2 SCAR 5.1
CC
2
Cov
63%
PRELOAD
3.9
wraptie
2.0
new_tie
2.0
CRAP 1.7
« Stringy.pm ↑ Summary
1 package IO::WrapTie;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # Inheritance, exporting, and package version:
7 6 our @ISA = qw(Exporter);
8 6 our @EXPORT = qw(wraptie);
9 6 our $VERSION = '2.113';
10  
11 # Function, exported.
12 0 ▶sub wraptie {
Subroutine wraptie: not called CC=1 SCAR=6.9
Pod documented
13 0 IO::WrapTie::Master->new(@_);
14 }
15  
16 # Class method; BACKWARDS-COMPATIBILITY ONLY!
17 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
18 1 shift;
19 1 IO::WrapTie::Master->new(@_);
20 }
21  
22  
23  
24 #------------------------------------------------------------
25 package # hide from pause
26 IO::WrapTie::Master;
27 #------------------------------------------------------------
28  
29 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 6 ▶use vars qw($AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 6 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # We inherit from IO::Handle to get methods which invoke i/o operators,
34 # like print(), on our tied handle:
35 6 our @ISA = qw(IO::Handle);
36  
37 #------------------------------
38 # new SLAVE, TIEARGS...
39 #------------------------------
40 # Create a new subclass of IO::Handle which...
41 #
42 # (1) Handles i/o OPERATORS because it is tied to an instance of
43 # an i/o-like class, like IO::Scalar.
44 #
45 # (2) Handles i/o METHODS by delegating them to that same tied object!.
46 #
47 # Arguments are the slave class (e.g., IO::Scalar), followed by all
48 # the arguments normally sent into that class's C<TIEHANDLE> method.
49 # In other words, much like the arguments to tie(). :-)
50 #
51 # NOTE:
52 # The thing $x we return must be a BLESSED REF, for ($x->print()).
53 # The underlying symbol must be a FILEHANDLE, for (print $x "foo").
54 # It has to have a way of getting to the "real" back-end object...
55 #
56 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
57 1 my $master = shift;
58 1 my $io = IO::Handle->new; ### create a new handle
59 1 my $slave = shift;
60 1 tie *$io, $slave, @_; ### tie: will invoke slave's TIEHANDLE
61 1 bless $io, $master; ### return a master
62 }
63  
64 #------------------------------
65 # AUTOLOAD
66 #------------------------------
67 # Delegate method invocations on the master to the underlying slave.
68 #
69 1 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
70 1 my $method = $AUTOLOAD;
71 1 $method =~ s/.*:://;
72 1 my $self = shift; tied(*$self)->$method(\@_);
73 }
74  
75 #------------------------------
76 # PRELOAD
77 #------------------------------
78 # Utility.
79 #
80 # Most methods like print(), getline(), etc. which work on the tied object
81 # via Perl's i/o operators (like 'print') are inherited from IO::Handle.
82 #
83 # Other methods, like seek() and sref(), we must delegate ourselves.
84 # AUTOLOAD takes care of these.
85 #
86 # However, it may be necessary to preload delegators into your
87 # own class. PRELOAD will do this.
88 #
89 6 ▶sub PRELOAD {
Subroutine PRELOAD: called CC=2 SCAR=13.7
90 6 my $class = shift;
91 6 foreach (@_) {
92 0 ▶ eval "sub ${class}::$_ { my \$s = shift; tied(*\$s)->$_(\@_) }";
Subroutine clearerr: not called CC=1 SCAR=4.9
Subroutine close: not called CC=1 SCAR=4.9
Subroutine eof: not called CC=1 SCAR=4.9
Subroutine getpos: not called CC=1 SCAR=4.9
Subroutine open: not called CC=1 SCAR=4.9
Subroutine opened: not called CC=1 SCAR=4.9
Subroutine read: not called CC=1 SCAR=4.9
Subroutine seek: called CC=1 SCAR=4.9
Subroutine setpos: not called CC=1 SCAR=4.9
Subroutine tell: not called CC=1 SCAR=4.9
93 }
94 }
95  
96 # Preload delegators for some standard methods which we can't simply
97 # inherit from IO::Handle... for example, some IO::Handle methods
98 # assume that there is an underlying file descriptor.
99 #
100 6 PRELOAD IO::WrapTie::Master
101 qw(open opened close read clearerr eof seek tell setpos getpos);
102  
103  
104  
105 #------------------------------------------------------------
106 package # hide from pause
107 IO::WrapTie::Slave;
108 #------------------------------------------------------------
109 # Teeny private class providing a new_tie constructor...
110 #
111 # HOW IT ALL WORKS:
112 #
113 # Slaves inherit from this class.
114 #
115 # When you send a new_tie() message to a tie-slave class (like IO::Scalar),
116 # it first determines what class should provide its master, via TIE_MASTER.
117 # In this case, IO::Scalar->TIE_MASTER would return IO::Scalar::Master.
118 # Then, we create a new master (an IO::Scalar::Master) with the same args
119 # sent to new_tie.
120 #
121 # In general, the new() method of the master is inherited directly
122 # from IO::WrapTie::Master.
123 #
124 0 ▶sub new_tie {
Subroutine new_tie: not called CC=1 SCAR=6.9
125 0 my $self = shift;
126 0 $self->TIE_MASTER->new($self,@_); ### e.g., IO::Scalar::Master->new(@_)
127 }
128  
129 # Default class method for new_tie().
130 # All your tie-slave class (like IO::Scalar) has to do is override this
131 # method with a method that returns the name of an appropriate "master"
132 # class for tying that slave.
133 #
134 0 ▶sub TIE_MASTER { 'IO::WrapTie::Master' }
Subroutine TIE_MASTER: not called CC=1 SCAR=6.9
135  
136 #------------------------------
137 6 1;
138 __END__
« Stringy.pm ↑ Summary