perl json response to list all the files in a directory

Solutions on MaxInterview for perl json response to list all the files in a directory by the best coders in the world

showing results for - "perl json response to list all the files in a directory"
Roberto
27 Apr 2018
1#!/usr/bin/perl
2
3use JSON;
4use CGI;
5sub create_list_all() {
6    my $i = 1;
7    opendir my $dh, $dir or die "Can't open directory $dir: $!";
8    foreach $file ( readdir $dh ) {
9        if ( $i < 3 ) {
10            $i++;
11            next;
12        }
13        push( @list, $file );
14    }
15    closedir $dh;
16}
17
18print "Content-type: application/json;charset=UTF-8\n\n";
19
20my @list;
21#########insert directory path
22my $dir = './';
23create_list_all();
24@list = sort { length $a <=> length $b || $b cmp $a } @list;
25my $ret = to_json( \@list );
26print $ret;
27exit 0;
28
Jorge
30 Apr 2019
1#!/usr/bin/perl
2
3use JSON;
4use CGI;
5
6print "Content-type: application/json;charset=UTF-8\n\n";
7my $q = new CGI;
8my @list;
9my $dir = '/usr/amoeba/pub/b/OUHTC/etc/ERP/concur/data/to_proactive';
10sub create_list_all() {
11    my $i = 1;
12    opendir my $dh, $dir or die "Can't open directory $dir: $!";
13    foreach $file ( readdir $dh ) {
14        if ( $i < 3 ) {
15            $i++;
16            next;
17        }
18        push( @list, $file );
19    }
20    closedir $dh;
21}
22
23create_list_all();
24@list = sort { length $a <=> length $b || $b cmp $a } @list;
25my $ret = to_json( \@list );
26print "Content-type: application/json;charset=UTF-8\n\n";
27print $ret;
28exit 0;
29