0

I was attempting to integrate Perl code with JavaScript and came across an issue where I simply wanted to see the output of an Array I have stored within Perl. The following code gives me the proper elements within the array I have stored but I'm not able to see the elements within JavaScript.

my @list_of_vendors;
while(!$resultSet->EOF) {
push @list_of_vendors, $resultSet->Fields("vendor")->Value;
    $resultSet->MoveNext;
}

print "The First Vendor is: " . $list_of_vendors[0]. "\n";

This Prints out The First vendor is: 3D Systems The following code doesn't seem to give me the results I want...

print <<ONE;
<html>
<body>
<h4> Test Vendor Array Javascript </h4>

<p id="demo"</p>

<script type="text/javascript" language="JavaScript">
var myVendorArray = @list_of_vendors;
document.write('<p> $list_of_vendors[0] is the same as myVendorArray[0] </p>');

</script>
</body>
</html>
ONE

I thought this would give me 3D Systems is the same as 3D Systems but it gives a blank for the html screen.

Can anyone point out to me what I might be doing wrong? Or point me in the right direction to debug this using notepad++ or some other useful IDE for this. Thanks a bunch!!

3
  • Sorry not necessarily doesn't work but not giving me the results I think it is supposed to give...I want to see 3D Systems is the same as 3D Systems` on the html page... but for some reason this is not showing... Commented Jun 17, 2014 at 16:53
  • 2
    Have you tried doing a "View Source" to see the actually generated HTML? I think the problem will be pretty obvious . . . Commented Jun 17, 2014 at 17:04
  • @ruakh I tried that and noticed from here that I didn't get the results I wanted...The JavaScript picks up the variable but doesn't display it the way I want...It makes@list_of_vendors come back with a long string of vendors instead of being an array...Thanks for the suggestion #feelslikeanoob Commented Jun 17, 2014 at 18:24

1 Answer 1

3

Everything in your code's heredoc is interpolated with Perl but interpreted by the client as JavaScript. So if you write

@list_of_vendors = ("Alice","Bob","Charlie");
...
print <<EOF;
...
<script>
var list_of_vendors = @list_of_vendors;
...
EOF

what the client will see is something like

var list_of_vendors = Alice Bob Charlie;

Perl has very good facilities for converting Perl data structures into the JSON notation, which is easily understood by JavaScript, so you should do something like:

use JSON;
$list_of_vendors_json = encode_json(\@list_of_vendors);
...
var list_of_vendors = $list_of_vendors_json;
...

in which case the browser would see something like:

var list_of_vendors = ["Alice","Bob","Charlie"];

and the rest of your code would work as intended.

Sign up to request clarification or add additional context in comments.

2 Comments

Do I have to download JSON for this two work or is this a functionality within perl that can be made or is there some type of add-on for this?
Install JSON module with cpan.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.