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

I am seeking the wisdom of those who went before me *bows down*. Im working on a template engine for a specific website system. Here is the structure of how the system works
index.cgi  ~ Loader, Calls all modules, defines global variables
-->Calls decodeTemplate('templateName');
---->Looks in the template for a marker %content%
------->calls "modules/templateName.pl";
---------->This module needs to decode form content
-------------->Calls breakupForm("post");

Simpler put
index.cgi --> $template->decodeTemplate('templateName') --> calls require "modules/templateName.pl" &templateName --> calls %form = $forms->breakupForms('post') which returns "%form"; But for some reason the breakupForms module isnt reading the "STDIN" to breakup the form Any suggestions? This is driving me nuts,

Replies are listed 'Best First'.
Re: Module Problems
by ikegami (Patriarch) on Oct 23, 2004 at 18:10 UTC

    What do you by "isnt reading the STDIN"? Do you mean code reads from STDIN, but finds nothing there? I've seen people ask that when the form data was already read from STDIN at an earlier point in the program. The data is no longer there to be read.

      That might explain it. Then another question would be. I tried this
      our (%gform,%pform); gform is the Query Strings
                           pform is the Form Posts
      
      %pform = formBreaker->breakupForms('post');
      %gform = formBreaker->breakupForms('get'); 
      

      Now, using the same structure as above, when I get to the &templateName; routine. it doesnt have access to the original "our" decloration in the index.cgi file.
      I know it doesnt have access anymore because at that module, Strict is telling me the %pform/%gform requires explicit decleration.
      Is there any way to pass the %pform,%gform down the levels without actually manually passing them through?
        Is there any way to pass the %pform,%gform down the levels without actually manually passing them through?

        Yes and no. For example,

        package formBreaker; sub test { print($CallerPackage::var, $/); } package CallerPackage; our $var; $var = 'success'; formBreaker->test();

        You can, but as you can see, you have to either hardcode the caller's package (bad!) or you have to get the caller's package name using caller and dynamically build the variable name (ugly and bad!). What's wrong with
        %pform = formBreaker->breakupForms('post');
        or
        formBreaker->breakupForms('post', \%pform);
        ?

        A Reply to myself. I just realized that the routine breakupForms isn't returning a thing!
        Heres the code
        package formBreaker;
        
        use strict;
        sub breakupForms {
                my (%pform,%gform);
                my $submittype = @_;
                #Do the Form {Post} first
        
                read(STDIN, $buffer,$ENV{'CONTENT_LENGTH'});
                my @pairs = split(/&/, $buffer);
        
                foreach $pair (@pairs) {
                        my ($name,$value);
                        ($name, $value) = split(/=/, $pair);
                        $name =~ tr/+/ /;
                        $name =~ s/%(a-fA-F0-9a-fA-F0-9)/pack("C", hex($1))/eg;
        
                        $value =~ tr/+/ /;
                        $value =~ s/%(a-fA-F0-9a-fA-F0-9)/pack("C", hex($1))/eg;
        
                        if($pform{$name} ne "") {
                                $pform{$name} = join("\|",$pform{$name},$value);
                        }
                        else {
                                $pform{$name}=$value;
                        }
                }
        
                #breakup form query strings
                my @pairs = split(/\&/,$ENV{'QUERY_STRING'});
                        foreach(@pairs) {
                                my($name,$value);
                                ($name,$value) = split(/\=/,$_);
                                $gform{$name}=$value;
                        }
        
                if($submittype eq "post") {
                        return %pform;
                }
                elsif($submittype eq "get") {
                        return %gform;
                }
        } #end of sub for breakupForms
        1;
        
Re: Module Problems
by tilly (Archbishop) on Oct 24, 2004 at 01:46 UTC
    I presume that you're aware that other templating systems have been written. In fact so many that perrin saw the need to write a very good comparison.

    I suspect that no matter what you want out of a templating system, you can find one pre-built that does it very well already.