SlideShare ist ein Scribd-Unternehmen logo
1 von 28
Downloaden Sie, um offline zu lesen
Einf¨hrung
                           u
                 Mojo::UserAgent
        HTML/JSON-Verarbeitung
          auf der Kommandozeile




                   Mojo::UserAgent
parallele HTTP-Anfragen, Kommandozeilen-Verwendung


                           Uwe V¨lker
                                o

                              XING AG


                           05.03.2012




                      Uwe V¨lker
                           o        Mojo::UserAgent
Einf¨hrung
                                    u
                          Mojo::UserAgent
                 HTML/JSON-Verarbeitung
                   auf der Kommandozeile




1   Einf¨hrung
        u

2   Mojo::UserAgent

3   HTML/JSON-Verarbeitung

4   auf der Kommandozeile




                               Uwe V¨lker
                                    o        Mojo::UserAgent
Einf¨hrung
                                  u
                                           HTTP-Requests in Perl?
                        Mojo::UserAgent
                                           parallele HTTP-Requests
               HTML/JSON-Verarbeitung
                                           Mojo::UserAgent
                 auf der Kommandozeile




1   Einf¨hrung
        u
      HTTP-Requests in Perl?
      parallele HTTP-Requests
      Mojo::UserAgent

2   Mojo::UserAgent

3   HTML/JSON-Verarbeitung

4   auf der Kommandozeile




                             Uwe V¨lker
                                  o        Mojo::UserAgent
Einf¨hrung
                                u
                                         HTTP-Requests in Perl?
                      Mojo::UserAgent
                                         parallele HTTP-Requests
             HTML/JSON-Verarbeitung
                                         Mojo::UserAgent
               auf der Kommandozeile


HTTP-Requests in Perl?




     LWP::UserAgent
     LWP::Simple




                           Uwe V¨lker
                                o        Mojo::UserAgent
Einf¨hrung
                                 u
                                          HTTP-Requests in Perl?
                       Mojo::UserAgent
                                          parallele HTTP-Requests
              HTML/JSON-Verarbeitung
                                          Mojo::UserAgent
                auf der Kommandozeile


HTTP-Requests in Perl?




     LWP::UserAgent
     LWP::Simple
     HTTP::Lite
     HTTP::Client (nur GET!)




                            Uwe V¨lker
                                 o        Mojo::UserAgent
Einf¨hrung
                                  u
                                           HTTP-Requests in Perl?
                        Mojo::UserAgent
                                           parallele HTTP-Requests
               HTML/JSON-Verarbeitung
                                           Mojo::UserAgent
                 auf der Kommandozeile


parallele HTTP-Requests




     LWP::Parallel::UserAgent (sehr alt - 2004)
     HTTP::Async
     AnyEvent::HTTP, POE::Component::Client::HTTP
     WWW::Curl::Multi




                             Uwe V¨lker
                                  o        Mojo::UserAgent
Einf¨hrung
                                     u
                                              HTTP-Requests in Perl?
                           Mojo::UserAgent
                                              parallele HTTP-Requests
                  HTML/JSON-Verarbeitung
                                              Mojo::UserAgent
                    auf der Kommandozeile


Mojo::UserAgent



  Wer sich nicht daran st¨rt, Mojolicious zu installieren, bekommt
                         o
  einen m¨chtigen HTTP-Klienten:
         a
      asynchron
      HTTP 1.1
      WebSocket-Unterst¨tzung
                       u




                                Uwe V¨lker
                                     o        Mojo::UserAgent
Einf¨hrung
                                    u        ¨
                                             Uberblick
                          Mojo::UserAgent
                                             non-blocking
                 HTML/JSON-Verarbeitung
                                             parallele Anfragen
                   auf der Kommandozeile




1   Einf¨hrung
        u

2   Mojo::UserAgent
     ¨
     Uberblick
     non-blocking
     parallele Anfragen

3   HTML/JSON-Verarbeitung

4   auf der Kommandozeile




                               Uwe V¨lker
                                    o        Mojo::UserAgent
Einf¨hrung
                                         u        ¨
                                                  Uberblick
                               Mojo::UserAgent
                                                  non-blocking
                      HTML/JSON-Verarbeitung
                                                  parallele Anfragen
                        auf der Kommandozeile

