1HTML::Mason::Component(U3s)er Contributed Perl DocumentatHiToMnL::Mason::Component(3)
2
3
4
6 HTML::Mason::Component - Mason Component Class
7
9 my $comp1 = $m->current_comp;
10 my $comp2 = $m->callers(1);
11 my $comp3 = $m->fetch_comp('foo/bar');
12
13 foreach ($comp1,$comp2,$comp3) {
14 print "My name is ".$_->title.".\n";
15 }
16
18 Mason uses the Component class to store components loaded into memory.
19 Components come from three distinct sources:
20
21 1 File-based: loaded from a source or object file.
22
23 2 Subcomponents: embedded components defined with the "<%def>" or
24 "<%method>" tags.
25
26 3 Anonymous: created on-the-fly with the "make_component" Interp
27 method.
28
29 Some of the methods below return different values (or nothing at all)
30 depending on the component type.
31
32 The component API is primarily useful for introspection, e.g. "what
33 component called me" or "does the next component take a certain argu‐
34 ment". You can build complex Mason sites without ever dealing directly
35 with a component object.
36
37 CREATING AND ACCESSING COMPONENTS
38
39 Common ways to get handles on existing component objects include the
40 Request->current_comp, Request->callers, and Request->fetch_comp meth‐
41 ods.
42
43 There is no published "new" method, because creating a component
44 requires an Interpreter. Use the make_component method to create a new
45 component dynamically.
46
47 Similarly, there is no "execute" or "call" method, because calling a
48 component requires a request. All of the interfaces for calling a com‐
49 ponent (<& &>, "$m-"comp>, "$interp->exec") which normally take a com‐
50 ponent path will also take a component object.
51
53 attr (name)
54 Looks for the specified attribute in this component and its par‐
55 ents, returning the first value found. Dies with an error if not
56 found. Attributes are declared in the "<%attr>" section.
57
58 attr_if_exists (name)
59 This method works exactly like the one above but returns undef if
60 the attribute does not exist.
61
62 attr_exists (name)
63 Returns true if the specified attribute exists in this component or
64 one of its parents, undef otherwise.
65
66 attributes
67 Returns a hashref containing the attributes defined in this compo‐
68 nent, with the attribute names as keys. This does not return
69 attributes inherited from parent components.
70
71 call_method (name, args...)
72 Looks for the specified user-defined method in this component and
73 its parents, calling the first one found. Dies with an error if not
74 found. Methods are declared in the "<%method>" section.
75
76 create_time
77 A synonym for load_time (deprecated).
78
79 declared_args
80 Returns a reference to a hash of hashes representing the arguments
81 declared in the "<%args>" section. The keys of the main hash are
82 the variable names including prefix (e.g. $foo, @list). Each sec‐
83 ondary hash contains:
84
85 * 'default': the string specified for default value (e.g. 'fido')
86 or undef if none specified. Note that in general this is not
87 the default value itself but rather a Perl expression that gets
88 evaluated every time the component runs.
89
90 For example:
91
92 # does $comp have an argument called $fido?
93 if (exists($comp->declared_args->{'$fido'})) { ... }
94
95 # does $fido have a default value?
96 if (defined($comp->declared_args->{'$fido'}->{default})) { ... }
97
98 dir_path
99 Returns the component's notion of a current directory, relative to
100 the component root; this is used to resolve relative component
101 paths. For file-based components this is the full component path
102 minus the filename. For subcomponents this is the same as the com‐
103 ponent that defines it. Undefined for anonymous components.
104
105 flag (name)
106 Returns the value for the specified system flag. Flags are
107 declared in the "<%flags>" section and affect the behavior of the
108 component. Unlike attributes, flags values do not get inherited
109 from parent components.
110
111 is_subcomp
112 Returns true if this is a subcomponent of another component. For
113 historical reasons, this returns true for both methods and subcom‐
114 ponents.
115
116 is_method
117 Returns true if this is a method.
118
119 is_file_based
120 Returns true if this component was loaded from a source or object
121 file.
122
123 load_time
124 Returns the time (in Perl time() format) when this component object
125 was created.
126
127 method_exists (name)
128 Returns true if the specified user-defined method exists in this
129 component or one of its parents, undef otherwise.
130
131 methods
132 This method works exactly like the subcomps method, but it returns
133 methods, not subcomponents. This does not return methods inherited
134 from parent components.
135
136 Methods are declared in "<%method>" sections.
137
138 name
139 Returns a short name of the component. For file-based components
140 this is the filename without the path. For subcomponents this is
141 the name specified in "<%def>". Undefined for anonymous components.
142
143 object_file
144 Returns the object filename for this component.
145
146 parent
147 Returns the parent of this component for inheritance purposes, by
148 default the nearest "autohandler" in or above the component's
149 directory. Can be changed via the "inherit" flag.
150
151 path
152 Returns the entire path of this component, relative to the compo‐
153 nent root.
154
155 scall_method (name, args...)
156 Like item_call_method, but returns the method output as a string
157 instead of printing it. (Think sprintf versus printf.) The method's
158 return value, if any, is discarded.
159
160 subcomps
161 With no arguments, returns a hashref containing the subcomponents
162 defined in this component, with names as keys and component objects
163 as values. With one argument, returns the subcomponent of that
164 name or undef if no such subcomponent exists. e.g.
165
166 if (my $subcomp = $comp->subcomps('.link')) {
167 ...
168 }
169
170 Subcomponents are declared in "<%def>" sections.
171
172 title
173 Returns a printable string denoting this component. It is intended
174 to uniquely identify a component within a given interpreter
175 although this is not 100% guaranteed. Mason uses this string in
176 error messages, among other places.
177
178 For file-based components this is the component path. For subcom‐
179 ponents this is "parent_component_path:subcomponent_name". For
180 anonymous components this is a unique label like "[anon 17]".
181
183 The following methods apply only to file-based components (those loaded
184 from source or object files). They return undef for other component
185 types.
186
187 source_file
188 Returns the source filename for this component.
189
190 source_dir
191 Returns the directory of the source filename for this component.
192
194 HTML::Mason, HTML::Mason::Devel, HTML::Mason::Request
195
196
197
198perl v5.8.8 2007-04-17 HTML::Mason::Component(3)