1Class::Forward(3) User Contributed Perl Documentation Class::Forward(3)
2
3
4
6 Class::Forward - Namespace Dispatch and Resolution
7
9 version 0.100006
10
12 use Class::Forward;
13
14 # create a resolution object
15 my $res = Class::Forward->new(namespace => 'MyApp');
16
17 # returns MyApp::Data
18 say $res->forward('data');
19
20 # returns a MyApp::Data instance
21 my $data = $res->forward('data.new');
22
23 # returns the string /my_app/data
24 my $string = $res->reverse('data.new');
25
26 # returns MyApp::Data
27 say $res->forward($string);
28
30 Class::Forward is designed to resolve Perl namespaces from shorthand
31 (which is simply a file-path-like specification). Class::Forward can
32 also be used to dispatch method calls using said shorthand. See the
33 following exported functions for examples on how this can be used.
34
36 clsf
37 The exported function clsf is responsible for resolving your shorthand.
38 The following is an example of how it functions:
39
40 package App::Store;
41
42 use CGI;
43 use Class::Forward;
44
45 clsf; # returns App::Store
46 clsf './user'; # returns App::Store::User
47 clsf './user.new', name => 'N30'; # return a new App::Store::User object
48 clsf './user_profile.new'; # ... App::Store::UserProfile object
49 clsf '../user'; # returns App::User
50 clsf '//'; # returns App; (top of the calling class)
51 clsf '//.new'; # returns a new App object
52 clsf '//view'; # ... returns App::View
53 clsf '//view.new'; # ... returns a new App::View object
54 clsf '//view.new.render'; # ... dispatches methods in succession
55 clsf 'cgi'; # returns App::Store::Cgi
56 clsf '/cgi'; # returns Cgi (or CGI if already loaded)
57
58 1;
59
60 The clsf function takes two arguments, the shorthand to be translated,
61 and an optional list of arguments to be passed to the last method
62 appended to the shorthand.
63
64 clsr
65 The exported function clsr is responsible for resolving your shorthand.
66 The following is an example of how it functions:
67
68 package App::Store;
69
70 use CGI;
71 use Class::Forward;
72
73 clsr; # returns /app/store
74 clsr './user'; # returns /app/store/user
75 clsr './user.new', name => 'N30'; # returns /app/store/user
76 clsr './user_profile'; # returns /app/store/user_profile
77 clsr '../user'; # returns /app/user
78 clsr '//'; # returns /app
79 clsr '//.new'; # returns /app
80 clsr '//view'; # returns /app/view
81 clsr '//view.new'; # returns /app/view
82 clsr '//view.new.render'; # returns /app/view
83 clsr 'cgi'; # returns /app/store/cgi
84 clsr '/cgi'; # returns /cgi
85
86 1;
87
88 The clsr function takes three arguments, the shorthand to be translated
89 (required), the offset (optional level of namespace nodes to omit left-
90 to-right), and the delimiter to be used to generate the resulting path
91 (defaults to forward-slash).
92
94 new
95 The new method is used to instantiate a new instance.
96
97 namespace
98 The namespace method is used to get/set the root namespace used as an
99 anchor for all resolution requests.
100
101 my $namespace = $self->namespace('MyApp');
102
103 forward
104 The forward (or forward_lookup) method is used to resolve Perl
105 namespaces from path-like shorthand.
106
107 say $self->forward('example');
108 # given a default namespace of MyApp
109 # prints MyApp::Example
110
111 reverse
112 The reverse method (or reverse_lookup) is used to generate path-like
113 shorthand from Perl namespaces.
114
115 say $self->reverse('Simple::Example');
116 # given a default namespace of MyApp
117 # prints /my_app/simple/example
118
119 say $self->reverse('Simple::Example', 1);
120 # given a default namespace of MyApp
121 # prints simple/example
122
123 say $self->reverse('Simple::Example', 1, '_');
124 # given a default namespace of MyApp
125 # prints simple_example
126
128 Class::Forward was designed to provide shorthand and easy access to
129 class namespaces in an environment where you're dealing with a
130 multitude of long well-named classes. In that vein, it provides an
131 alternative to modules like aliased, aliased::factory, as, and the
132 like, and also modules like Namespace::Dispatch which are similar
133 enough to be mentioned but really address a completely different issue.
134
136 Al Newkirk <anewkirk@ana.io>
137
139 This software is copyright (c) 2012 by Al Newkirk.
140
141 This is free software; you can redistribute it and/or modify it under
142 the same terms as the Perl 5 programming language system itself.
143
144
145
146perl v5.28.0 2013-08-08 Class::Forward(3)