|
NAMEpQuery - A port of jQuery.js to PerlVERSIONThis document describes pQuery version 0.24.SYNOPSISuse pQuery; pQuery("http://google.com/search?q=pquery") ->find("h2") ->each(sub { my $i = shift; print $i + 1, ") ", pQuery($_)->text, "\n"; }); DESCRIPTIONpQuery is a pragmatic attempt to port the jQuery JavaScript framework to Perl. It is pragmatic in the sense that it switches certain JavaScript idioms for Perl ones, in order to make the use of it concise. A primary goal of jQuery is to "Find things and do things, concisely". pQuery has the same goal.pQuery exports a single function called "pQuery". (Actually, it also exports the special "PQUERY" function. Read below.) This function acts a constructor and does different things depending on the arguments you give it. This is discussed in the "CONSTRUCTORS" section below. A pQuery object acts like an array reference (because, in fact, it is). Typically it is an array of pQuery::DOM elements, but it can be an array of anything. pQuery::DOM is roughly an attempt to duplicate JavaScript's DOM in Perl. It subclasses HTML::TreeBuilder/HTML::Element so there are a few differences to be aware of. See the "pQuery::DOM" documentation for details. Like jQuery, pQuery methods return a pQuery object; either the original object or a new derived object. All pQuery "METHODS" are described below. THE ROYAL PQUERYThe power of jQuery is that single method calls can apply to many DOM objects. pQuery does the exact same thing but can take this one step further. A single PQUERY object can contain several DOMs!Consider this example: > perl -MpQuery -le 'PQUERY(\ map "http://search.cpan.org/~$_/", qw(ingy gugod miyagawa))\ ->find("table")->eq(1)->find("tr")\ ->EACH(sub{\ printf("%40s - %s Perl distributions\n", $_->url, $_->length - 1)\ })' http://search.cpan.org/~ingy/ - 88 Perl distributions http://search.cpan.org/~gugod/ - 86 Perl distributions http://search.cpan.org/~miyagawa/ - 138 Perl distributions The power lies in "PQUERY", a special constructor that creates a wrapper object for many pQuery objects, and applies all methods called on it to all the pQuery objects it contains. CONSTRUCTORSThe pQuery constructor is an exported function called "pQuery". It does different things depending on the arguments you pass it.URLIf you pass pQuery a URL, it will attempt to get the page and use its HTML to create a pQuery::DOM object. The pQuery object will contain the top level pQuery::DOM object.pQuery("http://google.com"); It will also set the global variable $pQuery::document to the resulting DOM object. Future calls to pQuery methods will use this document if none other is supplied. HTMLIf you already have an HTML string, pass it to pQuery and it will create a pQuery::DOM object. The pQuery object will contain the top level pQuery::DOM object.pQuery("<p>Hello <b>world</b>.</p>"); FILEIf you pass pQuery a string that ends with .html and contains no whitespace, pQuery will assume it is the name of a file containing html and will read the contents and parse the HTML into a new DOM.pQuery("my/webpage.html"); Selector StringYou can create a pQuery object with a selector string just like in jQuery. The problem is that Perl doesn't have a global document object lying around like JavaScript does.One thing you can do is set the global variable, $pQuery::document, to a pQuery::DOM document. This will be used by future selectors. Another thing you can do is pass the document to select on as the second parameter. (jQuery also has this second, context parameter). pQuery("table.mygrid > td:eq(7)", $dom); pQuery ObjectYou can create a new pQuery object from another pQuery object. The new object will be a shallow copy.my $pquery2 = pQuery($pquery1); Array ReferenceYou can create a pQuery object as an array of anything you want; not just pQuery::DOM elements. This can be useful to use the "each" method to iterate over the array.pQuery(\ @some_array); No ArgumentsCalling pQuery with no arguments will return a pQuery object that is just an empty array reference. This is useful for using it to call class methods that don't need a DOM object.my $html = pQuery->get("http://google.com")->content; PQUERY(@list_of_pQuery_constructor_args)The PQUERY constructor takes a list of any of the above pQuery forms and creates a PQUERY object with one pQuery object per argument.METHODSThis is a reference of all the methods you can call on a pQuery object. They are almost entirely ported from jQuery.
UNDER CONSTRUCTIONThis module is still being written. The documented methods all work as documented (but may not be completed ports of their jQuery counterparts yet).The selector syntax is still very limited. (Single tags, IDs and classes only). Version 0.02 added the pQuery::DOM class which is a huge improvement, and should facilitate making the rest of the porting easy. But there is still much more code to port. Stay tuned... AUTHORIngy döt Net <ingy@cpan.org>COPYRIGHT AND LICENSECopyright 2008-2016. Ingy döt Net.This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See <http://www.perl.com/perl/misc/Artistic.html>
Visit the GSP FreeBSD Man Page Interface. |