in reply to Refactoring webcode to use templates

For myself, I have found dividing my project into separate files really is helpful in organizing everything. In place of my real filenames, I've done a slight modification, but you can see the gist of it here:

require "${directory}MyCGI_HTML.pl"; require "${directory}MyCGI_HTML_Main.pl"; require "${directory}MyCGI_HTML_Preferences.pl"; require "${directory}MyCGI_MySQL.pl"; require "${directory}MyCGI_LANG.pl"; require "${directory}MyCGI_Other.pl"; require "${directory}MyCGI_DB-Export.pl"; require "${directory}MyCGI_FindReplace.pl"; require "${directory}MyCGI_SEARCH.pl"; require "${directory}MyCGI_AJAX.pl"; require "${directory}MyCGI_Valid.pl"; require "${directory}MyCGI_SpellCheck.pl";

Naturally, the $directory variable holds the location of these files, which does not happen to be the /cgi-bin/ directory. It's a cinch to do a bit of versioning by simply copying the files to a new directory, updating them there, then changing this directory variable in the main CGI script.

As you can see, the files each have their specific purpose. In the "MySQL" one, I put subroutines with all of the MySQL queries and operations that need to be done--at least, most of them. In the "ExportDB" one, several specialized routines for making database exports available are to be found, and it's a sufficiently unique subset of MySQL operations to merit its own file.

These, of course, are only the Perl files. I also have, with similar nomenclature, separate .js and .css files. As I am working with AJAX, I put the Perl AJAX routines in a separate Perl file from the JavaScript AJAX functions, which would be in a .js file. It makes it very easy to find things, and works well for me.

I don't use any framework like Dancer, etc. I do all of my own programming from scratch. I've found it's much less troublesome for me to troubleshoot my own code than to be dabbling with someone else's code. I don't even use jQuery--it's just too esoteric for me, and bloated as well. My own functions take a fraction of the bandwidth that jQuery-driven functions would.

Two of the files, from my perspective, could be further subdivided to make them less unwieldy--the HTML and the MySQL. Both have a lot in them. Perl code tends to be compact compared to some of the non-Perl routines.

At the top of each of the required Perl files, I might have something like this:

use utf8; use CGI qw(-utf8); binmode(STDOUT, ":utf8"); 1; sub exportdatabase { #INCOMING GLOBAL VARS: $statement, $db_export_file, # $cur_date, $cur_time, $OS, $table, $quest fork: { #...truncated.

Notice that the file then just has the subroutines in it (below the 1;), and attaches to the main script as if it were part of it, with all of the global variables shared, etc. If all of these subroutines were combined into the same file, it would work just the same. By dividing things, it just helps to be more organized. (I'm not even sure if I need the "use" clauses at the top...but have put them there for good measure, particularly when the file contains UTF-8 characters.)

I embed variables within the main HTML template--which is itself put into a variable that is the return value of the subroutine. A sample might look like this:

my $template = <<TEMPLATE; <!DOCTYPE html> <HTML lang="utf8"> <head> <title>My Page Title</title> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf8"> $dragcss <!-- # S T A R T O F J A V A S C R I P T S H E R E --> $positionscript $AJAXJS <link rel="stylesheet" href="$cssfile"/> <link rel="stylesheet" href="$css_skin" title="Choice"/> </head> <body> <DIV class="fullpage" > ... Put the main content here. </DIV> </body> </html> TEMPLATE

Hope this gives you a few ideas.

Blessings,

~Polyglot~

Replies are listed 'Best First'.
Re^2: Refactoring webcode to use templates
by hippo (Archbishop) on Feb 01, 2021 at 15:31 UTC
    require "${directory}MyCGI_HTML.pl"; require "${directory}MyCGI_HTML_Main.pl"; require "${directory}MyCGI_HTML_Preferences.pl"; require "${directory}MyCGI_MySQL.pl"; require "${directory}MyCGI_LANG.pl"; require "${directory}MyCGI_Other.pl"; require "${directory}MyCGI_DB-Export.pl"; require "${directory}MyCGI_FindReplace.pl"; require "${directory}MyCGI_SEARCH.pl"; require "${directory}MyCGI_AJAX.pl"; require "${directory}MyCGI_Valid.pl"; require "${directory}MyCGI_SpellCheck.pl";

    Have you considered structuring your files as modules instead?


    🦛

      Have you considered structuring your files as modules instead?

      This is exactly the sort of thing that I am moving away from...lots of require files. Thanks to the good influence of the Monastery, I am now using modules for new code. This whole refactoring project will revolve around rewriting require as use modules...