0

I am taking backend perl variables and using it at the front end for display. Here is the code.

head1 and head2 are the header names taken from perl script. I use that and display those headers of grid on webpage.

var headname1= "@head1";
var headname2= "@head2";

Now, if there are 100 headers, i would have to assign variables 100 times separately. so its better to make it dynamic. Taking values from backend and assigning it to javascript variable. This task will be dynamic. Here is my attempted code for that.

I hav taken all the headers from backend and assigned it to line_array. @line_arry is the name of that array. If i do @line_arry[0] and so on. appropriate values will be displyaed.

var headname=[];

for(var i=0;i<total_columns;i++) {
    headname[i+1]= "@line_arry[(i)]";
}

This code doesnot work. It displays only the first column. i.e.

 headname[1] = "Order";
 headname[2] = "Order";

Order is the name of the first header. In headname[2], header of second column should be displayed. but it still displays "Order"

If i write the below code, it displays first and second header.

 headname[i+1]= "@line_arry[(0)]";
headname[i+1]= "@line_arry[(1)]"; 

"@line_arry[i]" doesnt work whereas 
"@line_arry[0], @line_arry[1] works.

Can anyone suggeest why it is not performing looping action using variable i

7
  • What back-end framework are you using? CGI or something more modern? Commented Aug 26, 2015 at 22:20
  • 1
    It looks like you're trying to generate JavaScript code from Perl. I think it would be better to make the Perl script return the data you want in JSON format; then you can make an Ajax request from your JavaScript code to get the data. Commented Aug 26, 2015 at 22:20
  • @ThisSuitIsBlackNot- ok, will use ajax get method to get data from backend and then work on it accordingly. thanks Commented Aug 26, 2015 at 22:22
  • I tried using it directly because it was just an array i was referring to. It worked in case of variables but i guess array is a bit confusing to use this way. will use the conventional method of ajax. Commented Aug 26, 2015 at 22:24
  • 2
    "Taking values from backend and assigning it to javascript variable." Can you explain what you mean by that? I've read your question 3 times, and I can't for the life of me figure out what you're trying to do. Commented Aug 26, 2015 at 22:37

1 Answer 1

1

Disclaimer: This has a lot of assumptions about the question.

If you are using CGI and you are trying to create JS code in Perl that you will print to the browser, it makes more sense to use JSON. As was suggested in the comments, you can use AJAX, but you can also just use the JSON module to get the JSON representation of your Perl data structure, and incorporate that as a string into your website.

use strict;
use warnings;
use CGI;
use JSON 'encode_json';

my $q = CGI->new;

# make some test headers
my @headers = map { "header $_" } ( 1 .. 100 };

# convert it to JSON representation (a string!)
my $header_as_json = encode_json(\@headers);

print $q->header();
print <<HTML
<html>
  <head>
    <script>
var headname = $header_as_json;
    </script>
  </head>
  <body>Hello World!</body>
</html>
HTML
Sign up to request clarification or add additional context in comments.

1 Comment

@Simbabque- All your assumptions were right. I just had to use JSON module to get JSON representation of the prel data structure. And after that i was able to get the values in a for loop properly.

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.