.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "HTML::Mason::Component 3pm" .TH HTML::Mason::Component 3pm 2024-03-05 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME HTML::Mason::Component \- Mason Component Class .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 3 \& my $comp1 = $m\->current_comp; \& my $comp2 = $m\->callers(1); \& my $comp3 = $m\->fetch_comp(\*(Aqfoo/bar\*(Aq); \& \& foreach ($comp1,$comp2,$comp3) { \& print "My name is ".$_\->title.".\en"; \& } .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Mason uses the Component class to store components loaded into memory. Components come from three distinct sources: .IP 1. 4 File-based: loaded from a source or object file. .IP 2. 4 Subcomponents: embedded components defined with the \f(CW\*(C`<%def>\*(C'\fR or \f(CW\*(C`<%method>\*(C'\fR tags. .IP 3. 4 Anonymous: created on-the-fly with the \f(CW\*(C`make_component\*(C'\fR Interp method. .PP Some of the methods below return different values (or nothing at all) depending on the component type. .PP 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. .SS "CREATING AND ACCESSING COMPONENTS" .IX Subsection "CREATING AND ACCESSING COMPONENTS" Common ways to get handles on existing component objects include the Request\->current_comp, Request\->callers, and Request\->fetch_comp methods. .PP There is no published \f(CW\*(C`new\*(C'\fR method, because creating a component requires an Interpreter. Use the make_component method to create a new component dynamically. .PP Similarly, there is no \f(CW\*(C`execute\*(C'\fR or \f(CW\*(C`call\*(C'\fR method, because calling a component requires a request. All of the interfaces for calling a component (\f(CW\*(C`<& &>\*(C'\fR, \f(CW\*(C`$m\->comp\*(C'\fR, \f(CW\*(C`$interp\->exec\*(C'\fR) which normally take a component path will also take a component object. .SH METHODS .IX Header "METHODS" .IP "attr (name)" 4 .IX Item "attr (name)" 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 \f(CW\*(C`<%attr>\*(C'\fR section. .IP "attr_if_exists (name)" 4 .IX Item "attr_if_exists (name)" This method works exactly like the one above but returns undef if the attribute does not exist. .IP "attr_exists (name)" 4 .IX Item "attr_exists (name)" Returns true if the specified attribute exists in this component or one of its parents, undef otherwise. .IP attributes 4 .IX Item "attributes" 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. .IP "call_method (name, args...)" 4 .IX Item "call_method (name, args...)" 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 \f(CW\*(C`<%method>\*(C'\fR section. .IP create_time 4 .IX Item "create_time" A synonym for load_time (deprecated). .IP declared_args 4 .IX Item "declared_args" Returns a reference to a hash of hashes representing the arguments declared in the \f(CW\*(C`<%args>\*(C'\fR section. The keys of the main hash are the variable names including prefix (e.g. \f(CW$foo\fR, \f(CW@list\fR). Each secondary hash contains: .RS 4 .IP \(bu 4 \&'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. .RE .RS 4 .Sp For example: .Sp .Vb 2 \& # does $comp have an argument called $fido? \& if (exists($comp\->declared_args\->{\*(Aq$fido\*(Aq})) { ... } \& \& # does $fido have a default value? \& if (defined($comp\->declared_args\->{\*(Aq$fido\*(Aq}\->{default})) { ... } .Ve .RE .IP dir_path 4 .IX Item "dir_path" 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. .IP "flag (name)" 4 .IX Item "flag (name)" Returns the value for the specified system flag. Flags are declared in the \f(CW\*(C`<%flags>\*(C'\fR section and affect the behavior of the component. Unlike attributes, flags values do not get inherited from parent components. .IP is_subcomp 4 .IX Item "is_subcomp" Returns true if this is a subcomponent of another component. For historical reasons, this returns true for both methods and subcomponents. .IP is_method 4 .IX Item "is_method" Returns true if this is a method. .IP is_file_based 4 .IX Item "is_file_based" Returns true if this component was loaded from a source or object file. .IP load_time 4 .IX Item "load_time" Returns the time (in Perl \fBtime()\fR format) when this component object was created. .IP "method_exists (name)" 4 .IX Item "method_exists (name)" Returns true if the specified user-defined method exists in this component or one of its parents, undef otherwise. .IP methods 4 .IX Item "methods" This method works exactly like the subcomps method, but it returns methods, not subcomponents. This does not return methods inherited from parent components. .Sp Methods are declared in \f(CW\*(C`<%method>\*(C'\fR sections. .IP name 4 .IX Item "name" 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 \f(CW\*(C`<%def>\*(C'\fR. Undefined for anonymous components. .IP object_file 4 .IX Item "object_file" Returns the object filename for this component. .IP parent 4 .IX Item "parent" Returns the parent of this component for inheritance purposes, by default the nearest \f(CW\*(C`autohandler\*(C'\fR in or above the component's directory. Can be changed via the \f(CW\*(C`inherit\*(C'\fR flag. .IP path 4 .IX Item "path" Returns the entire path of this component, relative to the component root. .IP "scall_method (name, args...)" 4 .IX Item "scall_method (name, args...)" 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. .IP subcomps 4 .IX Item "subcomps" 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. .Sp .Vb 3 \& if (my $subcomp = $comp\->subcomps(\*(Aq.link\*(Aq)) { \& ... \& } .Ve .Sp Subcomponents are declared in \f(CW\*(C`<%def>\*(C'\fR sections. .IP title 4 .IX Item "title" 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. .Sp 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]". .SH "FILE-BASED METHODS" .IX Header "FILE-BASED METHODS" The following methods apply only to file-based components (those loaded from source or object files). They return undef for other component types. .IP source_file 4 .IX Item "source_file" Returns the source filename for this component. .IP source_dir 4 .IX Item "source_dir" Returns the directory of the source filename for this component.