Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

How do you run a subroutine from inside HTML code?

by coldmiser (Hermit)
on Mar 15, 2006 at 16:09 UTC ( #536851=perlquestion: print w/replies, xml ) Need Help??

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

I have a small bit of code that I'm trying to run a subroutine from here is an example:
foreach $index (keys %{$_[0]}){ print "<TD WIDTH=\"$table_vars{$index}\">$index</TD>\n"; }
Yes, $index is the name of my subroutine. $index = &login
The problem is that the HTML just prints the name of the subroutine and doesn't actually run it.
Does anyone have any experience with how to run a subroutine from inside a table?

Replies are listed 'Best First'.
Re: How do you run a subroutine from inside HTML code?
by marto (Cardinal) on Mar 15, 2006 at 16:14 UTC
Re: How do you run a subroutine from inside HTML code?
by dorward (Curate) on Mar 15, 2006 at 16:21 UTC

    The short answer is that "You can't".

    A more accurate answer would mention the client side PerlScript in which case you would do something along the lines of:

    <script type="text/perlscript"> mySub </script>

    Note that I've never used PerlScript so that syntax could be wrong (since very few people have browsers that support PerlScript there isn't much point in using it on the WWW).

    The other answer is that you might have something that looks like HTML, but isn't really. Mason for example is a templating language which looks a lot like HTML but with a few extra bits to run Perl. In it <html> means "Output the string <html>". The end result is that you can drop Perl code into it so it looks like the HTML is executing the Perl.

    Personally, I'd use a templating language like Template-Toolkit rather than trying to mix Perl and HTML into a single file.

Re: How do you run a subroutine from inside HTML code?
by ikegami (Patriarch) on Mar 15, 2006 at 16:36 UTC
Re: How do you run a subroutine from inside HTML code?
by Tanktalus (Canon) on Mar 15, 2006 at 16:19 UTC

    I may be a bit dense, but I don't see a subroutine there... perhaps you could expand on what you mean a bit? Is $index supposed to be a subroutine?

      Yes, $index is the name of my subroutine. $index = &login
Re: How do you run a subroutine from inside HTML code?
by matija (Priest) on Mar 15, 2006 at 16:39 UTC
    Which is the name of the subroutine, the $index?

    At first I thought you might be needing someting like HTML::Template or TemplateToolkit - that is, if you wanted to fill a space in the table with the result of the subroutine.

    You'd call the subroutine as {$index}(), I think.

    On second thought, you may be trying to do some AJAX related stuff. In that case, I suggest you look at CGI::Ajax

Re: How do you run a subroutine from inside HTML code?
by coldmiser (Hermit) on Mar 15, 2006 at 17:03 UTC
    Thanks for all who helped, but I got it running. I had to edit the code to this:
    foreach $index (keys %{$_[0]}){ print "<TD WIDTH=\"$table_vars{$index}\">\n"; &$index(); print "</TD>\n"; }
    and I had to remove the & from $index so instead of $index = &login it is now $index = login

      I'd first think to try the following, but that may be perl6 interpolation talking:

      my $index = \&login; for (keys %{$_[0]}) { print qq[<TD WIDTH="$table_vars{$_}">\n$index->()\n</TD>\n]; }

      Generally, and definitely perl5, "@{[foo()]}" calls &foo as list context and interpolates the return value as with an array and "${\foo()]}" similarly, with scalar context and as a scalar.

      For your particular example, however, I'd just use printf.

      for (keys %{$_[0]}) { printf qq[<TD WIDTH="$table_vars{$_}"\n%s</TD>\n], $index->(); }

      There is no need to escape quoting delimeters in Perl.

      and I had to remove the & from $index so instead of $index = &login it is now $index = login

      Please always use strict; in your code.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2022-05-21 15:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (76 votes). Check out past polls.

    Notices?