Brethren,

I've recently been assisting a colleague with some legacy code at work, and I came across the attached function, which I think is attempt to rewrite a C idiom into Perl.

By way of context, this function takes as arguments the name of a template file and a reference to a hash. The content of the template file is a set of pseudo-HTML template definitions, which are parsed out by this function and stored in the hashref.

Templates follow the pattern:

<TEMPLATE NAME="TEMPLATE1"> # Processing instructions here ... </TEMPLATE> <TEMPLATE NAME="TEMPLATE2"> # Instructions for some other action ... </TEMPLATE> # etc

The exact form of the processing instructions is irrelevant, as they are simply copied into the hash (as we shall see).

The subroutine is shown below (with error checking and some irrelevant detail removed for clarity):

sub read_template { my ($templfile, $hashref) = @_; my ($line, $dest, $stuff); my $fh = new FileHandle("<$templfile"); # This looks like a C idiom. # $dest is initially set to point to $stuff (which is # undef). # # If a template start is found, the hash key is set with # the template name, and # $dest is re-pointed at the value corresponding to this # hash key. # # Subsequent lines are appended to $dest (and hence to # the hash value), # until a closing tag is found, at which point $dest is # re-set to $stuff again. # # The hash is a reference, so we are also modifying the # caller at the same time. Nothing is explicitly # returned. # $dest = \$stuff; while ($line = <$fh>) { if ($line =~ /^\s*<\s*TEMPLATE\s+NAME="(.*)"\s*>/i) { $hashref->{$1} = ''; $dest = \$hashref->{$1}; } elsif ($line =~ /^\s*<\s*\/\s*TEMPLATE\s*>/i) { $dest = \$stuff; } else { ${ $dest } .= $line; } } $fh->close; }

My questions are these:

  1. Is my interpretation of the working of the function (in the comments in the code fragment above) correct?
  2. Is there a more "Perlish" way of expressing this idiom (preferably without the nastiness of modifying the caller from within the function)?

In reply to C idioms in Perl by slife

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.