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?
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.
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.
$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.
$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: []
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;