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


I am reading some lines (as below) via perl script from a text file.





Now my requirement is to have HTML page/template which should display the options read from perl script as a hyperlink.

I need Output as below on web page as below:

OptionsDisplay ABC DEF GHI JKL

Let's say if I get four options in my text file then 4 hyperlink should be displayed, If I get 8 options in text then 8 hyperlinks will be there on page.

Otherwise a default template is used if I am not reading the file in my perlcode

How do I use/design the HTML template according to this purpose. Please advice on this or provide a sample refrence to achieve this.

Replies are listed 'Best First'.
Re: Perl HTML template design scenarios
by Your Mother (Archbishop) on May 16, 2016 at 14:46 UTC
    #!/usr/bin/env perl use strictures; use Text::Xslate; use Data::Section::Simple; my $vpath = Data::Section::Simple->new()->get_data_section(); my $tx = Text::Xslate->new( path => [ $vpath ] ); my @moos = join("", "A".."L") =~ /\w\w\w/g; print $tx->render("base.tx", { moos => rand(1) > .5 ? \@moos : [] }); __DATA__ @@ base.tx <!doctype html> <html lang="kitteh"> <head> <title>OHAI</tile> </head> <body> <h1>MOOS</h1> : if ! $moos.0 { <blockquote>OHNOES</blockquote> : } : for $moos -> $moo { <p><: $moo :></p> : } </body> </html>

    PM may not be a code writing service but it is a Your Mother amusement parlor at times. I am not recommending that code. Just messing around. :P See also: Text::Xslate, Template::Toolkit, Template::Alloy, Mojolicious, HTML::Template, et cetera.

Re: Perl HTML template design scenarios
by QuillMeantTen (Friar) on May 16, 2016 at 11:43 UTC

    Update: what is written I gathered from experience building a simple phishing website (for my internship) that would use a text file as a database, the techniques used to give it crud abilities seems to apply to the case here Greetings, fellow monk
    There are many ways to skin a (cat|fish),
    I gather you want the webpage to be served so, if you are not too much constrained resource wise, I'd advise using a web framework (catalyst,dancer or mojolicious).

    This seems to be the exact use case for a custom model, you'd call your script inside it, then pass it to the controller which will, in turn, give it to an html view (look up TT) where with a little bit of scripting (a loop with your standard hyperlink template and a part where you'll put the context hash and the needed key to retrieve your value, ABC,DEF and so on).

    That's how I'd go about it but keep in mind I never did any web programming in perl without a framework so there is a bias.
    Another argument I have for catalyst is the easy portability and the ability to use

    perl Makefile.PL; make installdeps;
    To get everything you need on the new machine.

    Also I'm not pushing my own fishing agenda here so if anyone else can propose a more lightweight way to go about it please do so, I'd be most interested especially if you give details about the implementations and reasons why it's better!
    Update2, in hindsight the whole process might look complicated but believe me, learning a framework is never a waste of time

Re: Perl HTML template design scenarios
by GotToBTru (Prior) on May 16, 2016 at 11:57 UTC

    What do you have so far? It's easier to suggest improvements or solutions when we have some code to look at. We're not a code writing service.

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)