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

hi monks,

i writing 10 web pages for a project, each of them would be having menus and some common code.

i am generating all this web pages using Perl so can't I put menu bar code in separate file, and call it whenever required.

i don't to use any framework, like catalyst, dancer, etc, because i want to know about web development from low level.

or should i write a simple module of myself as shown at http://www.perlmonks.org/?node_id=102347

Replies are listed 'Best First'.
Re: calling menu bar creating code from another cgi/pl code
by DanielSpaniel (Scribe) on Aug 27, 2013 at 01:21 UTC

    Yes, you could put the menu-bar code in a separate file and call it whenever required; you could also read it from a database if you wanted to.

    If you're not already familiar with the idea, then you may also find it useful to read a little about Server Side Includes (SSI). I don't know what type of Web server you are using, but I think the principle is common to most servers.

    Apologies if I'm telling you something you already know, but, in a nutshell, SSI allows you to put special tags into your html pages, and the server will execute the commands; some may be as simple as printing the date, but (so long as your server is configured for it) you can also execute Perl scripts, or whatever, which might then produce a menu, or footer, etc. It's a very common thing to do.

    There are lots of web sites out there with information about Server Side Includes, but I've provided a couple below:

    http://www.crucialp.com/resources/tutorials/web-programming/server-side-includes-ssi-tutorial.php

    http://httpd.apache.org/docs/current/howto/ssi.html

      thanks daniel,

      i had little idea about SSI, but some where i read, it produces some security issues, so never went that way, but it seems, if used properly, it could provide, some useful informations and functions, for which one does not have to write long statement.

      is there some kind of system at perlmonk, to mark questions, as solved, or to accept reply, as answer. i had been searching for such links, but could not find one

Re: calling menu bar creating code from another cgi/pl code (mojo)
by Anonymous Monk on Aug 27, 2013 at 02:12 UTC

    i am generating all this web pages using Perl so can't I put menu bar code in separate file, and call it whenever required.

    Sure you can :) you can do the same with frobnits.cgi/pagename ... Path::Dispatcher, CGI::Application::Dispatch

    example :) run it (maybe  perl moannoy.pl daemon -l http://localhost:80/ ), click the links in your browser ....

    #!/usr/bin/perl -- use Mojolicious::Lite; get '/' => { template => 'index', title => time };;; app->start; __DATA__ @@ index.html.ep <html> <head> <title><%= title =%></title> </head> <body> <h1><%= title =%></h1> <button onclick="annoying()"> hi </button> <a href="/annoying.js"> /annoying.js </a> %= javascript '/annoying.js' </body> </html> @@ annoying.js function annoying() { alert('hi'); }

    or should i write a simple module

    You should write that regardless -- doesn't have to be a real module, but each page should be a function DebugCGI

      thanks anonymous monk

      i was actually looking some thing like as follows

      print $cgi->start_html(); # using ssi to call another code print '<!--#include virtual="/cgi-bin/menu.pl" -->'; # some similar cgi method to call above mentioned # perl / html file print $cgi->p("hello world"); print $cgi->end_html();

      i am currently against using any framework, because my production scale is not that much large, i am not well aware with mvc development pattern, and yes i am new to perl cgi.

      For portability (link relativity/portability) use url_for
      - <a href="/annoying.js"> /annoying.js </a> + %= link_to( ( "/annoying.js" ) x 2 )

      That is  link_to( "/annoying.js"  => "/annoying.js"  ), its perl after all :)