Re: Giving Javascript code access to Perl variables

Felipe Gasper <[email protected]>
Newsgroups gmane.comp.db.mysql.perl
Message-ID <[email protected]>
Sorry, and no disrespect intended, but there's too much misinformation 
in here not to post this for general consumption.

The "usual" way to "fill" a javascript array is to use an array literal, 
like so:

var myArray = ['foo',2,['another','array']];

If you're going to populate an array one member at a time, then use the 
push method of the Array prototype:

myArray.push(newValue);

Associative arrays (in perl "hashes") are represented in Javascript via 
object literals, like so

var myHash = {foo: 'bar', qux: [1,2,3]};

Yes, you can declare "var ar = new Array();" and so forth, but why? (You 
can also use Array objects for hashes, but it's wasteful and pointless.)

I strongly urge anyone considering outputting perl data structures of 
any kind into Javascript to use the JSON module. You'll save quite a bit 
of work and be introduced to a wonderful format for data exchange in the 
process. It works like so:

my $perlData = {foo => 'bar', anArray => [1,2,3]};
my $jsData = JSON::objToJson($perlData);
print script("jsData = ".$jsData);

Boom, the javascript variable "jsData" contains the data structure 
originally in perlData, with typing preserved. Easy.

-FG

Quoth Christopher Pryce on 10/17/2005 9:38 PM...
> 
> 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(<script type="text/javascript">
>     var ar = new Array();
> );
> 
> foreach ( @somePerlList ) {
>     $js .= qq( ar[ ar.length ] = $_ );
> }
> 
> $js .= qq(</script>);
> 
> You can do the same thing with a Perl hash and a javascript Array 
> (again, untested):
> my $js = qq(<script type="text/javascript">
>     var ar = new Array();
> );
> 
> while (($key, $value) = each %hash) {
>     $js = ar[ $key ] = $value;
> }
> 
> Creates, in affect, an object.
> 
> You can combine a DBI fetch with a complex javascript object. Again, 
> untested:
> 
> my $sql = "Select * from foo":
> my $sth = $dbh->prepare( $sql );
> $sth->execute();
> 
> my $js = qq( var obj = new Object(); );
> while (my $row = $sth->fecthrow_hashref() ) {
>     $js .= 'obj[ obj.length] = {' . join(", ", map{ "$_: $row->{ $_ }" } 
> keys %$row ) . "};\n";
> }
> 
> or if you have a keyfield, in this example called 'rowid', you can do:
> 
> my $hashref = $dbh->selectall_hashref( $sql, 'rowid');
> my $js = qq( var obj = new Object(); );
> 
> foreach my $rowid ( keys %$hashref ) {
>     $js .= "obj[ $rowid ] = {" . join(", ", map{ "$_: '$hashref->{ 
> $rowid }{ $_ }'" } keys %{ $hashref->{ $rowid } }) . "};\n";
> }
> 
> and if you use the CGI module:
> $script = script({-type=>'text/javascript'}, $js );
> print header(), start_html(-head => $script ), etc...
> 
> 
> 
> 
> 

-- 
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/[email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.