File Coverage

blib/lib/Template/Plugin/Package.pm
Criterion Covered Total %
statement 34 36 94.4
branch 4 8 50.0
condition n/a
subroutine 9 10 90.0
pod 1 1 100.0
total 48 55 87.2


line stmt bran cond sub pod time code
1             package Template::Plugin::Package;
2              
3             =head1 NAME
4              
5             Template::Plugin::Package - allow calling of class methods on arbitrary classes that do not accept the class name as their first argument.
6              
7             =head1 SYNOPSIS
8              
9             [% USE foo = Package('Foo') %]
10             [% foo.bar('arguments', 'to', 'bar') %]
11              
12             =head1 DESCRIPTION
13              
14             Template::Plugin::Package allows you to call functions in arbitrary
15             packages much like Template::Plugin::Class does, but the methods are called
16             without the package class name as the first parameter.
17              
18             Use Template::Plugin::Package to call class methods that in normal Perl
19             code require '::' to call.
20              
21             Use Template::Plugin::Class to call class methods that require '->' to
22             call.
23              
24             =cut
25              
26 2     2   244969 use 5.010;
  2         7  
27 2     2   17 use warnings;
  2         3  
  2         164  
28 2     2   12 use strict;
  2         3  
  2         58  
29              
30 2     2   847 use parent 'Template::Plugin';
  2         639  
  2         10  
31              
32             our $VERSION = '1.00';
33              
34             sub new {
35 1     1 1 213624 my $class = shift;
36 1         2 my $context = shift;
37 1         2 my $arg = shift;
38              
39 1 50       69 if ( ! eval "require $arg; 1;" ) {
40             # Only ignore "Can't locate" errors from our eval require.
41             # Other fatal errors (syntax etc) must be reported.
42 1         7 (my $filename = $arg) =~ s!::!/!g;
43 1 50       29 die $@ if $@ !~ /Can't locate \Q$filename.pm/;
44             }
45 2     2   11038 no strict 'refs';
  2         4  
  2         273  
46 1 50       4 if ( not scalar(%{"$arg\::"}) ) {
  1         5  
47 0         0 die "Package \"$arg\" appears to have not been loaded. (Perhaps you need to 'use' the module, which defines that package first.)";
48             }
49              
50 1         8 return bless \$arg, 'Template::Plugin::Package::Proxy';
51             }
52              
53              
54             package Template::Plugin::Package::Proxy;
55              
56 2     2   11 use warnings;
  2         4  
  2         80  
57 2     2   7 use strict;
  2         4  
  2         317  
58              
59             our $AUTOLOAD;
60              
61             sub AUTOLOAD {
62 4     4   146 my $self = shift;
63 4         9 my $class = ref $self;
64              
65             # Strip name of the proxy class, leaving just the function to call.
66 4         46 my ($fn_name) = ($AUTOLOAD =~ /^$class\::(.*)/);
67              
68             # $$self evaluates to the name of the original class.
69             # Get a reference to the function to call.
70 4 50       37 my $fn = $$self->can( $fn_name ) or die "class $$self cannot $fn_name";
71              
72             # Call the function.
73 4         17 return $fn->( @_ );
74             }
75              
76              
77             sub DESTROY {
78 0     0     return;
79             }
80              
81              
82             =head1 SEE ALSO
83              
84             L
85              
86             =head1 COPYRIGHT & LICENSE
87              
88             Copyright 2024 Andy Lester.
89              
90             This library is free software; you can redistribute it and/or modify it
91             under the terms of the Artistic License version 2.0.
92              
93             =head1 ACKNOWLEDGEMENTS
94              
95             Template::Plugin::Package is taken directly from Template::Plugin::Class.
96              
97             =head1 AUTHOR
98              
99             Current maintainer: Andy Lester, C<< >>
100              
101             =cut
102              
103             1;