Giving Javascript code access to Perl variables

Giving Javascript code access to Perl variables

am 16.10.2005 02:14:04 von J Wermont

In a .CGI file, I'd like to include some Javascript code (which will be
written to the output file sent to the client's browser), which will
access Perl variables that were set in the same CGI file.

It seems to work in some cases. For example, I have a CGI file that
includes this code:

#!/usr/local/bin/perl

... Perl code ...

$loopcount = 4;

... more Perl code ...

print << "ENDOFPAGE";

... HTML code ...



... more HTML ...

ENDOFPAGE


When this page is displayed in the browser, you see:

Hello world
Hello world
Hello world
Hello world

So clearly the Javascript code recognizes the Perl variable $loopcount.

Can you do this with Perl array values, too? So far I haven't had any
success. Can anyone recommend a good site (or book) that addresses this
issue?

Here's my reason: I'm using Javascript to validate an HTML form, and
I want it to be done on the client side. This works fine for checking
invalid types of input (eg, numbers where letters should go, etc), but
I also would like to make sure that what is being typed into a text
input field is not a duplicate of something I already have stored in
a database.

Since I can access the database from my Perl script and store the values
in a Perl array variable, I'm thinking I could then download the array
to the client, so that the Javascript code can compare typed input with
values in the array. Since I was able to download the value "4" in the
Perl variable $loopcount, shouldn't I be able to download an array of
values to the client?

(By the way, this is a *very tiny* array of database values. If it
involved a large amount of data, I would check for duplicates on the
server, after the form was submitted, since I imagine the time it
would take to download all that data to the browser would undermine
any performance savings you get by doing client-side checking.)

Hope this makes sense. Maybe I should post this to a Javascript group
as well, but I'm hoping that some of you might be familiar with both
languages and can advise me.

Thanks!

J. Wermont


--
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe: http://lists.mysql.com/perl?unsub=gcdmp-msql-mysql-modules@m .gmane.org

Re: Giving Javascript code access to Perl variables

am 16.10.2005 18:20:43 von Tony Dean

The simplest approach, as I see it, is just to interrupt the print of the JS
code at the point you wish to make the declaration.

my (@array) = ('now','is','the','time','for','all','good','men');
my (@new, $tmp, $val);

for $val (@array) { push @new, "'$val'";} # Ensure quotes are present
$tmp = join ',', @new;

# Print bulk JS code

print "var array_name = new Array($tmp);"; # Now in JS style

# Print more JS code

Hope this helps... Tony Dean

----- Original Message -----
From: "J Wermont"
To:
Sent: Sunday, October 16, 2005 1:14 AM
Subject: Giving Javascript code access to Perl variables


> In a .CGI file, I'd like to include some Javascript code (which will be
> written to the output file sent to the client's browser), which will
> access Perl variables that were set in the same CGI file.
>
> It seems to work in some cases. For example, I have a CGI file that
> includes this code:
>
> #!/usr/local/bin/perl
>
> ... Perl code ...
>
> $loopcount = 4;
>
> ... more Perl code ...
>
> print << "ENDOFPAGE";
>
> ... HTML code ...
>
>
>
> ... more HTML ...
>
> ENDOFPAGE
>
>
> When this page is displayed in the browser, you see:
>
> Hello world
> Hello world
> Hello world
> Hello world
>
> So clearly the Javascript code recognizes the Perl variable $loopcount.
>
> Can you do this with Perl array values, too? So far I haven't had any
> success. Can anyone recommend a good site (or book) that addresses this
> issue?
>
> Here's my reason: I'm using Javascript to validate an HTML form, and
> I want it to be done on the client side. This works fine for checking
> invalid types of input (eg, numbers where letters should go, etc), but
> I also would like to make sure that what is being typed into a text
> input field is not a duplicate of something I already have stored in
> a database.
>
> Since I can access the database from my Perl script and store the values
> in a Perl array variable, I'm thinking I could then download the array
> to the client, so that the Javascript code can compare typed input with
> values in the array. Since I was able to download the value "4" in the
> Perl variable $loopcount, shouldn't I be able to download an array of
> values to the client?
>
> (By the way, this is a *very tiny* array of database values. If it
> involved a large amount of data, I would check for duplicates on the
> server, after the form was submitted, since I imagine the time it
> would take to download all that data to the browser would undermine
> any performance savings you get by doing client-side checking.)
>
> Hope this makes sense. Maybe I should post this to a Javascript group
> as well, but I'm hoping that some of you might be familiar with both
> languages and can advise me.
>
> Thanks!
>
> J. Wermont
>
>
> --
> MySQL Perl Mailing List
> For list archives: http://lists.mysql.com/perl
> To unsubscribe: http://lists.mysql.com/perl?unsub=ajhpdean@tesco.net
>
>


