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

Pattern Example

This is a console program takes a pattern 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

Arguments:

  • -filename or -f: The path to a pattern 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::PatternV3;
use JSON;
use Data::Dumper;
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 "DAT file does not exist! usage: $0 -f <dat filename>\n" unless -e ("$filename");

# Initialize Pattern library

# For pattern a dataset pointer is needed. This will be passd into other
# PatternV3 functions.
my $dsPointer = FiftyOneDegrees::PatternV3::dataSetInitWithPropertyString($filename, $propertyList);
my $json = FiftyOneDegrees::PatternV3::getMatch($dsPointer, $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;
}

FiftyOneDegrees::PatternV3::destroyDataset($dsPointer);

Need Help?