Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 4th, 2001, 01:33 PM
sjbates sjbates is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2001
Location: England, UK
Posts: 41 sjbates User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
using templates in a perl script

Hi

Is there anyone who knows where i can find how to use templates in a script i am making as it would make editing the pages a lot easier.

Thanks.

Reply With Quote
  #2  
Old August 4th, 2001, 02:11 PM
Hero Zzyzzx's Avatar
Hero Zzyzzx Hero Zzyzzx is offline
11
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2001
Location: Lynn, MA
Posts: 4,635 Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 23 h 44 m 19 sec
Reputation Power: 77
Send a message via AIM to Hero Zzyzzx
Try doing a google search for "html perl template" and you'll find a bunch of great solutions.

My current personal favorite is HTML::Template, on CPAN, because of the simplicity of the syntax and the flexibility.

Reply With Quote
  #3  
Old August 4th, 2001, 02:15 PM
sjbates sjbates is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2001
Location: England, UK
Posts: 41 sjbates User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Hi.

OK thanks i'll go and have a look.

Thanks.

Reply With Quote
  #4  
Old August 6th, 2001, 09:17 AM
jdk's Avatar
jdk jdk is offline
phpkid ~~~~~~ :o)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Nov 2000
Location: NJ, USA
Posts: 2,535 jdk User rank is Lance Corporal (50 - 100 Reputation Level)jdk User rank is Lance Corporal (50 - 100 Reputation Level)jdk User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 11 m 11 sec
Reputation Power: 10
Send a message via Yahoo to jdk
hi,
i have created function myself to use template in perl script.
below is the code for it.
Code:
################################################################################
##### NAME : CreatePage
##### FUNCTION :::
##### Generates HTML pages to be displayed according to template.
##### ARGUMENTS :::::
##### First argument is Template File.
##### Second argument is the hash containing variable to be replaced.
##### Third argument specifies whether write HTML page to file or to STDOUT.
################################################################################

sub CreatePage {
	
   my ($templatefile,$r_namespace,$filename) = @_;
   my ($template);

  #OUTPUT FLUSH ON.
  $| = 1;

  # Open template and read in

open(TEMPLATE,$file) or HandleError("Can not open $file !!!",$!,"Make sure $file exits");
{
    local($/) = undef;
	#By setting system variable $/ you are reading whole file in $template, otherwise, it would read file data
	#separated by newline character.
    $template = <TEMPLATE>;

 }
 close(TEMPLATE);
  
$template =~ s/\$([A-Z]+)/$r_namespace->{$1}/g;

if($filename eq "")
{
	print $template;
}
#if filename is specified then write to file else write to std out.

else
  {
	  open (MYINDEXPAGE,">$filename") or HandleError("Can not open $filename for writing.",$!,"Make sure $filename can be written!!");
	  print MYINDEXPAGE $template;
	  close(MYINDEXPAGE);
  }
  return 1;

}


in ur template file u will have variables like $NAME,$SURNAME. etc. etc.
and u call the function like this,

Code:
		local %namespace = (
				CONTACTID => "SPECIAL",
				EMAIL => $opt_param,
				OPTEMAIL => "webmaster\@texsurat.com",
				SCRIPTURL => $scripturl,
				PERSONNAME => 'ITS SPECIAL',
				COMNAME => '' );
			if($header == 0)
			{
				print "Content type:text/html\n\n";
			}
			CreatePage("form2.htm",\%namespace,"");


here first arugment is the file name which acts as template and which has template variable in $TEMPVAR format.
here the temp var name must be capital and it should not have special characters.( i havent wrote best template parsing function !!! )
and yeah third argument that is a file name is optional , if specified, then template will be parsed, the generated data would be stored in that file.
normally it prints the template parsed o/p to standard o/p.

i wrote this coz the template parsing libraries are so complex to use ( atleast i find them complex )
wat i needed was simple solution and i came up with this.

if u r unclear about how to use it,then post back, i would tell u how to use it,
but i think if u spare some time, its fairly easy to get this code,
jd
__________________
_____________________________
d.k.jariwala (JD)
~ simple thought, simple act ~
I blog @ http://jdk.phpkid.org