¨
Uberblick


  #! / u s r / b i n / p e r l

  use s t r i c t ;
  use w a r n i n g s ;

  use Mojo : : U s e r A g e n t ;
  my $ua=Mojo : : UserAgent −>new ( m a x r e d i r e c t s =>5);

  my $ t x=$ua−>g e t ( ’ h t t p : / /www. p e r l w o r k s h o p . de / ’ ) ;
  p r i n t $tx −>r e s −>body ;



                                    Uwe V¨lker
                                         o        Mojo::UserAgent
Einf¨hrung
                                 u        ¨
                                          Uberblick
                       Mojo::UserAgent
                                          non-blocking
              HTML/JSON-Verarbeitung
                                          parallele Anfragen
                auf der Kommandozeile


HTTP-Methoden




    get
    post, post form
    put
    delete
    head




                            Uwe V¨lker
                                 o        Mojo::UserAgent
Einf¨hrung
                                   u        ¨
                                            Uberblick
                         Mojo::UserAgent
                                            non-blocking
                HTML/JSON-Verarbeitung
                                            parallele Anfragen
                  auf der Kommandozeile


transaction-Objekt




      req - request-Objekt
      res - response-Objekt




                              Uwe V¨lker
                                   o        Mojo::UserAgent
Einf¨hrung
                                   u        ¨
                                            Uberblick
                         Mojo::UserAgent
                                            non-blocking
                HTML/JSON-Verarbeitung
                                            parallele Anfragen
                  auf der Kommandozeile


transaction-Objekt




      req - request-Objekt
      res - response-Objekt
      local address, local port
      remote address, remote port




                              Uwe V¨lker
                                   o        Mojo::UserAgent
Einf¨hrung
                                     u        ¨
                                              Uberblick
                           Mojo::UserAgent
                                              non-blocking
                  HTML/JSON-Verarbeitung
                                              parallele Anfragen
                    auf der Kommandozeile


non-blocking




  $ua−>g e t ( ’ h t t p : / / p e r l . o r g ’ => sub {
    my ( $ua , $ t x ) = @ ;
    p r i n t $tx −>r e s −>body ;
    Mojo : : IOLoop−>s t o p ;
  });
  Mojo : : IOLoop−>s t a r t ;




                                Uwe V¨lker
                                     o        Mojo::UserAgent
Einf¨hrung
                                      u        ¨
                                               Uberblick
                            Mojo::UserAgent
                                               non-blocking
                   HTML/JSON-Verarbeitung
                                               parallele Anfragen
                     auf der Kommandozeile


Mojo::IOLoop




     minimalistic IO loop
     building block for non-blocking TCP clients and servers
     start, stop




                                 Uwe V¨lker
                                      o        Mojo::UserAgent
Einf¨hrung
                                      u        ¨
                                               Uberblick
                            Mojo::UserAgent
                                               non-blocking
                   HTML/JSON-Verarbeitung
                                               parallele Anfragen
                     auf der Kommandozeile


parallele Anfragen


  my $ d e l a y = Mojo : : IOLoop−>d e l a y ;
  f o r e a c h my $ u r l ( ’ m o j o l i c i o . u s ’ , ’ cpan . o r g ’ ) {
      $ d e l a y −>b e g i n ;
      $ua−>g e t ( $ u r l => sub {
          my ( $ua , $ t x ) = @ ;
          $ d e l a y −>end ( $tx −>r e s −>dom−>a t ( ’ t i t l e ’)−> t e x t ) ;
      });
  }
  my @ t i t l e s = $ d e l a y −>wait ;




                                 Uwe V¨lker
                                      o        Mojo::UserAgent
Einf¨hrung
                                  u        ¨
                                           Uberblick
                        Mojo::UserAgent
                                           non-blocking
               HTML/JSON-Verarbeitung
                                           parallele Anfragen
                 auf der Kommandozeile


Mojo::IOLoop::Delay




     synchronize events
     begin - increase event counter
     end - decrease event counter (and set return values)
     wait - wait for ”finish” event (and return results)




                             Uwe V¨lker
                                  o        Mojo::UserAgent
Einf¨hrung
                                    u
                          Mojo::UserAgent    Mojo::DOM
                 HTML/JSON-Verarbeitung      Mojo::JSON::Pointer
                   auf der Kommandozeile




1   Einf¨hrung
        u

2   Mojo::UserAgent

3   HTML/JSON-Verarbeitung
     Mojo::DOM
     Mojo::JSON::Pointer

4   auf der Kommandozeile




                               Uwe V¨lker
                                    o        Mojo::UserAgent
Einf¨hrung
                                u
                      Mojo::UserAgent    Mojo::DOM
             HTML/JSON-Verarbeitung      Mojo::JSON::Pointer
               auf der Kommandozeile


