Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Dynamic Language questions

by philcrow (Priest)
on May 14, 2007 at 13:11 UTC ( [id://615295]=note: print w/replies, xml ) Need Help??


in reply to Dynamic Language questions

For number one, you might also point out that there are many live sites on the web generate something like the example, including this site and slashdot. I would also point out that we have templating systems that make redesigns easy.

For number three, we use a Perl framework for tasks like that. There are several of those, ours is http://usegantry.org available from cpan as Gantry. Catalyst is another fine choice. Use of a framework means the code samples would be quite involved. If you are writing a little self contained script, you should probably use Data::FormValidator for the form validation.

For number two, I have a small document style SOAP client to run from the command line. It is self contained, except for using LWP::UserAgent for actual tcp traffic. I just dumps its results, but you could combine its approach with the answer to Number 1 above to render the result in pretty form.

use strict; use warnings; use LWP::UserAgent; my $f_temp = shift || 68; my $url = 'http://localhost:8080/GantrySoapService/f2c'; my $site = { action_url => $url, post_to_url => $url, target_namespace => 'http://sunflower.com/soapns', }; my $request_args = [ { temperature => [ { farenheit => $f_temp }, ] }, ]; my $request_xml = form_xml( $site, $request_args, 1 ); warn "request:\n$request_xml\n"; transact_via_xml( $site, $request_xml ); sub form_xml { my $size = shift; my $request_args = shift; my $pretty = shift; my $args = build_args( $request_args, $pretty ); return <<"EO_XML"; <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="$site->{ target_namespace }" xmlns:xs="http://www.w3.org/2001/XMLSchema" > <soap:Body> $args </soap:Body> </soap:Envelope> EO_XML } sub build_args { # recursive my $request_args = shift; my $pretty = shift; my $depth = shift || 0; my $retval = ''; return $request_args unless ref( $request_args ) eq 'ARRAY'; # pretty printed for debugging: my $indent = ( $pretty ) ? ' ' x ( 2 * $depth ) : ''; my $nl = ( $pretty ) ? "\n" : ''; foreach my $arg ( @{ $request_args } ) { my ( $key, $values ) = %{ $arg }; if ( $values ) { my $start_tag = "$indent<tns:$key>"; my $child_output = build_args( $values, $pretty, $depth + +1 ); my $end_tag = "</tns:$key>$nl"; # now pretty print it pretty please if ( ref( $values ) eq 'ARRAY' ) { $start_tag = "$start_tag$nl"; $end_tag = "$indent$end_tag"; } $retval .= "$start_tag$child_output$end_tag"; } else { # values is undef or the empty string $retval .= "$indent<tns:$key/>$nl"; } } return $retval; } sub transact_via_xml { my ( $site, $request_xml ) = @_; # make the request my $user_agent = LWP::UserAgent->new(); $user_agent->agent( 'Sunflower/1.0' ); my $request = HTTP::Request->new( POST => $site->{ post_to_url } ); $request->content_type( 'text/xml; charset=utf-8' ); $request->content_length( length $request_xml ); $request->header( 'Host' => $site->{host} ); $request->header( 'SoapAction' => $site->{ action_url } ); $request->content( $request_xml ); my $response = $user_agent->request( $request ); warn $response->content . "\n"; }

Phil

The Gantry Web Framework Book is now available.

Replies are listed 'Best First'.
Re^2: Dynamic Language questions
by Dice (Beadle) on May 14, 2007 at 14:30 UTC
    Thanks a lot for the code sample!

    Regarding example 1, yes, I can point to plenty of Perl sites that do what they're asking for in the wireframe example. But, while I can hand them the Slash sourcecode, it would be overkill, boggle their brains, and make them think "it would be so much easier to do this in PHP." What would be useful here is a *minimal* example, much like you suggest for example #3.

    Regarding example #3, yes, I used Data::FormValidator, HTML::Template and HTML::FillInForm. (My own code is available for download from the links talexb provided in his original post.) I contemplated CGI::Application for a minute but discarded it as being overkill for the application at hand. But who knows, maybe HTML::Embperl would be the right way to go in this situation. I haven't coded like that in a long time but it might give a useful point of comparison to the other languages in the survey.

    Cheers,
    Richard

Re^2: Dynamic Language questions
by Dice (Beadle) on May 14, 2007 at 19:49 UTC
    Phil,

    Could you describe to me the web service this accesses and what it does with it? Maybe show me an text-capture of a user interactive session with it?

    Thanks,
    Richard

      Here is the output from running the above script:
      perl soapclient 98.6
      Produces request:
      <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://sunflower.com/soapns" xmlns:xs="http://www.w3.org/2001/XMLSchema" > <soap:Body> <tns:temperature> <tns:farenheit>98.6</tns:farenheit> </tns:temperature> </soap:Body> </soap:Envelope>
      When the server has done its bit, the script prints the response:
      <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema" > <soap:Body> <GantrySoapServiceResponse xmlns="http://usegantry.org/soapservice"> <currentUTCTime>2007-05-15T12:08:18Z</currentUTCTime> <celcius>37</celcius> </GantrySoapServiceResponse> </soap:Body> </soap:Envelope>
      The names of the parameters and their allowed types are usually governed by a WSDL file (perhaps that expands to Web Service Description Language, but I'm not sure of the achronymn).

      Note well that you can convert the script to contact any document style service on the web by changing the URLs and providing the correct argument structure. For instance, I started this test against a public web service. I used:

      my $site = { action_url => 'http://in2test.lsi.uniovi.es/sqlmutationws/getMutantsCompress +ed', post_to_url => 'http://in2test.lsi.uniovi.es/sqlmutationws/SQLMutationWS.asmx +', target_namespace => 'http://in2test.lsi.uniovi.es/sqlmutationws', };
      and
      my $request_args = [ { getMutantsCompressed => [ { sql => 'select * from table1' }, { schema => undef }, { options => undef }, ] }, ];
      If you make those changes, and the public service is still up, you should get a result from anywhere on the net.

      In case it might help with this question or some other question you didn't post, here is the code from the Gantry samples which serves the original script:

      If you have Gantry installed, that's a complete server module. All you need to do is mount it with mod_perl or some other server approach.

      Thanks for taking the time to respond to the survey for all of us.

      Phil

      The Gantry Web Framework Book is now available.
        Your support (and everyone else's!) makes all the difference. Thanks for helping out.

        Cheers,
        Richard

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://615295]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-19 15:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found