Gossamer Forum
Home : General : Perl Programming :

Regular Expressions

Quote Reply
Regular Expressions
I have a subroutine that looks like this:


sub LoadTemplate {
my %template_tags = (
path => "/var/www/cgi-bin"
);

my $filename = shift;
my $text;

local $/;
local *F;

open(F, "< $filename\0");
$text = <F>;
close(F);

$text =~ s{ %% ( .*? ) %% }
{ exists($template_tags->{$1})
? $template_tags->{$1}
: ""
}gsex;
&PrintHeaders;
print $text;

}


I call the sub in this fashion LoadTemplate("templates/tags.html");

Now, it loads the template and prints the template correctly except that it does not replace the occurance of %%path%% which is in the tags.html file. It just strips it and leaves it blank.

The template file looks like this:
<html>
<head>
<title>Tags Test Page</title>
</head>
<body>
<font size="3">This the path: %%path%%</font>

</body>
</html>


Any help would be greatly appreciated.

Later,
Paul

http://www.fullmoonshining.com for Pearl Jam Fans
Quote Reply
Re: Regular Expressions In reply to
%template_tags is a hash, not a reference to a hash.

Therefore $template_tags->{$1} doesn't exist. Remove the arrow.

$template_tages{$1}

should work for you.

Quote Reply
Re: Regular Expressions In reply to
Thanks. I didn't see that. Later, if I want to add variables to the hash would I have to reference them? Like this:
path => \$path


Thanks,
Paul


http://www.fullmoonshining.com for Pearl Jam Fans
Quote Reply
Re: Regular Expressions In reply to
No...

You should simply use:

path => $path

LIKE Links 2.0 and LINKS SQL.

Also, you should first define the path as follows:

my $path = "/var/www/cgi-bin/";

Also, if you want to define multiple values in the hash, then you should separate them by commas.

Regards,

Eliot Lee
Quote Reply
Re: Regular Expressions In reply to
Ok thanks. Yeah, I'm usually pretty good about declaring variables but I guess I slipped up this time. I knew about the commas, I was just testing it with one tag to see if it worked. I usually program in C++ and have only been playing with perl for just over a month. So I'm still trying learn the correct syntax.

Thanks Again,
Paul

http://www.fullmoonshining.com for Pearl Jam Fans
Quote Reply
Re: Regular Expressions In reply to
Funny you posted this. I recently wrote a template parser to use in my non-Links scripts. It's not quite as elegant as Alex's Template.pm but it's functional.

The file is here:
ftp://ftp.camelsoup.com/pub/parser/Parser.pm
An example script and template are here:
ftp://ftp.camelsoup.com/pub/parser/example/

--Drew
Free, hot camel soup for Links hackers...
http://www.camelsoup.com
Quote Reply
Re: Regular Expressions In reply to
HTML::Template (available at CPAN) is a really good one

Quote Reply
Re: Regular Expressions In reply to
Maybe it's just me, but I've looked through several parsers available from cpan and was not impressed. They all are way too complicated and have features I never intend to use... HTML::Template is like 3000 lines of deadweight.

--Drew
Free, hot camel soup for Links hackers...
http://www.camelsoup.com
Quote Reply
Re: Regular Expressions In reply to
Hi,

GT::Template is fast and very easy to use. Just call:

my $parsed = GT::Template->parse('filename.html', { var => $val, var2 => $val2 });

at it's simplest. It will return filename.html parsed with var and var2 tags replaced.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: Regular Expressions In reply to
Yeah, I just wanted a simple subroutine to parse a file because my program won't have that many templates. Now, I feel stupid because it was a simple error that I didn't see and then asked an even dumber question afterwords. Anyway, that's life and programming, the simple things mess with your mind.

By the way yours looks great junko. I think I remember you posting something about this awhile back, or something similar, but forgot about it.

Thanks,
Paul

http://www.fullmoonshining.com for Pearl Jam Fans
Quote Reply
Re: Regular Expressions In reply to
In Reply To:
By the way yours looks great junko.
Thanks Paul.
In Reply To:
I think I remember you posting something about this awhile back, or something similar
Yeah, I had a 10 liner just like yours that I posted once I think but I don't remember where.

--Drew
Free, hot camel soup for Links hackers...
http://www.camelsoup.com