Reply With Quote
  #5  
Old August 6th, 2001, 09:33 AM
Hero Zzyzzx's Avatar
Hero Zzyzzx Hero Zzyzzx is offline
11
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2001
Location: Lynn, MA
Posts: 4,635 Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 23 h 44 m 19 sec
Reputation Power: 77
Send a message via AIM to Hero Zzyzzx
Very nice, but

Code:
use Wheel;
my $wheel=Wheel->new();
$wheel->reinvent();


HTML::Template is very, very robust and has features to allow you to easily create templated tables and lists using loops, and supports memory caching of templates and pretty much everything else you could want a template to do. Plus it's at least as easy to figure out.

(Parts of this came from DCScripts, didn't it? (Isn't open-source great!) Some of the syntax looks very familiar, particularly the use of $r_namespace and the regex. . .)

Why make your own wheel when there is a lighter/faster one out there, for free? This is the essence of perl hacking- laziness.

Reply With Quote
  #6  
Old August 6th, 2001, 09:54 AM
jdk's Avatar
jdk jdk is offline
phpkid ~~~~~~ :o)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Nov 2000
Location: NJ, USA
Posts: 2,535 jdk User rank is Lance Corporal (50 - 100 Reputation Level)jdk User rank is Lance Corporal (50 - 100 Reputation Level)jdk User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 11 m 11 sec
Reputation Power: 10
Send a message via Yahoo to jdk
ok.

1. use Wheel;

warning :: i can not handle that wheel at line 1 , its too big.

so i created a small wheel that i can handle

see for u that wheel is easy to use, its fast and blah blah.
but it can not hold true for everyone.

i didnt find those template parsing libraries that easy to use it in my code.

Quote:
i wrote this coz the template parsing libraries are so complex to use ( atleast i find them complex )
wat i needed was simple solution and i came up with this.


hi hero, see if u have read my post, i have mentioned there that this isnt the best template parsing thing around.


i was need in simple stuff and i did it. and about DCscripts.. yeah may be it looks similar..but that coz i like to study others code and sometimes u can have same idea for doing something. and yeah now i looked at the code, its i think same. so should i now hanged till death ??

Quote:
Is there anyone who knows where i can find how to use templates in a script i am making as it would make editing the pages a lot easier.


come on man, he wanted to have some template parsing script,
and i think i can include myself in that 'anyone' and just like u i had some idea which i thought may be helpful to him,

so i posted,let me know if i have done something wrong
jd

Reply With Quote
  #7  
Old August 6th, 2001, 10:04 AM
Hero Zzyzzx's Avatar
Hero Zzyzzx Hero Zzyzzx is offline
11
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2001
Location: Lynn, MA
Posts: 4,635 Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 23 h 44 m 19 sec
Reputation Power: 77
Send a message via AIM to Hero Zzyzzx
No, no, no. I just recognized the thing. . . What's the saying?

"Creativity is 10% inspiration, 10 % perspiration, and 80% plagiarism."

Or something to that effect. We all build of each other's work. Otherwise, why bother? We might as well make your own programming languages.

On the wheel thing- It would just be shame to build an application around an imcomplete templating system. What if you got halfway through and realized you needed more flexibility?

Recoding it would be a real pain, and spending a few hours up front figuring out HTML::Template (or some other system) would really save some pain down the road.

Plus, it's always better to push new programmers towards using modules. Better code, better maintenance, and other benefits too numerous to mention.

If there's anything worth learning in perl, it's learning how to use modules.

Reply With Quote
  #8  
Old August 6th, 2001, 10:19 AM
jdk's Avatar
jdk jdk is offline
phpkid ~~~~~~ :o)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Nov 2000
Location: NJ, USA
Posts: 2,535 jdk User rank is Lance Corporal (50 - 100 Reputation Level)jdk User rank is Lance Corporal (50 - 100 Reputation Level)jdk User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 11 m 11 sec
Reputation Power: 10
Send a message via Yahoo to jdk
hm.

Quote:
On the wheel thing- It would just be shame to build an application around an imcomplete templating system. What if you got halfway through and realized you needed more flexibility?


