Click to See Complete Forum and Search --> : Can PERL read a JS data only file?


JMRKER
03-22-2011, 07:23 PM
This may be a stupid question, but I need to ask.

Is there a way that PERL can read a javascript data file?

I have a script that reads a 2-D data from an external file with the .js extension.
Is there a way to read the data and use it in a PERL script?

Could I treat it as a simple text file and split the information as needed?

Any other concepts I should consider?


Example 2-D data file:

// Externship Site Description
var Sites = [
["Key","Externship Site","","Location","O.Dz.<br>Site","Number of Students<br> Per Rotation"],
["1","Aker-Kasten Eye Center","","Boca Raton, FL","Y","3"],
["2","Advanced Eye Clinic","","Cheyenne, WY","Y","1"],
["3","Aran Eye Associates","","North Miami Beach, FL","Y","5"],
["4","Atlantic Avenue Eye Care","","Delray Beach, FL","N","1"],
["5","Bascom Palmer Eye Institute","","Miami, FL","Y","3 <br>(6 month rotation)"],
["6","Braverman Eye Center","","Hallandale, FL","Y","5"],
["7","Brevard Eye Center","(Dr. David Hendrix)","Melbourne, FL","Y","1"],
["8","Brevard Vision Care","(Dr. Kenneth Boyle)","Melbourne, FL","N","1"],
["9","Center for Sight","","Sarasota, FL","Y","3"],
["10","Clayton Eye Center","","Morrow, GA","Y","2"]
];


Example 1-D data file:

// Class of 2013
var Students = [
"Last Name,First Name,N#,Email,GPA",
"Boop,Betty,N0101,la51,2.5",
"Oop,Ally,N0090,alsa,",
"Fudd,Elmer,N0062,alsa,",
"Bunny,Bugs,N0068,la52,",
"Mouse,Mighty,N0108,aa10,",
"Mouse,Mickey,N0072,ashk,",
"Yosemite,Sam,N0105,fb27,",
"Kid,Katzenjammer,N0027,brij,",
"Duck,Donald,N0089,tb78,"
];

Ultimater
03-23-2011, 02:15 AM
You can try Perl's JSON (http://search.cpan.org/~makamaka/JSON-2.51/lib/JSON.pm) module. Although the module is very strict about the syntax of the incoming JavaScript Object.


#!/Perl/bin/perl -w
print "Content-Type: text/html\n\n";
use CGI::Carp qw(fatalsToBrowser);
use JSON;
use Data::Dumper;
my $js_string='[
["Key","Externship Site","","Location","O.Dz.<br>Site","Number of Students<br> Per Rotation"],
["1","Aker-Kasten Eye Center","","Boca Raton, FL","Y","3"],
["2","Advanced Eye Clinic","","Cheyenne, WY","Y","1"],
["3","Aran Eye Associates","","North Miami Beach, FL","Y","5"],
["4","Atlantic Avenue Eye Care","","Delray Beach, FL","N","1"],
["5","Bascom Palmer Eye Institute","","Miami, FL","Y","3 <br>(6 month rotation)"],
["6","Braverman Eye Center","","Hallandale, FL","Y","5"],
["7","Brevard Eye Center","(Dr. David Hendrix)","Melbourne, FL","Y","1"],
["8","Brevard Vision Care","(Dr. Kenneth Boyle)","Melbourne, FL","N","1"],
["9","Center for Sight","","Sarasota, FL","Y","3"],
["10","Clayton Eye Center","","Morrow, GA","Y","2"]
]';
my $result = decode_json($js_string);
print Dumper($result);

JMRKER
03-23-2011, 08:55 AM
Thank you. I'll look into that solution.