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