perl json parser code example
Example: perl json response to list all the files in a directory
#!/usr/bin/perl
use JSON;
use CGI;
sub create_list_all() {
my $i = 1;
opendir my $dh, $dir or die "Can't open directory $dir: $!";
foreach $file ( readdir $dh ) {
if ( $i < 3 ) {
$i++;
next;
}
push( @list, $file );
}
closedir $dh;
}
print "Content-type: application/json;charset=UTF-8\n\n";
my @list;
#########insert directory path
my $dir = './';
create_list_all();
@list = sort { length $a <=> length $b || $b cmp $a } @list;
my $ret = to_json( \@list );
print $ret;
exit 0;