Click to See Complete Forum and Search --> : Perl to Java Script


rbeckmon
06-24-2003, 08:33 PM
Is it possible to write java script from perl script?

jeffmott
06-24-2003, 10:26 PM
All you need to do is output the JS just as you would HTML. e.g.,print q`<script type="text/javascript">
alert("Hello World!");
</script>`;

rbeckmon
06-25-2003, 03:47 AM
Thanks Jeff,
I'll see if I can do something with that.

rbeckmon
06-25-2003, 11:11 AM
I' m sorry Jeff,
You answered my question perfectly. I just didn't ask it properly. I wanted know if it were possible to write a java script .js file from perl? A file that could be used by multiple web pages.

Charles
06-25-2003, 11:18 AM
#!user/local/bin/perl
use CGI qw(header);
print header (-type=>'text/javascript'), <<EOQ;
Date.ONE_SECOND = 1000;
Date.ONE_MINUTE = Date.ONE_SECOND * 60;
Date.ONE_HOUR = Date.ONE_MINUTE * 60;

Date.prototype.toTimeString = function () {
var HOURS = ['', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve'];
var MINUTES = ['o\'clock', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty', 'twenty-one', 'twenty-two', 'twenty-three', 'twenty-four', 'twenty-five', 'twenty-six', 'twenty-seven', 'twenty-eight', 'twenty-nine', 'thirty', 'thirty-one', 'thirty-two', 'thirty-three', 'thirty-four', 'thirty-five', 'thirty-six', 'thirty-seven', 'thirty-eight', 'thirty-nine', 'forty', 'forty-one', 'forty-two', 'forty-three', 'forty-four', 'forty-five', 'forty-six', 'forty-seven', 'forty-eight', 'forty-nine', 'fifty', 'fifty-one', 'fifty-two', 'fifty-three', 'fifty-four', 'fifty-five', 'fifty-six', 'fifty-seven', 'fifty-eight', 'fifty-nine', 'sixty'];

return ['Current time: ', HOURS[this.getHours() > 12 ? this.getHours() - 12 : this.getHours()], MINUTES[this.getMinutes()]].join(' ')}
EOQ

DaiWelsh
06-25-2003, 11:20 AM
There should be no problem doing that, provided that your perl script has write permissions to the directory that the .js file resides in/needs to reside in.

You might want to be extra careful about parameters to the perl script as it is effectively generating new content for your site, but then we all do that always anyway right ;)

Depending on exactly what you are trying to achieve an alternative is to actually call the perl script as the external file for the javascript and generate the javascript on the fly. e.g.
<script language="JavaScript" src="myscript.pl">. This is good if you want to make the js dynamic, but will be heavier load on the server than your method of writing the js to disk, like I said it depends what you are trying to achieve....

HTH,

Dai

rbeckmon
06-29-2003, 11:34 AM
:confused:
Hi Charles,
Your method for writing Java.js files from perl works fine. There is only one problem. It actualy rewrites the pl file as java script. With this method I might as well just write the java script file. What I'm trying to do is write a java script .js file from a form that I can rewrite when I need to make changes. I have multiple pages that then read from this js file.

Thanks for all your input.
Thank you too DaiWelsh, your advice was very useful.

Charles
06-29-2003, 12:14 PM
My example was intended to be used to generate an HTTP response a la <script src="charlesFineScript.pl"></script>.