homepage Welcome to WebmasterWorld Guest from 54.205.189.156
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Perl script query
dawlish




msg:3179845
 11:20 am on Dec 6, 2006 (gmt 0)

Hi,

I'm trying to make a modification to a perl script which would enable me to use a $include[filename.html] in a template file for including a header / footer etc.

Script below. Important line is

$formTemplate=~s/$include[([^]]+)]/qx(cat ""$1"")/ge;

$dateCreated=&getDate($id);
$formTemplate=~s/$include[([^]]+)]/qx(cat ""$1"")/ge;
$formTemplate=~s/(\$if[^\}]*\})/&getIf($1)/ge;
$formTemplate=~s/\$data\[([0-9]+)\]/$data[$1]/g;
$formTemplate=~s/\$escape\[([0-9]+)\]/$escape[$1]/g;
$formTemplate=~s/\$fields\[([0-9]+)\]/$fields[$1]/g;
$formTemplate=~s/\$id/$id/g;
$formTemplate=~s/\$datecreated/$dateCreated/g;
$formTemplate=~s/\$membername/$owner/g;

but the script fails with the following message in my error log

Unmatched ) in regex; marked by <-- HERE in m/[([^]]+) <-- HERE ]/ at data.pl line 259.

I'm a novice when it comes to perl so any help much appreciated. I'd also like to be able to include a url rather than just a file name. Would this be possible with the code above?

 

MichaelBluejay




msg:3179959
 1:17 pm on Dec 6, 2006 (gmt 0)

The error message is pretty clear: You have an unmatched ) symbol. You have to have the same number of open & close parentheses.

Now, I have no idea what the line in question is supposed to do, so I don't know how to balance it. Did you copy & paste it from somewhere, or did you write it yourself? If the latter, what exactly is it supposed to do? The =~ s/// syntax is for replacing some characters with other characters, but I'm a bit sleepy to try to interpret this particular use of it.

dawlish




msg:3180149
 4:00 pm on Dec 6, 2006 (gmt 0)

Thanks for the reply.

I copy and pasted the line which was recommended by someone else.

$formTemplate=~s/$include[([^]]+)]/qx(cat ""$1"")/ge;

However this is the line were the problem is with error:

Unmatched ) in regex; marked by <-- HERE in m/[([^]]+) <-- HERE ]/ at data.pl line 259.

Essentially, I have a content management system for my site which was developed by someone else as my programming skills are limited. I'm a designer.

The addition of the line of code is to enable me to use $include[whateverfilename.html] in the site templates. The effect would be to act like a server side include and enable me to use a header and footer html file. For instance where ever i entered $include[header.htm] in the website templates, the content of header.htm would be included.

I can see the error is suggesting I'm missing a )but can't work out how to fix it.

perl_diver




msg:3180422
 6:48 pm on Dec 6, 2006 (gmt 0)

this:

$include[([^]]+)]

should be:

$include[([^\]]+)]

the ']' is obviously a special character, it's the end of the character class. Adding the \ escapes it so perl treats it literally.

But shelling out to open the file qx(cat ""$1"") could probably be done in a better way using perls open() function. Using the shell is not efficient and certainly not portable. Write a sub rotuine that opens and prints the file or stores the file in a variable for printing later.

$formTemplate=~s/$include[([^\]]+)]/go_get($1)/ge;

sub go_get {
my $file = shift return 0;
open(FH,$file) or die "Can't open $file: $!";
print <FH>;
close FH;
}

but really you may want to look into HTML::Template or other templating system.

perl_diver




msg:3180428
 6:52 pm on Dec 6, 2006 (gmt 0)

I jumped the gun, this:

$formTemplate=~s/$include[([^]]+)]/qx(cat ""$1"")/ge;

should be:

$formTemplate=~s/$include\[([^\]]+)\]/go_get($1)/ge;

even better would be to change your delimiters from [] to something else, maybe:

$include%%filenme%%

s/$include%%([^%])%%/go_get($1)/ge;

this avoids interpolation by perl of the meta characters: []

dawlish




msg:3181265
 12:34 pm on Dec 7, 2006 (gmt 0)

Many thanks perl_diver. I've now got it to work.

Could you offer any advice as to how I can modify the script to include the output of a cgi script.

At the moment I can only include a file.

I want to include a banner rotate script at the top of my site.

Ideally use $include%%/cgi-bin/banner.cgi?type=plants%%

I know that I'll need use LWP::Simple; at the top of the script but don't know how to change

s/$include%%([^%])%%/go_get($1)/ge;

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved