.\" -*- 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 "Mojo::DOM58 3" .TH Mojo::DOM58 3 2023-01-20 "perl v5.36.0" "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 Mojo::DOM58 \- Minimalistic HTML/XML DOM parser with CSS selectors .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use Mojo::DOM58; \& \& # Parse \& my $dom = Mojo::DOM58\->new(\*(Aq
Test
123
456
\*(Aq); \& $dom\->at(\*(Aq#c\*(Aq)\->prepend($dom\->new_tag(\*(Aqp\*(Aq, id => \*(Aqd\*(Aq, \*(Aq789\*(Aq)); \& $dom\->find(\*(Aq:not(p)\*(Aq)\->map(\*(Aqstrip\*(Aq); \& \& # Render \& say "$dom"; .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Mojo::DOM58 is a minimalistic and relaxed pure-perl HTML/XML DOM parser based on Mojo::DOM. It supports the HTML Living StandardHi!
\*(Aq); \& say $dom\->at(\*(Aqp[id]\*(Aq)\->text; .Ve .PP If an XML declaration is found, the parser will automatically switch into XML mode and everything becomes case-sensitive. .PP .Vb 3 \& # XML semantics \& my $dom = Mojo::DOM58\->new(\*(AqHi!
\*(Aq); \& say $dom\->at(\*(AqP[ID]\*(Aq)\->text; .Ve .PP HTML or XML semantics can also be forced with the "xml" method. .PP .Vb 3 \& # Force HTML semantics \& my $dom = Mojo::DOM58\->new\->xml(0)\->parse(\*(AqHi!
\*(Aq); \& say $dom\->at(\*(Aqp[id]\*(Aq)\->text; \& \& # Force XML semantics \& my $dom = Mojo::DOM58\->new\->xml(1)\->parse(\*(AqHi!
\*(Aq); \& say $dom\->at(\*(AqP[ID]\*(Aq)\->text; .Ve .SH SELECTORS .IX Header "SELECTORS" Mojo::DOM58 uses a CSS selector engine based on Mojo::DOM::CSS. All CSS selectors that make sense for a standalone parser are supported. .IP * 4 Any element. .Sp .Vb 1 \& my $all = $dom\->find(\*(Aq*\*(Aq); .Ve .IP E 4 .IX Item "E" An element of type \f(CW\*(C`E\*(C'\fR. .Sp .Vb 1 \& my $title = $dom\->at(\*(Aqtitle\*(Aq); .Ve .IP E[foo] 4 .IX Item "E[foo]" An \f(CW\*(C`E\*(C'\fR element with a \f(CW\*(C`foo\*(C'\fR attribute. .Sp .Vb 1 \& my $links = $dom\->find(\*(Aqa[href]\*(Aq); .Ve .IP "E[foo=""bar""]" 4 .IX Item "E[foo=""bar""]" An \f(CW\*(C`E\*(C'\fR element whose \f(CW\*(C`foo\*(C'\fR attribute value is exactly equal to \f(CW\*(C`bar\*(C'\fR. .Sp .Vb 2 \& my $case_sensitive = $dom\->find(\*(Aqinput[type="hidden"]\*(Aq); \& my $case_sensitive = $dom\->find(\*(Aqinput[type=hidden]\*(Aq); .Ve .IP "E[foo=""bar"" i]" 4 .IX Item "E[foo=""bar"" i]" An \f(CW\*(C`E\*(C'\fR element whose \f(CW\*(C`foo\*(C'\fR attribute value is exactly equal to any (ASCII-range) case-permutation of \f(CW\*(C`bar\*(C'\fR. Note that this selector is \&\fBEXPERIMENTAL\fR and might change without warning! .Sp .Vb 3 \& my $case_insensitive = $dom\->find(\*(Aqinput[type="hidden" i]\*(Aq); \& my $case_insensitive = $dom\->find(\*(Aqinput[type=hidden i]\*(Aq); \& my $case_insensitive = $dom\->find(\*(Aqinput[class~="foo" i]\*(Aq); .Ve .Sp This selector is part of Selectors Level 4bar
baz\enI ♥ Mojo::DOM58!
\*(Aq); \& $dom = $dom\->append(Mojo::DOM58\->new); .Ve .PP Append HTML/XML fragment to this node (for all node types other than \f(CW\*(C`root\*(C'\fR). .PP .Vb 3 \& # "Test 123
" \& $dom\->parse(\*(AqTest
\*(Aq)\->at(\*(Aqp\*(Aq) \& \->child_nodes\->first\->append(\*(Aq 123\*(Aq)\->root; .Ve .SS append_content .IX Subsection "append_content" .Vb 2 \& $dom = $dom\->append_content(\*(AqI ♥ Mojo::DOM58!
\*(Aq); \& $dom = $dom\->append_content(Mojo::DOM58\->new); .Ve .PP Append HTML/XML fragment (for \f(CW\*(C`root\*(C'\fR and \f(CW\*(C`tag\*(C'\fR nodes) or raw content to this node's content. .PP .Vb 3 \& # "Test123
" \& $dom\->parse(\*(AqTest
\*(Aq)\->at(\*(Aqp\*(Aq)\->append_content(\*(Aq123\*(Aq)\->root; .Ve .SS at .IX Subsection "at" .Vb 2 \& my $result = $dom\->at(\*(Aqdiv ~ p\*(Aq); \& my $result = $dom\->at(\*(Aqsvg|line\*(Aq, svg => \*(Aqhttp://www.w3.org/2000/svg\*(Aq); .Ve .PP Find first descendant element of this element matching the CSS selector and return it as a Mojo::DOM58 object, or \f(CW\*(C`undef\*(C'\fR if none could be found. All selectors listed in "SELECTORS" are supported. .PP .Vb 2 \& # Find first element with "svg" namespace definition \& my $namespace = $dom\->at(\*(Aq[xmlns\e:svg]\*(Aq)\->{\*(Aqxmlns:svg\*(Aq}; .Ve .PP Trailing key/value pairs can be used to declare xml namespace aliases. .PP .Vb 3 \& # "123
" \& $dom\->parse(\*(AqTest123
\*(Aq)\->at(\*(Aqp\*(Aq)\->child_nodes\->first\->remove; \& \& # "" \& $dom\->parse(\*(Aq123\*(Aq)\->child_nodes\->first; \& \& # " Test " \& $dom\->parse(\*(Aq123\*(Aq)\->child_nodes\->last\->content; .Ve .SS children .IX Subsection "children" .Vb 2 \& my $collection = $dom\->children; \& my $collection = $dom\->children(\*(Aqdiv ~ p\*(Aq); .Ve .PP Find all child elements of this element matching the CSS selector and return a collection containing these elements as Mojo::DOM58 objects. All selectors listed in "SELECTORS" are supported. .PP .Vb 2 \& # Show tag name of random child element \& say $dom\->children\->shuffle\->first\->tag; .Ve .SS content .IX Subsection "content" .Vb 3 \& my $str = $dom\->content; \& $dom = $dom\->content(\*(AqI ♥ Mojo::DOM58!
\*(Aq); \& $dom = $dom\->content(Mojo::DOM58\->new); .Ve .PP Return this node's content or replace it with HTML/XML fragment (for \f(CW\*(C`root\*(C'\fR and \f(CW\*(C`tag\*(C'\fR nodes) or raw content. .PP .Vb 2 \& # "Test" \& $dom\->parse(\*(Aq123
" \& $dom\->parse(\*(AqTest
\*(Aq)\->at(\*(Aqp\*(Aq)\->content(\*(Aq123\*(Aq)\->root; \& \& # "123
" \& $dom\->parse(\*(Aq123
\*(Aq) \& \->descendant_nodes\->grep(sub { $_\->type eq \*(Aqcomment\*(Aq }) \& \->map(\*(Aqremove\*(Aq)\->first; \& \& # "testtest
" \& $dom\->parse(\*(Aq123456
\*(Aq) \& \->at(\*(Aqp\*(Aq)\->descendant_nodes\->grep(sub { $_\->type eq \*(Aqtext\*(Aq }) \& \->map(content => \*(Aqtest\*(Aq)\->first\->root; .Ve .SS find .IX Subsection "find" .Vb 2 \& my $collection = $dom\->find(\*(Aqdiv ~ p\*(Aq); \& my $collection = $dom\->find(\*(Aqsvg|line\*(Aq, svg => \*(Aqhttp://www.w3.org/2000/svg\*(Aq); .Ve .PP Find all descendant elements of this element matching the CSS selector and return a collection containing these elements as Mojo::DOM58 objects. All selectors listed in "SELECTORS" are supported. .PP .Vb 2 \& # Find a specific element and extract information \& my $id = $dom\->find(\*(Aqdiv\*(Aq)\->[23]{id}; \& \& # Extract information from multiple elements \& my @headers = $dom\->find(\*(Aqh1, h2, h3\*(Aq)\->map(\*(Aqtext\*(Aq)\->each; \& \& # Count all the different tags \& my $hash = $dom\->find(\*(Aq*\*(Aq)\->reduce(sub { $a\->{$b\->tag}++; $a }, {}); \& \& # Find elements with a class that contains dots \& my @divs = $dom\->find(\*(Aqdiv.foo\e.bar\*(Aq)\->each; .Ve .PP Trailing key/value pairs can be used to declare xml namespace aliases. .PP .Vb 3 \& # "A
C\*(Aq)\->at(\*(Aqp\*(Aq)\->following_nodes\->last\->content; .Ve .SS matches .IX Subsection "matches" .Vb 2 \& my $bool = $dom\->matches(\*(Aqdiv ~ p\*(Aq); \& my $bool = $dom\->matches(\*(Aqsvg|line\*(Aq, svg => \*(Aqhttp://www.w3.org/2000/svg\*(Aq); .Ve .PP Check if this element matches the CSS selector. All selectors listed in "SELECTORS" are supported. .PP .Vb 3 \& # True \& $dom\->parse(\*(AqA
\*(Aq)\->at(\*(Aqp\*(Aq)\->matches(\*(Aq.a\*(Aq); \& $dom\->parse(\*(AqA
\*(Aq)\->at(\*(Aqp\*(Aq)\->matches(\*(Aqp[class]\*(Aq); \& \& # False \& $dom\->parse(\*(AqA
\*(Aq)\->at(\*(Aqp\*(Aq)\->matches(\*(Aq.b\*(Aq); \& $dom\->parse(\*(AqA
\*(Aq)\->at(\*(Aqp\*(Aq)\->matches(\*(Aqp[id]\*(Aq); .Ve .PP Trailing key/value pairs can be used to declare xml namespace aliases. .PP .Vb 3 \& # True \& $dom\->parse(\*(Aq\*(Aq) \& \->matches(\*(Aqsvg|rect\*(Aq, svg => \*(Aqhttp://www.w3.org/2000/svg\*(Aq); .Ve .SS namespace .IX Subsection "namespace" .Vb 1 \& my $namespace = $dom\->namespace; .Ve .PP Find this element's namespace, or return \f(CW\*(C`undef\*(C'\fR if none could be found. .PP .Vb 2 \& # "http://www.w3.org/2000/svg" \& Mojo::DOM58\->new(\*(Aq\*(Aq)\->at(\*(Aqsvg\e:circle\*(Aq)\->namespace; \& \& # Find namespace for an element with namespace prefix \& my $namespace = $dom\->at(\*(Aqsvg > svg\e:circle\*(Aq)\->namespace; \& \& # Find namespace for an element that may or may not have a namespace prefix \& my $namespace = $dom\->at(\*(Aqsvg > circle\*(Aq)\->namespace; .Ve .SS next .IX Subsection "next" .Vb 1 \& my $sibling = $dom\->next; .Ve .PP Return Mojo::DOM58 object for next sibling element, or \f(CW\*(C`undef\*(C'\fR if there are no more siblings. .PP .Vb 2 \& # "123456
\*(Aq) \& \->at(\*(Aqb\*(Aq)\->next_node\->next_node; \& \& # " Test " \& $dom\->parse(\*(Aq123456
\*(Aq) \& \->at(\*(Aqb\*(Aq)\->next_node\->content; .Ve .SS parent .IX Subsection "parent" .Vb 1 \& my $parent = $dom\->parent; .Ve .PP Return Mojo::DOM58 object for parent of this node, or \f(CW\*(C`undef\*(C'\fR if this node has no parent. .PP .Vb 2 \& # "Test" \& $dom\->parse(\*(AqTest
\*(Aq)\->at(\*(Aqi\*(Aq)\->parent; .Ve .SS parse .IX Subsection "parse" .Vb 1 \& $dom = $dom\->parse(\*(AqC
\*(Aq)\->at(\*(Aqp\*(Aq)\->preceding_nodes\->first\->content; .Ve .SS prepend .IX Subsection "prepend" .Vb 2 \& $dom = $dom\->prepend(\*(AqI ♥ Mojo::DOM58!
\*(Aq); \& $dom = $dom\->prepend(Mojo::DOM58\->new); .Ve .PP Prepend HTML/XML fragment to this node (for all node types other than \f(CW\*(C`root\*(C'\fR). .PP .Vb 3 \& # "Test 123
" \& $dom\->parse(\*(Aq123
\*(Aq) \& \->at(\*(Aqp\*(Aq)\->child_nodes\->first\->prepend(\*(AqTest \*(Aq)\->root; .Ve .SS prepend_content .IX Subsection "prepend_content" .Vb 2 \& $dom = $dom\->prepend_content(\*(AqI ♥ Mojo::DOM58!
\*(Aq); \& $dom = $dom\->prepend_content(Mojo::DOM58\->new); .Ve .PP Prepend HTML/XML fragment (for \f(CW\*(C`root\*(C'\fR and \f(CW\*(C`tag\*(C'\fR nodes) or raw content to this node's content. .PP .Vb 3 \& # "123Test
" \& $dom\->parse(\*(AqTest
\*(Aq)\->at(\*(Aqp\*(Aq)\->prepend_content(\*(Aq123\*(Aq)\->root; .Ve .SS previous .IX Subsection "previous" .Vb 1 \& my $sibling = $dom\->previous; .Ve .PP Return Mojo::DOM58 object for previous sibling element, or \f(CW\*(C`undef\*(C'\fR if there are no more siblings. .PP .Vb 2 \& # "123456
\*(Aq) \& \->at(\*(Aqb\*(Aq)\->previous_node\->previous_node; \& \& # " Test " \& $dom\->parse(\*(Aq123456
\*(Aq) \& \->at(\*(Aqb\*(Aq)\->previous_node\->content; .Ve .SS remove .IX Subsection "remove" .Vb 1 \& my $parent = $dom\->remove; .Ve .PP Remove this node and return "root" (for \f(CW\*(C`root\*(C'\fR nodes) or "parent". .PP .Vb 2 \& # "" \& $dom\->parse(\*(Aq456
" \& $dom\->parse(\*(Aq123456
\*(Aq) \& \->at(\*(Aqp\*(Aq)\->child_nodes\->first\->remove\->root; .Ve .SS replace .IX Subsection "replace" .Vb 2 \& my $parent = $dom\->replace(\*(Aq123
" \& $dom\->parse(\*(AqTest
\*(Aq) \& \->at(\*(Aqp\*(Aq)\->child_nodes\->[0]\->replace(\*(Aq123\*(Aq)\->root; .Ve .SS root .IX Subsection "root" .Vb 1 \& my $root = $dom\->root; .Ve .PP Return Mojo::DOM58 object for \f(CW\*(C`root\*(C'\fR node. .SS selector .IX Subsection "selector" .Vb 1 \& my $selector = $dom\->selector; .Ve .PP Get a unique CSS selector for this element. .PP .Vb 2 \& # "ul:nth\-child(1) > li:nth\-child(2)" \& $dom\->parse(\*(AqTest
\*(Aq)\->at(\*(Aqi\*(Aq)\->selector; .Ve .SS strip .IX Subsection "strip" .Vb 1 \& my $parent = $dom\->strip; .Ve .PP Remove this element while preserving its content and return "parent". .PP .Vb 2 \& # "bar
bazbar
baz\enTest
\*(Aq)\->type; \& \& # "tag" \& $dom\->parse(\*(AqTest
\*(Aq)\->at(\*(Aqp\*(Aq)\->type; \& \& # "text" \& $dom\->parse(\*(AqTest
\*(Aq)\->at(\*(Aqp\*(Aq)\->child_nodes\->first\->type; .Ve .SS val .IX Subsection "val" .Vb 1 \& my $value = $dom\->val; .Ve .PP Extract value from form element (such as \f(CW\*(C`button\*(C'\fR, \f(CW\*(C`input\*(C'\fR, \f(CW\*(C`option\*(C'\fR, \&\f(CW\*(C`select\*(C'\fR and \f(CW\*(C`textarea\*(C'\fR), or return \f(CW\*(C`undef\*(C'\fR if this element has no value. In the case of \f(CW\*(C`select\*(C'\fR with \f(CW\*(C`multiple\*(C'\fR attribute, find \f(CW\*(C`option\*(C'\fR elements with \&\f(CW\*(C`selected\*(C'\fR attribute and return an array reference with all values, or \&\f(CW\*(C`undef\*(C'\fR if none could be found. .PP .Vb 2 \& # "a" \& $dom\->parse(\*(Aq\*(Aq)\->at(\*(Aqinput\*(Aq)\->val; \& \& # "b" \& $dom\->parse(\*(Aq\*(Aq)\->at(\*(Aqtextarea\*(Aq)\->val; \& \& # "c" \& $dom\->parse(\*(Aq\*(Aq)\->at(\*(Aqoption\*(Aq)\->val; \& \& # "d" \& $dom\->parse(\*(Aq\*(Aq) \& \->at(\*(Aqselect\*(Aq)\->val; \& \& # "e" \& $dom\->parse(\*(Aq\*(Aq) \& \->at(\*(Aqselect\*(Aq)\->val\->[0]; \& \& # "on" \& $dom\->parse(\*(Aq\*(Aq)\->at(\*(Aqinput\*(Aq)\->val; .Ve .SS with_roles .IX Subsection "with_roles" .Vb 3 \& my $new_class = Mojo::DOM58\->with_roles(\*(AqMojo::DOM58::Role::One\*(Aq); \& my $new_class = Mojo::DOM58\->with_roles(\*(Aq+One\*(Aq, \*(Aq+Two\*(Aq); \& $dom = $dom\->with_roles(\*(Aq+One\*(Aq, \*(Aq+Two\*(Aq); .Ve .PP Equivalent to "with_roles" in Mojo::Base. Note that role support depends on Role::Tiny (2.000001+). .SS wrap .IX Subsection "wrap" .Vb 2 \& $dom = $dom\->wrap(\*(Aq\*(Aq); \& $dom = $dom\->wrap(Mojo::DOM58\->new); .Ve .PP Wrap HTML/XML fragment around this node (for all node types other than \f(CW\*(C`root\*(C'\fR), placing it as the last child of the first innermost element. .PP .Vb 2 \& # "123Test
" \& $dom\->parse(\*(AqTest\*(Aq)\->at(\*(Aqb\*(Aq)\->wrap(\*(Aq123
\*(Aq)\->root; \& \& # "Test
123Test
123
" \& $dom\->parse(\*(AqTest\*(Aq)\->at(\*(Aqb\*(Aq)\->wrap(\*(Aq123
\*(Aq)\->root; \& \& # "Test
" \& $dom\->parse(\*(AqTest
\*(Aq)\->at(\*(Aqp\*(Aq)\->child_nodes\->first\->wrap(\*(Aq\*(Aq)\->root; .Ve .SS wrap_content .IX Subsection "wrap_content" .Vb 2 \& $dom = $dom\->wrap_content(\*(Aq\*(Aq); \& $dom = $dom\->wrap_content(Mojo::DOM58\->new); .Ve .PP Wrap HTML/XML fragment around this node's content (for \f(CW\*(C`root\*(C'\fR and \f(CW\*(C`tag\*(C'\fR nodes), placing it as the last children of the first innermost element. .PP .Vb 2 \& # "123Test
" \& $dom\->parse(\*(AqTest
\*(Aq)\->at(\*(Aqp\*(Aq)\->wrap_content(\*(Aq123\*(Aq)\->root; \& \& # "
Test
123
" \& $dom\->parse(\*(AqTest\*(Aq)\->wrap_content(\*(Aq123
\*(Aq); .Ve .SS xml .IX Subsection "xml" .Vb 2 \& my $bool = $dom\->xml; \& $dom = $dom\->xml($bool); .Ve .PP Disable HTML semantics in parser and activate case-sensitivity, defaults to auto detection based on XML declarations. .SH "COLLECTION METHODS" .IX Header "COLLECTION METHODS" Some Mojo::DOM58 methods return an array-based collection object based on Mojo::Collection, which can either be accessed directly as an array reference, or with the following methods. .PP .Vb 5 \& # Chain methods \& $collection\->map(sub { ucfirst })\->shuffle\->each(sub { \& my ($word, $num) = @_; \& say "$num: $word"; \& }); \& \& # Access array directly to manipulate collection \& $collection\->[23] += 100; \& say for @$collection; .Ve .SS compact .IX Subsection "compact" .Vb 1 \& my $new = $collection\->compact; .Ve .PP Create a new collection with all elements that are defined and not an empty string. .PP .Vb 2 \& # $collection contains (0, 1, undef, 2, \*(Aq\*(Aq, 3) \& $collection\->compact\->join(\*(Aq, \*(Aq); # "0, 1, 2, 3" .Ve .SS each .IX Subsection "each" .Vb 2 \& my @elements = $collection\->each; \& $collection = $collection\->each(sub {...}); .Ve .PP Evaluate callback for each element in collection or return all elements as a list if none has been provided. The element will be the first argument passed to the callback and is also available as \f(CW$_\fR. .PP .Vb 5 \& # Make a numbered list \& $collection\->each(sub { \& my ($e, $num) = @_; \& say "$num: $e"; \& }); .Ve .SS first .IX Subsection "first" .Vb 5 \& my $first = $collection\->first; \& my $first = $collection\->first(qr/foo/); \& my $first = $collection\->first(sub {...}); \& my $first = $collection\->first($method); \& my $first = $collection\->first($method, @args); .Ve .PP Evaluate regular expression/callback for, or call method on, each element in collection and return the first one that matched the regular expression, or for which the callback/method returned true. The element will be the first argument passed to the callback and is also available as \f(CW$_\fR. .PP .Vb 2 \& # Longer version \& my $first = $collection\->first(sub { $_\->$method(@args) }); \& \& # Find first value that contains the word "mojo" \& my $interesting = $collection\->first(qr/mojo/i); \& \& # Find first value that is greater than 5 \& my $greater = $collection\->first(sub { $_ > 5 }); .Ve .SS flatten .IX Subsection "flatten" .Vb 1 \& my $new = $collection\->flatten; .Ve .PP Flatten nested collections/arrays recursively and create a new collection with all elements. .PP .Vb 2 \& # $collection contains (1, [2, [3, 4], 5, [6]], 7) \& $collection\->flatten\->join(\*(Aq, \*(Aq); # "1, 2, 3, 4, 5, 6, 7" .Ve .SS grep .IX Subsection "grep" .Vb 4 \& my $new = $collection\->grep(qr/foo/); \& my $new = $collection\->grep(sub {...}); \& my $new = $collection\->grep($method); \& my $new = $collection\->grep($method, @args); .Ve .PP Evaluate regular expression/callback for, or call method on, each element in collection and create a new collection with all elements that matched the regular expression, or for which the callback/method returned true. The element will be the first argument passed to the callback and is also available as \f(CW$_\fR. .PP .Vb 2 \& # Longer version \& my $new = $collection\->grep(sub { $_\->$method(@args) }); \& \& # Find all values that contain the word "mojo" \& my $interesting = $collection\->grep(qr/mojo/i); \& \& # Find all values that are greater than 5 \& my $greater = $collection\->grep(sub { $_ > 5 }); .Ve .SS head .IX Subsection "head" .Vb 2 \& my $new = $collection\->head(4); \& my $new = $collection\->head(\-2); .Ve .PP Create a new collection with up to the specified number of elements from the beginning of the collection. A negative number will count from the end. .PP .Vb 3 \& # $collection contains (\*(AqA\*(Aq, \*(AqB\*(Aq, \*(AqC\*(Aq, \*(AqD\*(Aq, \*(AqE\*(Aq) \& $collection\->head(3)\->join(\*(Aq \*(Aq); # "A B C" \& $collection\->head(\-3)\->join(\*(Aq \*(Aq); # "A B" .Ve .SS join .IX Subsection "join" .Vb 2 \& my $stream = $collection\->join; \& my $stream = $collection\->join("\en"); .Ve .PP Turn collection into string. .PP .Vb 2 \& # Join all values with commas \& $collection\->join(\*(Aq, \*(Aq); .Ve .SS last .IX Subsection "last" .Vb 1 \& my $last = $collection\->last; .Ve .PP Return the last element in collection. .SS map .IX Subsection "map" .Vb 3 \& my $new = $collection\->map(sub {...}); \& my $new = $collection\->map($method); \& my $new = $collection\->map($method, @args); .Ve .PP Evaluate callback for, or call method on, each element in collection and create a new collection from the results. The element will be the first argument passed to the callback and is also available as \f(CW$_\fR. .PP .Vb 2 \& # Longer version \& my $new = $collection\->map(sub { $_\->$method(@args) }); \& \& # Append the word "mojo" to all values \& my $domified = $collection\->map(sub { $_ . \*(Aqmojo\*(Aq }); .Ve .SS reduce .IX Subsection "reduce" .Vb 2 \& my $result = $collection\->reduce(sub {...}); \& my $result = $collection\->reduce(sub {...}, $initial); .Ve .PP Reduce elements in collection with callback, the first element will be used as initial value if none has been provided. .PP .Vb 2 \& # Calculate the sum of all values \& my $sum = $collection\->reduce(sub { $a + $b }); \& \& # Count how often each value occurs in collection \& my $hash = $collection\->reduce(sub { $a\->{$b}++; $a }, {}); .Ve .SS reverse .IX Subsection "reverse" .Vb 1 \& my $new = $collection\->reverse; .Ve .PP Create a new collection with all elements in reverse order. .SS slice .IX Subsection "slice" .Vb 1 \& my $new = $collection\->slice(4 .. 7); .Ve .PP Create a new collection with all selected elements. .PP .Vb 2 \& # $collection contains (\*(AqA\*(Aq, \*(AqB\*(Aq, \*(AqC\*(Aq, \*(AqD\*(Aq, \*(AqE\*(Aq) \& $collection\->slice(1, 2, 4)\->join(\*(Aq \*(Aq); # "B C E" .Ve .SS shuffle .IX Subsection "shuffle" .Vb 1 \& my $new = $collection\->shuffle; .Ve .PP Create a new collection with all elements in random order. .SS size .IX Subsection "size" .Vb 1 \& my $size = $collection\->size; .Ve .PP Number of elements in collection. .SS sort .IX Subsection "sort" .Vb 2 \& my $new = $collection\->sort; \& my $new = $collection\->sort(sub {...}); .Ve .PP Sort elements based on return value of callback and create a new collection from the results. .PP .Vb 2 \& # Sort values case\-insensitive \& my $case_insensitive = $collection\->sort(sub { uc($a) cmp uc($b) }); .Ve .SS tail .IX Subsection "tail" .Vb 2 \& my $new = $collection\->tail(4); \& my $new = $collection\->tail(\-2); .Ve .PP Create a new collection with up to the specified number of elements from the end of the collection. A negative number will count from the beginning. .PP .Vb 3 \& # $collection contains (\*(AqA\*(Aq, \*(AqB\*(Aq, \*(AqC\*(Aq, \*(AqD\*(Aq, \*(AqE\*(Aq) \& $collection\->tail(3)\->join(\*(Aq \*(Aq); # "C D E" \& $collection\->tail(\-3)\->join(\*(Aq \*(Aq); # "D E" .Ve .SS tap .IX Subsection "tap" .Vb 1 \& $collection = $collection\->tap(sub {...}); .Ve .PP Equivalent to "tap" in Mojo::Base. .SS to_array .IX Subsection "to_array" .Vb 1 \& my $array = $collection\->to_array; .Ve .PP Turn collection into array reference. .SS uniq .IX Subsection "uniq" .Vb 4 \& my $new = $collection\->uniq; \& my $new = $collection\->uniq(sub {...}); \& my $new = $collection\->uniq($method); \& my $new = $collection\->uniq($method, @args); .Ve .PP Create a new collection without duplicate elements, using the string representation of either the elements or the return value of the callback/method to decide uniqueness. Note that \f(CW\*(C`undef\*(C'\fR and empty string are treated the same. .PP .Vb 2 \& # Longer version \& my $new = $collection\->uniq(sub { $_\->$method(@args) }); \& \& # $collection contains (\*(Aqfoo\*(Aq, \*(Aqbar\*(Aq, \*(Aqbar\*(Aq, \*(Aqbaz\*(Aq) \& $collection\->uniq\->join(\*(Aq \*(Aq); # "foo bar baz" \& \& # $collection contains ([1, 2], [2, 1], [3, 2]) \& $collection\->uniq(sub{ $_\->[1] })\->to_array; # "[[1, 2], [2, 1]]" .Ve .SS with_roles .IX Subsection "with_roles" .Vb 1 \& $collection = $collection\->with_roles(\*(AqMojo::Collection::Role::One\*(Aq); .Ve .PP Equivalent to "with_roles" in Mojo::Base. Note that role support depends on Role::Tiny (2.000001+). .SH DEBUGGING .IX Header "DEBUGGING" You can set the \f(CW\*(C`MOJO_DOM58_CSS_DEBUG\*(C'\fR environment variable to get some advanced diagnostics information printed to \&\f(CW\*(C`STDERR\*(C'\fR. .PP .Vb 1 \& MOJO_DOM58_CSS_DEBUG=1 .Ve .SH BUGS .IX Header "BUGS" Report issues related to the format of this distribution or Perl 5.8 support to the public bugtracker. Any other issues should be reported directly to the upstream Mojolicious issue tracker. .SH AUTHOR .IX Header "AUTHOR" Dan Book