Click to See Complete Forum and Search --> : Printing Templates
druss
01-22-2003, 12:32 AM
my script now reads a html file and prints it, in other words i use a template.
however the problem is that the variables that i insert in the template gets printed with the variable name and perl does not replace it with its value.
EG:
in the template i have this tag inserted..
<FORM METHOD="POST" ACTION="$cryptic_url">
the script doesnt replace the variable in the template..
here is the script that i use to read and print the template:
------------------------------------------------------------
print "Content-type: text/html\n\n";
open(code,"$templates/cryptic_index.htm");
@code = <code>;
close(code);
foreach $slot (@code) {
print "$slot";
}
exit;
------------------------------------------------------------
Thanks
Goran
jeffmott
01-22-2003, 11:21 AM
This is because the variables you're talking about are in an HTML document, not a Perl script. And once it is read in the only thing Perl knows is that it is textual data. What you'll have to do is search that data for the string '$cryptic_url' and replace it with its value.
s/\$cryptic_url/$cryptic_url/g for @code;
druss
01-23-2003, 02:19 AM
Thanks. That helped me alot
however it doesnt seem to work with the @ variables
for some reason this dont work
s/\$alias[0]/$alias[0]/g for @code;
it is in a text field and normal variables such as $test have no problems, but the above does???
Thanks
Goran
jeffmott
01-23-2003, 08:41 AM
Square brackets [ ] are meta characters and need a backslash before them to be treated literally.
s/\$alias\[0\]/$alias[0]/g for @code;
druss
01-23-2003, 09:59 AM
perl is really complicated... you think you have got something right and the script should work perfectly but then a little problem always happens. If it werent for you jeffmot i would not even bother continuing because its so stressfull
anyways for some reason this function just dont work??
print "Location: $download\n\n";
$download is a entered link. it can be either a file or a website yet when i execute that command above, nothing happens?
Thanks
Goran
jeffmott
01-23-2003, 10:32 AM
Setting the location is a part of the header. Are you sure you havn't closed it yet (with \n\n), perhaps after printing the content-type?
Charles
01-23-2003, 10:49 AM
Originally posted by druss
perl is really complicated... you think you have got something right and the script should work perfectly but then a little problem always happens. If it werent for you jeffmot i would not even bother continuing because its so stressfull
anyways for some reason this function just dont work??
print "Location: $download\n\n";
$download is a entered link. it can be either a file or a website yet when i execute that command above, nothing happens?
Thanks
Goran Actually, I'm rather fond of Perl but like any programming or scripting language you cannot learn it by trial and error. You need to sit down and make a careful and complete study of the language. The dabbler has two things working against him or her. On the one hand, things often have unforseen side effects and on the other hand the dabbler may not know about the feature that is just right for the task. On another, long gone board someone once published a 56 line answer to a problem. I solved it in two lines.
One principal of good Perl scripting is to always use a module when one is available and I would suggest using the CGI.pm module here. But you will need to read the documentation. (http://theoryx5.uwinnipeg.ca/CPAN/data/perl/CGI.html)
druss
01-23-2003, 11:41 AM
hmm, that leaves me with a problem them...
this is the code i have:
------------------------------------------------------
sub promo {
print "Content-type: text/html\n\n";
open(code,"$templates/leech_promo.htm");
@code = <code>;
close(code);
print "@code";
print "Location: $download\n\n";
exit;
}
------------------------------------------------------
when i place the location on top it downloads but no page gets printed?
i guess perl does need some learning before you love the language, but then again im new to perl and the only thing thats keeping me here is the satisfaction when a script is completed, plus there are so many possibilities with perl, you practically can do anything.
Thanks Again
Goran
jeffmott
01-23-2003, 12:26 PM
Two a blank line separates the header from the body. For location to be a part of the header you must not create that blank line after you set the content-type, or omit content-type altogether. There also is no time delay with this kind of redirect, so nothing printed to the page will ever be seen.
Charles is correct that you should use a proven module when available. But in this case, I personally don't see the real benefit of loading a module and initiating a subroutine call to replace one line, a simple print statement. But in either case to perform the redirect with CGI.pm it would be:
use CGI;
my $cgi = CGI->new();
$cgi->redirect('absoluteURI');
Charles
On another, long gone board someone once published a 56 line answer to a problem. I solved it in two lines.
Although your solution was most likely better, less code does not always mean better performance.
Nedals
01-23-2003, 02:44 PM
Druss. Your last post got confussing...
sub promo {
print "Content-type: text/html\n\n";
open(code,"$templates/leech_promo.htm");
@code = <code>;
close(code);
print "@code";
print "Location: $download\n\n";
exit;
}
Either you want to send the @code data
OR
You want to send $download
.... but not both!!!
If you want to send the @code stuff, then use..
----------------------
sub promo {
print "Content-type: text/html\n\n";
open(code,"$templates/leech_promo.htm");
@code = <code>;
close(code);
# replace the variable with appropiate text
@code =~ s/\$HTMLvariable/$newvariable/g;
# You may have to do this line by line instead of the whole array.
print "@code";
exit;
}
------------------------
.. and if you simply want to send a different HTML doc, use...
-----------------------
print "Location: $download\n\n";
# not Content-type required
-----------------------
Good luck!
druss
01-23-2003, 08:39 PM
no but you see $download is not supposed to be added with the @code.
im doing an anti-leech script and all i want to do is when the anti-leech gets executed the file downloads directly and a promo page then loads.
is that possible or must i put the download link with the promo page?
thanks
goran
Nedals
01-23-2003, 08:56 PM
I don't know what an anti-leech script is but...
as far as I know you cannot do what you want. There may be a 'server push' technique but I'm not aware of any.
I would go with your suggestion to put the 'download' link on your promo page or possibly use a javascript onLoad() to get the 'download'.
But, there again, I'm not really sure what you are doing because, it would appear that, the download will replace your promo unless it is a non-html file that you want the user to save.!!
druss
01-23-2003, 09:21 PM
hmm ok,
well then is there a way to popup a page inside perl.
for example i run that location header and after that there is a popup?
in fact is there a way to popup a page in perl at all?? if there is id love to know it!
Thanks
Goran
Nedals
01-23-2003, 10:26 PM
well then is there a way to popup a page inside perl.
Perhaps I'm mis-understanding what you are doing. I don't know what you mean by a 'popup inside perl'. Perl cannot, as far as I know, create a popup window. You cannot, in the same script, send one page followed by a second page.
I think you mean you would like to display a page derived from a perl script and then display a popup window showing another page derived from a second perl script. This you can do.
Simply add something like the following...
onLoad = window.open('url to cgi',....);
to your first page and the popoup will appear after the page has loaded displaying the page from a second script.
Is that what you mean??
druss
01-23-2003, 10:59 PM
nah not really.
because i cannot control the page that loads the script i need to promote my site you see, here is an example.
->> user registers and configures ->> user adds link to cgi script on their website ->> script downloads the file that they configured ->> script shows promotion for the service.
However i need to do this without revealing the real url that they download, to stop any other site from downloading it!
and now the problem is that i cannot download the file and after show my promo page. so i figured if its possible to popup my page when the link is clicked without adding javascript or anything to the html, and then download the file on the normal page not the popup'ed one.
Nedals
01-23-2003, 11:56 PM
OK! I think.
Try this...
->>user registers and configures
save userid, random code, and other variables
->>user adds link to cgi script on their website
add link as filename.cgi?random code
->>script downloads the file that they configured
script gets random code ($ENV{'QUERY_STRING'} and uses that to insert variables via your template routine.
->>script shows promotion for the service.
... and that's where I get lost.
Of course, if someone gets hold of the random code they could display the page.
Anyway.. Just a thought!!
druss
01-24-2003, 06:57 AM
huh? ok im tripped out LOL
nevermind think ill try another way tehn
Thanks for your time anyways, really value it
Laterz
Goran