Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Perl / October 2003



Tip: Looking for answers? Try searching our database.

Using <> on non-filehandles

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nicholas Thornton - 21 Oct 2003 06:51 GMT
I'm working on a template-reading module and I have a
question. I'm using a regex to pluck out the
references to data and replace them with the data
itself, and the template file can be very long. If I
were reading straight from the file and interpolating
I could use a good ol' while(<>){} loop to keep the
segments being regexed short (i.e. to the length of
one line). But since I only want to read through the
template once and store it in memory,
while(<$template>){} doesn't work for obvious reasons.

So I was wondering, is there any good equivalent I can
use so that i don't have to slurp the whole file up at
once to regex?

~wren
David Wheeler - 21 Oct 2003 07:04 GMT
> So I was wondering, is there any good equivalent I can
> use so that i don't have to slurp the whole file up at
> once to regex?

Store the template in an array instead of a scalar?

my @template = <>;

for (@template) {...}

HTH,

David

Signature

David Wheeler                                     AIM: dwTheory
david@kineticode.com                              ICQ: 15726394
http://www.kineticode.com/                     Yahoo!: dew7e
                                               Jabber: Theory@jabber.org
Kineticode. Setting knowledge in motion.[sm]

Thilo Planz - 21 Oct 2003 08:02 GMT
> I'm working on a template-reading module and I have a
> question.

Are you sure you need to do that ?
Templating modules are already extremely over-implemented and you
should be able to find a suitable one on CPAN.

Here are some articles about the topic:

http://twiki.med.yale.edu/twiki2/bin/view/CGIapp/CompareTemplates
http://perl.apache.org/docs/tutorials/tmpl/comparison/comparison.html

While they a concerned mainly with using templating engines for CGI
applications, they give a good overview of general purpose templating
solutions.

> I'm using a regex to pluck out the
> references to data and replace them with the data
> itself, and the template file can be very long.

If all you want to do is replace references to data with the data
(strings ?),
you should take a look at HTML::Template (which in spite of its name
can be used for any kind of text, not just HTML)

Using this module, you can read (and parse) your template once, after
which it will be in main memory in a very efficient format.
You can then reuse it as often as you want.

The only situation where HTML::Template might be a little inconvenient
is when you do not know the names of the parameters (the references to
your data) beforehand.
Do you?

Seriously, try to use a CPAN templating module,

Thilo
Nicholas Thornton - 21 Oct 2003 08:25 GMT
--- David Wheeler <david@wheeler.net> wrote:
> Store the template in an array instead of a
> scalar?

d'oh! Yeh that should work, shoulda thought of it. The
sub that reads the template returns a hash (so you can
have sections to the template like header, footer,
paragraph,...), but turning it into a hash of arrays
shouldn't be too hard.

thanks,
~wren
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.