I'm brand new to Mojolicious (less than a week) and have some hopefully
simple questions about the Mojo::Collection map and Mojo::DOM attr methods
(Mojolicious v6.53).
I've seen numerous code examples similar to the following which don't
*seem* to match the map method signature in the documentation.
my $product_ids =
$dom->find('div[id*="item-"][class="product"][data-in-stock="yes"]')->map(attr=>'id');
my $item_price = $dom->find('h3[itemprop="price"]')->map('text');
Both of these map method calls work (produce the intended result) but I'm
scratching my head how / why they work given the documentation on the
Mojo::Collection map <http://mojolicious.org/perldoc/Mojo/Collection#map>
method (and the source too). The first example sure looks like a hash
element being passed and the second example, a string, neither of which
appear to be a callback or method reference shown in the documentation.
Curiously, these map method calls look like the method signatures for the
Mojo::DOM attr <http://mojolicious.org/perldoc/Mojo/DOM#attr> method.
Instead of the map calls in the above, could / should I be using attr
method calls? But the result of a $dom->find() call is a Mojo::Collection
object... and attr is a Mojo::DOM method.
Clearly, I'm missing something fundamental here. Help?
And one more newbie question, is there a good reason (e.g., backward
compatibility with earlier releases) to use the Mojo::Collection to_array
<http://mojolicious.org/perldoc/Mojo/Collection#to_array> method to access
an element of a Mojo::Collection?
my $item_price =
$dom->find('h3[itemprop="price"]')->map('text')->to_array->[0];
versus
my $item_price = $dom->find('h3[itemprop="price"]')->map('text')->[0];
Thank you.
... Dewey
--
You received this message because you are subscribed to the Google Groups
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.