Mojo::DOM




    HTML5/XML DOM-Parser
    klingt wie HTML::TreeBuilder?




                           Uwe V¨lker
                                o        Mojo::UserAgent
Einf¨hrung
                                u
                      Mojo::UserAgent    Mojo::DOM
             HTML/JSON-Verarbeitung      Mojo::JSON::Pointer
               auf der Kommandozeile


Mojo::DOM




    HTML5/XML DOM-Parser
    klingt wie HTML::TreeBuilder?
    Unterst¨tzung fuer CSS3-Selektoren
           u
    mehr wie HTML::TreeBuilder::XPath




                           Uwe V¨lker
                                o        Mojo::UserAgent
Einf¨hrung
                                     u
                           Mojo::UserAgent    Mojo::DOM
                  HTML/JSON-Verarbeitung      Mojo::JSON::Pointer
                    auf der Kommandozeile


Mojo::DOM


  use Mojo : :DOM;

 my $dom = Mojo : : DOM −>new (
   ’<d i v i d =”a”>A</d i v ><d i v i d =”b”>B</d i v > ’ ) ;

  p r i n t $dom−>a t ( ’#b ’)−> t e x t ;

 $dom−>d i v −>[1]−> r e p l a c e c o n t e n t ( ’ f o o ’ ) ;

 p r i n t $dom ;
 # <d i v i d =”a”>A</d i v ><d i v i d =”b”>foo </d i v >



                                Uwe V¨lker
                                     o        Mojo::UserAgent
Einf¨hrung
                                 u
                       Mojo::UserAgent    Mojo::DOM
              HTML/JSON-Verarbeitung      Mojo::JSON::Pointer
                auf der Kommandozeile


Mojo::DOM




    new/parse - DOM aufbauen
    Suche: at, find
    Bewegung: root, children, parent, div, p (Tags)
    Extraktion: attr, text
    Manipulation: append, prepend, replace




                            Uwe V¨lker
                                 o        Mojo::UserAgent
Einf¨hrung
                                      u
                            Mojo::UserAgent    Mojo::DOM
                   HTML/JSON-Verarbeitung      Mojo::JSON::Pointer
                     auf der Kommandozeile


Mojo::JSON::Pointer


  use Mojo : : JSON : : P o i n t e r ;

  my $p = Mojo : : JSON : : P o i n t e r −>new ;

  s a y $p−>g e t ( { f o o => [ 2 3 , ’ b a r ’ ] } , ’ / f o o /1 ’ ) ;

  i f ( $p−>c o n t a i n s ( { f o o => [ 2 3 , ’ b a r ’ ] } , ’ / f o o ’ ) ) {
     s a y ’ C o n t a i n s ”/ f o o ” . ’ ;
  }




                                 Uwe V¨lker
                                      o        Mojo::UserAgent
Einf¨hrung
                                  u
                        Mojo::UserAgent    Mojo::DOM
               HTML/JSON-Verarbeitung      Mojo::JSON::Pointer
                 auf der Kommandozeile


Mojo::JSON::Pointer




     http://tools.ietf.org/html/
     draft-pbryan-zyp-json-pointer-02
     get - extract parts of the data structure
     contains - check if a sub structure is present




                             Uwe V¨lker
                                  o        Mojo::UserAgent
Einf¨hrung
                                    u
                                             mojo get
                          Mojo::UserAgent
                                             CSS-Selektoren
                 HTML/JSON-Verarbeitung
                                             JSON-Pointer
                   auf der Kommandozeile




1   Einf¨hrung
        u

2   Mojo::UserAgent

3   HTML/JSON-Verarbeitung

4   auf der Kommandozeile
      mojo get
      CSS-Selektoren
      JSON-Pointer




                               Uwe V¨lker
                                    o        Mojo::UserAgent
Einf¨hrung
                                   u
                                            mojo get
                         Mojo::UserAgent
                                            CSS-Selektoren
                HTML/JSON-Verarbeitung
                                            JSON-Pointer
                  auf der Kommandozeile


mojo get




     mojo get -r perl.org
     mojo get -M POST -c body example.org
     mojo get -r perl.org ’#short lists .list p a’ 0
     mojo get http://search.twitter.com/search.json /error




                              Uwe V¨lker
                                   o        Mojo::UserAgent
Einf¨hrung
                                  u
                                           mojo get
                        Mojo::UserAgent
                                           CSS-Selektoren
               HTML/JSON-Verarbeitung
                                           JSON-Pointer
                 auf der Kommandozeile


CSS-Selektoren




     zusaetzliche Parameter nach der URL
     Selektor(en)
     Kommandos (text, all, attr, id)




                             Uwe V¨lker
                                  o        Mojo::UserAgent
Einf¨hrung
                                      u
                                               mojo get
                            Mojo::UserAgent
                                               CSS-Selektoren
                   HTML/JSON-Verarbeitung
                                               JSON-Pointer
                     auf der Kommandozeile


JSON-Pointer




     Content-Type muss JSON sein
     / ... / ...
     Hash-Key oder Array-Index




                                 Uwe V¨lker
                                      o        Mojo::UserAgent
Einf¨hrung
                                  u
                                           mojo get
                        Mojo::UserAgent
                                           CSS-Selektoren
               HTML/JSON-Verarbeitung
                                           JSON-Pointer
                 auf der Kommandozeile


Mojo::UserAgent




     Fragen?
     http://groups.google.com/group/mojolicious
     #mojo on irc.perl.org
     Einfach ausprobieren!




                             Uwe V¨lker
                                  o        Mojo::UserAgent

Weitere ähnliche Inhalte

Empfohlen

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Empfohlen (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

Mojo::UserAgent

  • 1. Einf¨hrung u Mojo::UserAgent HTML/JSON-Verarbeitung auf der Kommandozeile Mojo::UserAgent parallele HTTP-Anfragen, Kommandozeilen-Verwendung Uwe V¨lker o XING AG 05.03.2012 Uwe V¨lker o Mojo::UserAgent
  • 2. Einf¨hrung u Mojo::UserAgent HTML/JSON-Verarbeitung auf der Kommandozeile 1 Einf¨hrung u 2 Mojo::UserAgent 3 HTML/JSON-Verarbeitung 4 auf der Kommandozeile Uwe V¨lker o Mojo::UserAgent
  • 3. Einf¨hrung u HTTP-Requests in Perl? Mojo::UserAgent parallele HTTP-Requests HTML/JSON-Verarbeitung Mojo::UserAgent auf der Kommandozeile 1 Einf¨hrung u HTTP-Requests in Perl? parallele HTTP-Requests Mojo::UserAgent 2 Mojo::UserAgent 3 HTML/JSON-Verarbeitung 4 auf der Kommandozeile Uwe V¨lker o Mojo::UserAgent
  • 4. Einf¨hrung u HTTP-Requests in Perl? Mojo::UserAgent parallele HTTP-Requests HTML/JSON-Verarbeitung Mojo::UserAgent auf der Kommandozeile HTTP-Requests in Perl? LWP::UserAgent LWP::Simple Uwe V¨lker o Mojo::UserAgent
  • 5. Einf¨hrung u HTTP-Requests in Perl? Mojo::UserAgent parallele HTTP-Requests HTML/JSON-Verarbeitung Mojo::UserAgent auf der Kommandozeile HTTP-Requests in Perl? LWP::UserAgent LWP::Simple HTTP::Lite HTTP::Client (nur GET!) Uwe V¨lker o Mojo::UserAgent
  • 6. Einf¨hrung u HTTP-Requests in Perl? Mojo::UserAgent parallele HTTP-Requests HTML/JSON-Verarbeitung Mojo::UserAgent auf der Kommandozeile parallele HTTP-Requests LWP::Parallel::UserAgent (sehr alt - 2004) HTTP::Async AnyEvent::HTTP, POE::Component::Client::HTTP WWW::Curl::Multi Uwe V¨lker o Mojo::UserAgent
  • 7. Einf¨hrung u HTTP-Requests in Perl? Mojo::UserAgent parallele HTTP-Requests HTML/JSON-Verarbeitung Mojo::UserAgent auf der Kommandozeile Mojo::UserAgent Wer sich nicht daran st¨rt, Mojolicious zu installieren, bekommt o einen m¨chtigen HTTP-Klienten: a asynchron HTTP 1.1 WebSocket-Unterst¨tzung u Uwe V¨lker o Mojo::UserAgent
  • 8. Einf¨hrung u ¨ Uberblick Mojo::UserAgent non-blocking HTML/JSON-Verarbeitung parallele Anfragen auf der Kommandozeile 1 Einf¨hrung u 2 Mojo::UserAgent ¨ Uberblick non-blocking parallele Anfragen 3 HTML/JSON-Verarbeitung 4 auf der Kommandozeile Uwe V¨lker o Mojo::UserAgent
  • 9. Einf¨hrung u ¨ Uberblick Mojo::UserAgent non-blocking HTML/JSON-Verarbeitung parallele Anfragen auf der Kommandozeile ¨ Uberblick #! / u s r / b i n / p e r l use s t r i c t ; use w a r n i n g s ; use Mojo : : U s e r A g e n t ; my $ua=Mojo : : UserAgent −>new ( m a x r e d i r e c t s =>5); my $ t x=$ua−>g e t ( ’ h t t p : / /www. p e r l w o r k s h o p . de / ’ ) ; p r i n t $tx −>r e s −>body ; Uwe V¨lker o Mojo::UserAgent
  • 10. Einf¨hrung u ¨ Uberblick Mojo::UserAgent non-blocking HTML/JSON-Verarbeitung parallele Anfragen auf der Kommandozeile HTTP-Methoden get post, post form put delete head Uwe V¨lker o Mojo::UserAgent
  • 11. Einf¨hrung u ¨ Uberblick Mojo::UserAgent non-blocking HTML/JSON-Verarbeitung parallele Anfragen auf der Kommandozeile transaction-Objekt req - request-Objekt res - response-Objekt Uwe V¨lker o Mojo::UserAgent
  • 12. Einf¨hrung u ¨ Uberblick Mojo::UserAgent non-blocking HTML/JSON-Verarbeitung parallele Anfragen auf der Kommandozeile transaction-Objekt req - request-Objekt res - response-Objekt local address, local port remote address, remote port Uwe V¨lker o Mojo::UserAgent
  • 13. Einf¨hrung u ¨ Uberblick Mojo::UserAgent non-blocking HTML/JSON-Verarbeitung parallele Anfragen auf der Kommandozeile non-blocking $ua−>g e t ( ’ h t t p : / / p e r l . o r g ’ => sub { my ( $ua , $ t x ) = @ ; p r i n t $tx −>r e s −>body ; Mojo : : IOLoop−>s t o p ; }); Mojo : : IOLoop−>s t a r t ; Uwe V¨lker o Mojo::UserAgent
  • 14. Einf¨hrung u ¨ Uberblick Mojo::UserAgent non-blocking HTML/JSON-Verarbeitung parallele Anfragen auf der Kommandozeile Mojo::IOLoop minimalistic IO loop building block for non-blocking TCP clients and servers start, stop Uwe V¨lker o Mojo::UserAgent
  • 15. Einf¨hrung u ¨ Uberblick Mojo::UserAgent non-blocking HTML/JSON-Verarbeitung parallele Anfragen auf der Kommandozeile parallele Anfragen my $ d e l a y = Mojo : : IOLoop−>d e l a y ; f o r e a c h my $ u r l ( ’ m o j o l i c i o . u s ’ , ’ cpan . o r g ’ ) { $ d e l a y −>b e g i n ; $ua−>g e t ( $ u r l => sub { my ( $ua , $ t x ) = @ ; $ d e l a y −>end ( $tx −>r e s −>dom−>a t ( ’ t i t l e ’)−> t e x t ) ; }); } my @ t i t l e s = $ d e l a y −>wait ; Uwe V¨lker o Mojo::UserAgent
  • 16. Einf¨hrung u ¨ Uberblick Mojo::UserAgent non-blocking HTML/JSON-Verarbeitung parallele Anfragen auf der Kommandozeile Mojo::IOLoop::Delay synchronize events begin - increase event counter end - decrease event counter (and set return values) wait - wait for ”finish” event (and return results) Uwe V¨lker o Mojo::UserAgent
  • 17. Einf¨hrung u Mojo::UserAgent Mojo::DOM HTML/JSON-Verarbeitung Mojo::JSON::Pointer auf der Kommandozeile 1 Einf¨hrung u 2 Mojo::UserAgent 3 HTML/JSON-Verarbeitung Mojo::DOM Mojo::JSON::Pointer 4 auf der Kommandozeile Uwe V¨lker o Mojo::UserAgent
  • 18. Einf¨hrung u Mojo::UserAgent Mojo::DOM HTML/JSON-Verarbeitung Mojo::JSON::Pointer auf der Kommandozeile Mojo::DOM HTML5/XML DOM-Parser klingt wie HTML::TreeBuilder? Uwe V¨lker o Mojo::UserAgent
  • 19. Einf¨hrung u Mojo::UserAgent Mojo::DOM HTML/JSON-Verarbeitung Mojo::JSON::Pointer auf der Kommandozeile Mojo::DOM HTML5/XML DOM-Parser klingt wie HTML::TreeBuilder? Unterst¨tzung fuer CSS3-Selektoren u mehr wie HTML::TreeBuilder::XPath Uwe V¨lker o Mojo::UserAgent
  • 20. Einf¨hrung u Mojo::UserAgent Mojo::DOM HTML/JSON-Verarbeitung Mojo::JSON::Pointer auf der Kommandozeile Mojo::DOM use Mojo : :DOM; my $dom = Mojo : : DOM −>new ( ’<d i v i d =”a”>A</d i v ><d i v i d =”b”>B</d i v > ’ ) ; p r i n t $dom−>a t ( ’#b ’)−> t e x t ; $dom−>d i v −>[1]−> r e p l a c e c o n t e n t ( ’ f o o ’ ) ; p r i n t $dom ; # <d i v i d =”a”>A</d i v ><d i v i d =”b”>foo </d i v > Uwe V¨lker o Mojo::UserAgent
  • 21. Einf¨hrung u Mojo::UserAgent Mojo::DOM HTML/JSON-Verarbeitung Mojo::JSON::Pointer auf der Kommandozeile Mojo::DOM new/parse - DOM aufbauen Suche: at, find Bewegung: root, children, parent, div, p (Tags) Extraktion: attr, text Manipulation: append, prepend, replace Uwe V¨lker o Mojo::UserAgent
  • 22. Einf¨hrung u Mojo::UserAgent Mojo::DOM HTML/JSON-Verarbeitung Mojo::JSON::Pointer auf der Kommandozeile Mojo::JSON::Pointer use Mojo : : JSON : : P o i n t e r ; my $p = Mojo : : JSON : : P o i n t e r −>new ; s a y $p−>g e t ( { f o o => [ 2 3 , ’ b a r ’ ] } , ’ / f o o /1 ’ ) ; i f ( $p−>c o n t a i n s ( { f o o => [ 2 3 , ’ b a r ’ ] } , ’ / f o o ’ ) ) { s a y ’ C o n t a i n s ”/ f o o ” . ’ ; } Uwe V¨lker o Mojo::UserAgent
  • 23. Einf¨hrung u Mojo::UserAgent Mojo::DOM HTML/JSON-Verarbeitung Mojo::JSON::Pointer auf der Kommandozeile Mojo::JSON::Pointer http://tools.ietf.org/html/ draft-pbryan-zyp-json-pointer-02 get - extract parts of the data structure contains - check if a sub structure is present Uwe V¨lker o Mojo::UserAgent
  • 24. Einf¨hrung u mojo get Mojo::UserAgent CSS-Selektoren HTML/JSON-Verarbeitung JSON-Pointer auf der Kommandozeile 1 Einf¨hrung u 2 Mojo::UserAgent 3 HTML/JSON-Verarbeitung 4 auf der Kommandozeile mojo get CSS-Selektoren JSON-Pointer Uwe V¨lker o Mojo::UserAgent
  • 25. Einf¨hrung u mojo get Mojo::UserAgent CSS-Selektoren HTML/JSON-Verarbeitung JSON-Pointer auf der Kommandozeile mojo get mojo get -r perl.org mojo get -M POST -c body example.org mojo get -r perl.org ’#short lists .list p a’ 0 mojo get http://search.twitter.com/search.json /error Uwe V¨lker o Mojo::UserAgent
  • 26. Einf¨hrung u mojo get Mojo::UserAgent CSS-Selektoren HTML/JSON-Verarbeitung JSON-Pointer auf der Kommandozeile CSS-Selektoren zusaetzliche Parameter nach der URL Selektor(en) Kommandos (text, all, attr, id) Uwe V¨lker o Mojo::UserAgent
  • 27. Einf¨hrung u mojo get Mojo::UserAgent CSS-Selektoren HTML/JSON-Verarbeitung JSON-Pointer auf der Kommandozeile JSON-Pointer Content-Type muss JSON sein / ... / ... Hash-Key oder Array-Index Uwe V¨lker o Mojo::UserAgent
  • 28. Einf¨hrung u mojo get Mojo::UserAgent CSS-Selektoren HTML/JSON-Verarbeitung JSON-Pointer auf der Kommandozeile Mojo::UserAgent Fragen? http://groups.google.com/group/mojolicious #mojo on irc.perl.org Einfach ausprobieren! Uwe V¨lker o Mojo::UserAgent