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

Is it possible to use Tk coding in cgi?

While running Tk alone, its working properly, but when i am using in cgi, I am not getting the output.

#!C:\perl\bin\perl use strict; use CGI; use DBI; use Tk; use Tk::HList; my $q = new CGI; print $q->header("text/html"), $q->start_html(-title=>"test"); print $q->p("hi"); my $dbh = DBI->connect("DBI:mysql:velusamy", {PrintError =>0, RaiseErr +or =>0}) ; #automatic error checking my $data = qq(select empno, name from testing order by name); my $sth = $dbh->prepare($data); $sth->execute; my ($sno, $name); my $mw = MainWindow->new; my $hlist = $mw->HList(-columns => 2, -header => 1) ->pack(-expand => 1, -fill => 'both'); $hlist->headerCreate(0, -text => "Emp-No"); $hlist->headerCreate(1, -text => "Emp-Name"); while ( ( $sno, $name) = $sth->fetchrow_array ) { $hlist->add($sno); $hlist->itemCreate($sno, 0, -text => uc($sno)); $hlist->itemCreate($sno, 1, -text => uc($name)); } MainLoop; $dbh->disconnect; print $q->end_html;

Give your suggestions.

Regards,

Samy

Replies are listed 'Best First'.
Re: TK in CGI
by davido (Cardinal) on Jun 25, 2005 at 06:49 UTC

    Tk is a windowing mechanism. CGI is an interface through which you can execute server-side code to dynamically output HTML that gets rendered client-side. The dynamic HTML output by a CGI script ends up being rendered in the client-side web browser. Web browsers know how to render HTML, Javascript, and with plugins they can handle things like flash. But they don't do Tk.

    Think about it this way. Your CGI script executes on the webserver. If you create a Tk object within a CGI script, it is created on the server side, not the client side. It's rather useless to be creating Tk windows on your apache webserver. ;)


    Dave

      Thank you very much for your reply.

Re: TK in CGI
by zentara (Cardinal) on Jun 25, 2005 at 10:36 UTC
    Davido's answer is right on, but just for the sake of rounding out your knowledge, there is an "attempt" to make a browser plugin, which would allow you to run Tk thru a browser, like a Java application. Look at PerlPlus Plugin. It dosn't work as it should just yet, but there is always hope for the future. :-)

    I'm not really a human, but I play one on earth. flash japh

      ...there is always hope for the future.

      For the sake of the future of web browsing, I hope not. ;)

      It's hard enough to stop popups now. Imagine giving unknown someone Tk-style control of your system.


      Dave

        Well, if it's done right, you should have a setting in the browser to turn the plug-in off, like java and javascript can be turned off. I think the bigger concern is security, and it would have to be addressed by making a complete, but functionally limited perl and Tk , to be the standalone plug-in. Just connecting to the system's Perl, is far too big of a risk to run thru cgi.

        I'm not really a human, but I play one on earth. flash japh
users have the last word
by chanio (Priest) on Jun 25, 2005 at 18:03 UTC
    Even if it were possible to send a complete application that would render the Tk as a reply, the user would be prompted if she wanted to install such plugin. I remember accepting some years ago such a Tk plugin but I don't remember where and if it was something in PHP. And it worked, at least for that purpose. But, what would have happened if I had to visit that site several times?

    I guess that some multimedia player is using the Tk technollogy, but I don't know if there is any way of using it as a tool to load Tk scripts from server-side.

    What I understand from your question is that your personal path has reached the time of learning a bit more about server-side applications: persistence, communications, modperl (?), etc. See the Tutorials

    Good luck! You are going to enjoy a lot that great side of perl. Then you should compare what you can do with perl and with other similar languages. You are going to conclude that perl is the language for the web.

    .{\('v')/}   C H E E R   U P !
     _`(___)' ___a_l_b_e_r_t_o_________
    
    Wherever I lay my KNOPPIX disk, a new FREE LINUX nation could be established.