Mason uses the Component class to store components loaded into
memory. Components come from three distinct sources:
File-based: loaded from a source or object file.
Subcomponents: embedded components defined with the <%def>
or <%method> tags.
Anonymous: created on-the-fly with the make_component Interp method.
Some of the methods below return different values (or nothing at all)
depending on the component type.
The component API is primarily useful for introspection, e.g. "what
component called me" or "does the next component take a certain
argument". You can build complex Mason sites without ever dealing
directly with a component object.
There is no published new method, because creating a component
requires an Interpreter. Use the
make_component method to
create a new component dynamically.
Similarly, there is no execute or call method, because calling a
component requires a request. All of the interfaces for calling a
component (<& &>, $m->comp, $interp->exec)
which normally take a component path will also take a component
object.
Looks for the specified attribute in this component and its parents,
returning the first value found. Dies with an error if not
found. Attributes are declared in the <%attr> section.
Returns a hashref containing the attributes defined in this component,
with the attribute names as keys. This does not return attributes
inherited from parent components.
Looks for the specified user-defined method in this component and its
parents, calling the first one found. Dies with an error if not found.
Methods are declared in the <%method> section.
Returns a reference to a hash of hashes representing the arguments
declared in the <%args> section. The keys of the main hash are the
variable names including prefix (e.g. $foo, @list). Each
secondary hash contains:
'default': the string specified for default value (e.g. 'fido') or undef
if none specified. Note that in general this is not the default value
itself but rather a Perl expression that gets evaluated every time the
component runs.
For example:
# does $comp have an argument called $fido?
if (exists($comp->declared_args->{'$fido'})) { ... }
# does $fido have a default value?
if (defined($comp->declared_args->{'$fido'}->{default})) { ... }
Returns the component's notion of a current directory, relative to the
component root; this is used to resolve relative component paths. For
file-based components this is the full component path minus the filename.
For subcomponents this is the same as the component that defines it.
Undefined for anonymous components.
Returns the value for the specified system flag. Flags are declared
in the <%flags> section and affect the behavior of the component.
Unlike attributes, flags values do not get inherited from parent components.
This method works exactly like the
subcomps method, but it
returns methods, not subcomponents. This does not return methods
inherited from parent components.
Returns a short name of the component. For file-based components this
is the filename without the path. For subcomponents this is the name
specified in <%def>. Undefined for anonymous components.
Returns the parent of this component for inheritance purposes, by
default the nearest autohandler in or above the component's directory.
Can be changed via the inherit flag.
Like item_call_method, but returns the method output as
a string instead of printing it. (Think sprintf versus printf.) The
method's return value, if any, is discarded.
With no arguments, returns a hashref containing the subcomponents defined
in this component, with names as keys and component objects as values.
With one argument, returns the subcomponent of that name
or undef if no such subcomponent exists. e.g.
if (my $subcomp = $comp->subcomps('.link')) {
...
}
Returns a printable string denoting this component. It is intended to
uniquely identify a component within a given interpreter although this
is not 100% guaranteed. Mason uses this string in error messages,
among other places.
For file-based components this is the component path. For
subcomponents this is "parent_component_path:subcomponent_name". For
anonymous components this is a unique label like "[anon 17]".