Sebastian
Sebastian

Reputation: 2550

Dancer unique request ID

Is there any unique request ID in Dancer?

Apache has mod_unique_id: http://httpd.apache.org/docs/current/mod/mod_unique_id.html

PSGI/Plack has a middleware module: http://search.cpan.org/~bayashi/Plack-Middleware-RequestId-0.02/lib/Plack/Middleware/RequestId.pm

But is there anything native in Dancer I missed?

Upvotes: 5

Views: 720

Answers (1)

Sobrique
Sobrique

Reputation: 53478

When I have needed unique IDs for use with Mojolicious, I've used Data::UUID which generates long (128bit) numbers in line with RFC 4122

I can't be any more specific without a clearer idea of your use case, but this seems to work nicely:

#!/usr/bin/env perl

use strict;
use warnings;

use Data::UUID;

my $gen = Data::UUID -> new();

my $binary_uuid = $gen -> create ;

print $gen -> to_string ( $binary_uuid ),"\n";
print $gen -> to_hexstring ( $binary_uuid ),"\n";
print $gen -> to_b64string ( $binary_uuid ),"\n";

You have a choice of output formats. You can, if it's useful to your application, create directly, e.g.:

my $gen = Data::UUID -> new();
my $uuid = $gen -> create_str ;
print $uuid, "\n";
#reformat output
print $gen -> to_hexstring ( $uuid ),"\n";

Upvotes: 1

Related Questions