hm..to this i would say that , before i do any work, i analyse what will be my future need and how should i go about coding. so i havent yet faced a situation where in the MIDDLE i feel like changing the code from top to bottom. wat i needed was simple thing and i did it simple way.

Quote:
Plus, it's always better to push new programmers towards using modules. Better code, better maintenance, and other benefits too numerous to mention.


yeah u already had ur suggetion first, then i thought ok, may be he would like to check this out,
anyways from now onwards, i would take care that i suggest that use modules for ur need.

but u know , sometimes, u just keep using those modules, but never know wats goin on behind that ?? u know sometimes finding solution urself is also a good thing.

anyways hero,
i dont think we need a BIG argument goin, rt ??
WE are here to share OUR views, arent we ?
and yeah i agree that wat u r saying is true.

but i m not wrong either,

so i think i would too advice sjbates to use modules,
now let him decide,
jd

Reply With Quote
  #9  
Old August 6th, 2001, 10:35 AM
Hero Zzyzzx's Avatar
Hero Zzyzzx Hero Zzyzzx is offline
11
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2001
Location: Lynn, MA
Posts: 4,635 Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 23 h 44 m 19 sec
Reputation Power: 77
Send a message via AIM to Hero Zzyzzx
I should probably be more measured in my responses. I wasn't trying to start a thing going. . .

BTW, I agree with all you said. I just think that when folks are learning perl (including myself) they take some advice too much to heart- folks use code without really understanding what it does. It's like dogma, and I think dogma is dangerous.

Telling a newbie not to use a module when they probably should can have long-term repercussions. I know this happened with me. I had to relearn how to code.

I'm completely guilty of this, but I think that giving folks complex code frequently does them a disservice, unless all they want to be is a script-kiddie, and I don't want to help folks that don't want to learn.

On the "halfway through" thing- this is probably more applicable in future maintenance- I've definitely had simple projects that evolved into more complex ones over time. Using a module allows one to only have to worry about some code, and not all of it.

My feeling here is that sjbates wants to learn, but doesn't quite know where to look yet. Hopefully they'll go the "right" route, whatever that is for what they are trying to do.

Reply With Quote
  #10  
Old August 6th, 2001, 11:40 AM
jdk's Avatar
jdk jdk is offline
phpkid ~~~~~~ :o)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Nov 2000
Location: NJ, USA
Posts: 2,535 jdk User rank is Lance Corporal (50 - 100 Reputation Level)jdk User rank is Lance Corporal (50 - 100 Reputation Level)jdk User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 11 m 11 sec
Reputation Power: 10
Send a message via Yahoo to jdk
Quote:
I should probably be more measured in my responses. I wasn't trying to start a thing going. . .


nor me !!
actually..i think we can have had a cooler talk about this, though i accept that i got annoyed and my wordings werent good. plz dont mind.


Quote:
BTW, I agree with all you said. I just think that when folks are learning perl (including myself) they take some advice too much to heart- folks use code without really understanding what it does. It's like dogma, and I think dogma is dangerous.


hm..may be that is true.
but if someone really wants to learn something, i think he would find his way around. our job is to give guide them according to our experience,
and it seems like ur advice is better !!

Quote:
Telling a newbie not to use a module when they probably should can have long-term repercussions. I know this happened with me. I had to relearn how to code.


man, did i ever said that dont use module ?? i dont think so.
anyways, actualy it was my mistake that i didnt put up a line which should say that go for module too. this is a work around.
infect previously someone has posted similar question in PHP forum and i told him to use Smarty, its a template parsing system like the
one u have mentioned. but then he asked that he is not able to work with smarty then i gave him the code snippet for similar thing in PHP.
so its like..up to the coder,i think some things u learn by time..
see u realised that modules are better ,now u r using that....
and yeah u would like to advice that to the ppl coming to the programming, but u know sometimes they wont get u.
once they find out theirselves they would be much clearer regarding how to go for some specific stuff.


anyways,
hero it was nice sharing views with u.
nice to know ur views,
jd

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > using templates in a perl script


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |