I have a set of scripts that I did not write in an Object Oriented manner (Bad programmer! No buiscit!). I'm trying to go back and rewrite them, as it appears that I'll be adding features, but I'm having some troubles.
Summary:
File: mylib.pl #This file defines two subroutines. sub My_Load_Func { ($file_to_load, $ID)=@_; #ID is the page id in a database, used by some of the embedded rou +tines. # If I could set ID as a variable in the mylib.pl object, I could +use # this slot in the templates to pass the object, but I haven't fig +ured out how #Function calls My_Define_Vars, passing it the $ID, then #Function now loads $file_to_load via Text::Template, # placing a "require /path/to/mylib.pl" in the PREPEND section # (Text::Template allows you to embed perl code, # but each embedded block is in a new namespace. # Text::Template also passes in the Vars defined in My_Define_Vars } sub My_Define_Vars { #This function sets several variables, #Some are static (such as RootPath, which is prepended to any paths +) #some are looked up in a database based on the passed ID. #Under the current system, I have several variants of mylib.pl, #Each with different variations on these variables. #This lets me use the same content pages (below) with different tem +plates and variables #to generate different pages (for example, test site vs live site, +text site vs graphical, etc) } file: Content.html # This is a sample content file. #I have several hundred of these, and don't have sole control over the +m # so any redesign of the system shouldn't modify how these look #Text::Template is set to look for <!! !!>tags <!!&My_Load_Func($Var_With_Header_Template_Path,$ID)!!> <!!$BeginText #depending on version of mylib.pl, can be blank or can b +e <TEXTAREA> wrapper for editing !!> HTML content here <!! $EndText!!> <!!&My_Load_Func($Var_With_Trailer_Template_Path, $ID)!!> file: Template #there are many templates. Each template can # call other templates through cascading &My_Load_Func calls. # Eventually results in HTML. file: Calling script #There are several calling scripts, each with a different purpose #THese scripts are little more than: #--Determining which file and ID to pass to an initial call to My_Load +_Func, #--which mylib.pl to use, #--what to do with the results.
My trouble is that the "new namespace" feature of Text::Template means that I can't just turn mylib.pl into a module with my script setting the vars in My_Define_Vars, because they won't survive in nested My_Load_Func calls. (Under the current model My_Define_Vars gets called for every new block of embedded perl)

Lastly, I know the current method is hideously inefficient, but that was not a design concern, as they deliver static informational pages. I use the scripts to build the flat HTML files that are served normally. The templating system is used to help with development. (Which it has succeeded at). Any advice on modifying this system to a more expandable, maintainable format appreciated.


In reply to Objectifying after the fact by swiftone

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.