--
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe: http://lists.mysql.com/perl?unsub=gcdmp-msql-mysql-modules@m .gmane.org

Re: Giving Javascript code access to Perl variables

am 16.10.2005 19:11:11 von Felipe Gasper

I'd say the best way to do this is the JSON module on CPAN.

-FG

Quoth J Wermont on 10/15/2005 8:14 PM...
> In a .CGI file, I'd like to include some Javascript code (which will be
> written to the output file sent to the client's browser), which will
> access Perl variables that were set in the same CGI file.
>
> It seems to work in some cases. For example, I have a CGI file that
> includes this code:
>
> #!/usr/local/bin/perl
>
> ... Perl code ...
>
> $loopcount = 4;
>
> ... more Perl code ...
>
> print << "ENDOFPAGE";
>
> ... HTML code ...
>
>
>
> ... more HTML ...
>
> ENDOFPAGE
>
>
> When this page is displayed in the browser, you see:
>
> Hello world
> Hello world
> Hello world
> Hello world
>
> So clearly the Javascript code recognizes the Perl variable $loopcount.
>
> Can you do this with Perl array values, too? So far I haven't had any
> success. Can anyone recommend a good site (or book) that addresses this
> issue?
>
> Here's my reason: I'm using Javascript to validate an HTML form, and
> I want it to be done on the client side. This works fine for checking
> invalid types of input (eg, numbers where letters should go, etc), but
> I also would like to make sure that what is being typed into a text
> input field is not a duplicate of something I already have stored in
> a database.
>
> Since I can access the database from my Perl script and store the values
> in a Perl array variable, I'm thinking I could then download the array
> to the client, so that the Javascript code can compare typed input with
> values in the array. Since I was able to download the value "4" in the
> Perl variable $loopcount, shouldn't I be able to download an array of
> values to the client?
>
> (By the way, this is a *very tiny* array of database values. If it
> involved a large amount of data, I would check for duplicates on the
> server, after the form was submitted, since I imagine the time it
> would take to download all that data to the browser would undermine
> any performance savings you get by doing client-side checking.)
>
> Hope this makes sense. Maybe I should post this to a Javascript group
> as well, but I'm hoping that some of you might be familiar with both
> languages and can advise me.
>
> Thanks!
>
> J. Wermont
>
>

--
Felipe M. L. Gasper, conductor/singer
http://felipegasper.com

Judge ideas, not people.
Love people, not ideas.


--
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe: http://lists.mysql.com/perl?unsub=gcdmp-msql-mysql-modules@m .gmane.org

Re: Giving Javascript code access to Perl variables

am 18.10.2005 00:10:10 von Eric

------=_Part_1968_2014716.1129587010748
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Javascript is not actualy reading the value from perl. When you print out
the javascript it is inserting the value from the variable in perl into tha=
t
spot in the string. If you had used single quotes it would not work at all.
The moral, don't try to communicate between the two. Instead make your perl
output dynamic javascript. Yea that sounds crazy, but you can generate the
correct javascript for a specific case. So you do kind of get one way
communication, but realy in the end, if you view the javasript source it ha=
s
no idea that those values came from perl.

