• About Us
  • Blog
  • Basket
  • Account
  • Sign In

Perl V3.1 Documentation

You are viewing an old version of the documentation. For the latest, please go here: Perl V3.2

Trie Example

This is a console program takes a trie data file and a useragent as argument and outputs data about the detected device.

This program requires the JSON package. If you do not already have it installed you can get it via CPAN:

cpan JSON


  • -filename or -f: The path to a trie data file.
  • -properties or -p: The properties to return. If not set a default set of properties will be used.
  • -ua: The useragent to use. If not set a default useragent taken from Google Chrome 35 will be used.
use FiftyOneDegrees::TrieV3;
use JSON;
use feature qw/say/;
use Getopt::Long;

my $filename;
my $propertyList;
my $userAgentString;

my $r = GetOptions('filename|f=s' => \$filename,
                   'properties|p=s' => \$propertyList,
                   'ua=s' => \$userAgentString);

$userAgentString //= "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36";

$propertyList //= "Id,BrowserName,BrowserVendor,BrowserVersion,CookiesCapable,IsTablet,IsMobile,IsCrawler";

die "Trie DAT file does not exist! usage: $0 -f <Trie dat filename>\n" unless -e ("$filename");

# Initialize Trie library
eval {
	FiftyOneDegrees::TrieV3::dataSetInitWithPropertyString($filename, $propertyList);
	my $json = FiftyOneDegrees::TrieV3::getMatch($userAgentString);
	say "=== Data Output ===";

	my %properties = %{ decode_json($json) };
	# Loop through and print all returned properties
	while (my ($key, $value) = each %properties) {
		say $key . ": " . $value;

if ($@) {
    die "Error: $@\n";