File: tokenize-synopsis.pl

package info (click to toggle)
libjson-parse-perl 0.62-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 572 kB
  • sloc: ansic: 3,614; perl: 475; makefile: 12
file content (27 lines) | stat: -rwxr-xr-x 651 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/home/ben/software/install/bin/perl
use warnings;
use strict;
use JSON::Tokenize ':all';

my $input = '{"tuttie":["fruity", true, 100]}';
my $token = tokenize_json ($input);
print_tokens ($token, 0);

sub print_tokens
{
    my ($token, $depth) = @_;
    while ($token) {
	my $start = tokenize_start ($token);
	my $end = tokenize_end ($token);
	my $type = tokenize_type ($token);
	print "   " x $depth;
	my $value = substr ($input, $start, $end - $start);
	print "'$value' has type '$type'.\n";
	my $child = tokenize_child ($token);
	if ($child) {
	    print_tokens ($child, $depth+1);
	}
	my $next = tokenize_next ($token);
	$token = $next;
    }
}