On 10/16/05, Felipe Gasper wrote:
>
> I'd say the best way to do this is the JSON module on CPAN.
>
> -FG
>
> Quoth J Wermont on 10/15/2005 8:14 PM...
> > In a .CGI file, I'd like to include some Javascript code (which will be
> > written to the output file sent to the client's browser), which will
> > access Perl variables that were set in the same CGI file.
> >
> > It seems to work in some cases. For example, I have a CGI file that
> > includes this code:
> >
> > #!/usr/local/bin/perl
> >
> > ... Perl code ...
> >
> > $loopcount =3D 4;
> >
> > ... more Perl code ...
> >
> > print << "ENDOFPAGE";
> >
> > ... HTML code ...
> >
> >
> >
> > ... more HTML ...
> >
> > ENDOFPAGE
> >
> >
> > When this page is displayed in the browser, you see:
> >
> > Hello world
> > Hello world
> > Hello world
> > Hello world
> >
> > So clearly the Javascript code recognizes the Perl variable $loopcount.
> >
> > Can you do this with Perl array values, too? So far I haven't had any
> > success. Can anyone recommend a good site (or book) that addresses this
> > issue?
> >
> > Here's my reason: I'm using Javascript to validate an HTML form, and
> > I want it to be done on the client side. This works fine for checking
> > invalid types of input (eg, numbers where letters should go, etc), but
> > I also would like to make sure that what is being typed into a text
> > input field is not a duplicate of something I already have stored in
> > a database.
> >
> > Since I can access the database from my Perl script and store the value=
s
> > in a Perl array variable, I'm thinking I could then download the array
> > to the client, so that the Javascript code can compare typed input with
> > values in the array. Since I was able to download the value "4" in the
> > Perl variable $loopcount, shouldn't I be able to download an array of
> > values to the client?
> >
> > (By the way, this is a *very tiny* array of database values. If it
> > involved a large amount of data, I would check for duplicates on the
> > server, after the form was submitted, since I imagine the time it
> > would take to download all that data to the browser would undermine
> > any performance savings you get by doing client-side checking.)
> >
> > Hope this makes sense. Maybe I should post this to a Javascript group
> > as well, but I'm hoping that some of you might be familiar with both
> > languages and can advise me.
> >
> > Thanks!
> >
> > J. Wermont
> >
> >
>
> --
> Felipe M. L. Gasper, conductor/singer
> http://felipegasper.com
>
> Judge ideas, not people.
> Love people, not ideas.
>
>
> --
> MySQL Perl Mailing List
> For list archives: http://lists.mysql.com/perl
> To unsubscribe: http://lists.mysql.com/perl?unsub=3Deric256@gmail.com
>
>


--
--
__________
Eric Hodges

------=_Part_1968_2014716.1129587010748--

Re: Giving Javascript code access to Perl variables

am 18.10.2005 00:27:56 von J Wermont

Eric wrote:

> Javascript is not actualy reading the value from perl. When you print out
> the javascript it is inserting the value from the variable in perl into that
> spot in the string.

That makes sense. I guess (in my example), the Javascript isn't really
evaluating the variable $loopcount at all, but simply sees a 4.

> The moral, don't try to communicate between the two. Instead make your perl
> output dynamic javascript.

Hmm, I think that's what I'm doing. Do you mean that the Javascript
code is being output by Perl code, like this:

print << "ENDOFPAGE"; # This is a line of Perl code



ENDOFPAGE

# Back to Perl

If so, that is already what I'm doing.

Actually, I've experimented some more and have discovered that Perl
array values will also be substituted into the Javascript code, so that
the Javascript sees the actual value. But it only works if the subscript
is an actual number. I'll have to figure out a way to code it so that the
subscript can be a Perl variable. Otherwise, there's no point to doing
this, since I'd always have to know how many items were in the array,
and how often does one know that?

Thanks.

J Wermont

--
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe: http://lists.mysql.com/perl?unsub=gcdmp-msql-mysql-modules@m .gmane.org

Re: Giving Javascript code access to Perl variables

am 18.10.2005 03:38:45 von Christopher Pryce

On Oct 17, 2005, at 5:27 PM, J Wermont wrote:

> I'll have to figure out a way to code it so that the
> subscript can be a Perl variable. Otherwise, there's no point to doing
> this, since I'd always have to know how many items were in the array,
> and how often does one know that?

you should take this off-list, as it way off topic.

the usual way to fill a javascript array is:

var ar = new Array();
ar[ ar.length ] = 'some value';

So, if you are creating your javascript from a Perl program, you can do
this (untested):

my $js = qq();

You can do the same thing with a Perl hash and a javascript Array
(again, untested):
my $js = qq();
>
> You can do the same thing with a Perl hash and a javascript Array
> (again, untested):
> my $js = qq(