Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

HTML::Form Phenomena

by dReKurCe (Scribe)
on Mar 28, 2007 at 11:24 UTC ( [id://606981]=perlquestion: print w/replies, xml ) Need Help??

dReKurCe has asked for the wisdom of the Perl Monks concerning the following question:

Grettings Monks; When using two slightly different strategies :one that involves passing the result of a Form query into a sub and one outputing the result immediately. Here's the code and the results.

#! /usr/bin/perl use LWP; use HTTP::Request; use HTTP::Response; use HTML::Form; $con=new LWP::UserAgent; $con->agent('n3u7digit'); $request=new HTTP::Request(GET=>"http://www.<site>.org"); $reply=$con->request($request); @forms=HTML::Form->parse($reply); for $formh(@forms){@keys=keys %$formh; for $name(@keys){ print "\t---$name\n"; } }

....the second version ..



for $formh(@forms){@keys=values %$formh; for $name(@keys){ process($name); print "--$name--\n"; } + sub process{$return=shift;return $return;}

the output: CASE 1

---enctype ---action ---method ---attr ---inputs ---enctype ---action ---method ---attr ---inputs
CASE 2
--application/x-www-form-urlencoded-- --http:/<site>.org/-- --GET-- --HASH(0x84d3d4c)-- --ARRAY(0x85190e4)-- --application/x-www-form-urlencoded-- --http://<site>.org/en/video?destination=node%xxxxxx-- --POST--- --HASH(0x852232c)-- --ARRAY(0x852ac40)--
So the utility of a closure seems evident, but why?
#! /usr/bin/perl use LWP; use HTTP::Request; use HTTP::Response; use HTML::Form; $con=new LWP::UserAgent; $con->agent('n3u7digit'); $request=new HTTP::Request(GET=>"http://www.<site>.org"); $reply=$con->request($request); @forms=HTML::Form->parse($reply); for $formh(@forms){@keys=values %$formh; for $name(@keys){ process($name); print "--$name--\n"; } sub process{ $return=shift; return $return; }

Replies are listed 'Best First'.
Re: HTML::Form Phenomena
by eric256 (Parson) on Mar 28, 2007 at 13:37 UTC

    You are missing a closing } in your example, that would lead me to beleive that this isn't the code actualy producing that result....or just a cut and paste error, either way it would be good to add your } and enlose all your code and results in <code></code> tags.

    I am wondering if you are also misusing HTML::Form the documentation says to use $form->inputs() to get at the fields and your are accessing its hash directly. I would think that the behavour of objects when you don't follow their interface is undefined as you are seeing. I am willing to bet however that the output you are getting is the result of some sort of string overloading in the first case that you arn't getting in the second.


    ___________
    Eric Hodges
Re: HTML::Form Phenomena
by Anno (Deacon) on Mar 29, 2007 at 08:35 UTC
    In your first code example you are looping over the keys of a hash
    @keys=keys %$formh;
    In the second, you loop over the values
    @keys=values %$formh;
    Accordingly the output looks different. The call to process() in the second case does nothing and has nothing to do with the difference.

    It looks like you are running without strict and warnings. Switch them on, they're helpful.

    Anno

    Update: Added pointer to strict and warnings

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (7)
As of 2024-04-19 08:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found