| 1 | =head1 NAME | ||||||||||
| 2 | |||||||||||
| 3 | Sys::UniqueID - Get a guaranteed unique identifier. | ||||||||||
| 4 | |||||||||||
| 5 | Great for generating database keys, temporary filenames, | ||||||||||
| 6 | and even gets out those tough grass stains! | ||||||||||
| 7 | |||||||||||
| 8 | =head1 SYNOPSIS | ||||||||||
| 9 | |||||||||||
| 10 | use Sys::UniqueID; | ||||||||||
| 11 | $id= uniqueid; | ||||||||||
| 12 | |||||||||||
| 13 | =head1 AUTHOR | ||||||||||
| 14 | |||||||||||
| 15 | v, E<lt>v@rant.scriptmania.comE<gt> | ||||||||||
| 16 | |||||||||||
| 17 | =head1 SEE ALSO | ||||||||||
| 18 | |||||||||||
| 19 | perl(1), Sys::HostIP | ||||||||||
| 20 | |||||||||||
| 21 | =cut | ||||||||||
| 22 | |||||||||||
| 23 | package Sys::UniqueID; | ||||||||||
| 24 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 1 | ▶ | use Sys::HostIP; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 1 | ▶ | use vars qw($VERSION @ISA @EXPORT); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 1 | ▶ | use vars qw($netaddr $idnum); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | require Exporter; | ||||||||||
| 29 | |||||||||||
| 30 | @ISA = qw(Exporter); | ||||||||||
| 31 | @EXPORT = qw(uniqueid); | ||||||||||
| 32 | $VERSION = '1.0'; | ||||||||||
| 33 | |||||||||||
| 34 | sub uniqueid() | ||||||||||
| 35 | 0 | ▶ | { | ||||||||
|
Subroutine
uniqueid: not called CC=2 SCAR=17.9
| |||||||||||
| 36 | # absolutely ensure that id is unique: < 0x10000/second | ||||||||||
| 37 | 0 | ▶ | unless(++$idnum < 0x10000) { sleep 1; $idnum= 0; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 0 | return sprintf '%012X.%s.%08X.%04X', time, $netaddr, $$, $idnum; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | $netaddr= sprintf '%02X%02X%02X%02X', (split /\./, hostip); | ||||||||||