Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Ajax/perl/javascript in perl

by realmonk (Initiate)
on Apr 26, 2010 at 06:52 UTC ( [id://836843]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks, I am new to Ajax and perl (ie. CGI::Ajax) I have my sub routine running sucessfully however, I need this to be ran multipule times without realoading the entire page. I have tried using javascript as followed.
#!/usr/bin/perl use CGI; use CGI::Carp qw( fatalsToBrowser ); use CGI::Ajax; my $cgi = new CGI; my $ajax = new CGI::Ajax('report' => \&report); print $ajax->build_html($cgi, \&main); sub main { $html<<HTML_MAIN; <html> <body> <input type="button" id="test" value="submit" onclick="setInterval +('report([\'args__real\',\'args__monk\'],[\'report\'])', 5000)" <div id="report"></div> </body> </html> HTML_MAIN return $html; } sub report { $input1=shift; $input2=shift; $result=$input1.$input2; return $result; }
This is not working any ideas how to rerun my function every 5 seconds?? Please note that
onclick="report(['args__real','args__monk'],['report'])"
will print out the correct values. P.S. I am running this on tomcat" please ask for clarification! Thanks, realmonk

Replies are listed 'Best First'.
Re: Ajax/perl/javascript in perl
by derby (Abbot) on Apr 26, 2010 at 12:02 UTC

    realmonk please forgive me in advance, I'm not ranting on you (but it may feel that way) but ... CGI::Ajax and other modules like it are *dangerous* IMHO. No, they're not going to blow anything up or cause catastrophic failure but they are going to prevent you from not only learning another language, they also obfuscate the natural boundary between server-side and client-side coding.

    I think every perl dev should learn Javascript ... JQuery. Then learn a good templating module (HTML::Template, Template::Toolkit, etc.). Putting JS and HTML in your perl code is *not* 'peanut butter in my chocolate' nor is it 'red wine with chicken' ... it's more like 'driving and drinking' -- two really fun activities that should never be combined.

    -derby
      I have to agree with derby. I started my AJAX experiments with CGI::Ajax, but quickly discovered that it actually hurt more than it helped in most cases. I personally use CGI::Application for my webapps, and for AJAX code I just make new runmodes for AJAX queries and then use one of the excellent functions provided with jQuery.

      ++ here too.

Re: Ajax in perl
by Corion (Patriarch) on Apr 26, 2010 at 06:59 UTC

    How does it fail for you? Is it a Javascript error? Is it a Perl error? What does the Javascript Error Console say? What does your webserver error log say?

    Please post a short (20 lines) script that uses CGI::Ajax and the corresponding HTML that demonstrates the problem. My guess is that you are not exporting the Perl function.

      The function works find with a plan onclick="perlfunction('string','string','results')" problem happends when i am using the setInterval function in javascript. I am not good in javascript and don't have a debugger or editor. This is causing me to find the errors myself. I belive webserver (tomcat) doesn't show the error on the client side (javascript).

        You haven't posted any code, so how are we expected to reproduce your problem?

        If you are "not good in Javascript", maybe you should learn that? This sounds a bit like "I'm not good at swimming but I like to win this Triathlon.

        The Javascript Error Console is available in every Javascript-capable browser, at least in Firefox and likely also in Internet Explorer. You will need to learn lots more about Javascript if you plan on developping an application that makes heavy use of Javascript.

        Please read the Writeup Formatting Tips to make your code render and download nicely, thanks!

Re: Ajax/perl/javascript in perl
by Anonymous Monk on Apr 26, 2010 at 13:25 UTC

    Has nobody mentioned use strict; use warnings; yet?

    Use them!
    They would help find your typos, and they'll tell you about all kinds of problems before your code runs off into the weeds.

      Sheesh! Is that all you can point out.

      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-18 10:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found