Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unique Number UUID

Status
Not open for further replies.

DaveRolph

Programmer
Sep 12, 2001
26
GB
Reading various articles I now know I want to create a UUID variable within my existing form script.

Can anyone tell me where to find an idiots guide to UUID ?

I have some examples and I know what I need to add to the script, my problem is that I don't now how to install the module.

This is not my own server I am using fasthosts so I only have a web master type access.

Any help would be greatly appreciated.

Dave Rolph
 
ask the host to install any modules you need. Yuo can always use the lib pragma to include modules with actually installing them. Although some modules have many dependencies and are not practical to use via lib.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Oh dear, they won't install it !

Any idea how I can easily create a sequence number without adding another module ?

I want a unique sequence number from 0000001 and then in sequence to go in my file at the end of the rest of the data.

Help !
 
which module were you wanting to use?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I was wanting to use UUID

now I have given up on this one and I am building a string from Year,Month,Day, HH, SS, MM and IP address does this sound workable - the system will only be used by a few users and hardly ever at once !
 
Something I'd do is use a loop. If you had a directory called "users" with files like this:

Code:
00000001.txt
00000002.txt
00000003.txt
00000004.txt

then

Code:
my $uid = 0;
opendir (DIR, "./users");
foreach my $file (sort(grep(/\.txt$/i, readdir(DIR)))) {
    my ($num,$ext) = split(/\./, $file, 2);
    $uid = int($num); # remove all those 0's
}
closedir (DIR);

# at this point, $uid = 4, so we add 1 to it.
$uid++;

# pad the UID with all the zeroes we want
$uid = "0" . $uid until length $uid == 8;

# now we have our uid for the new user.
open (CREATE, ">./users/$uid\.txt");
print CREATE "new user!";
close (CREATE);

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
Kirsle said:
Code:
# pad the UID with all the zeroes we want
$uid = "0" . $uid until length $uid == 8;

It's much simpler and easier to decypher to just use sprintf for padding numbers and strings.

Code:
[blue]$uid[/blue] = [url=http://perldoc.perl.org/functions/sprintf.html][black][b]sprintf[/b][/black][/url] [red]"[/red][purple]%08d[/purple][red]"[/red], [blue]$uid[/blue][red];[/red]

- Miller


 
You can most likely just copy and paste the source code of the UUID module into a text file, save it as UUID.pm and upload it into your cgi-bin and use it that way.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin's method would probably work just fine.

I personally always stick with databases when it comes to tracking user sessions.

However, if you want to use Kirsle's suggestion, here is a cleaned up and "safer" version of his code.

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]File::Spec[/green][red];[/red]
[black][b]use[/b][/black] [green]IO::File[/green][red];[/red]
[black][b]use[/b][/black] [green]List::Util[/green] [red]qw([/red][purple]max[/purple][red])[/red][red];[/red]
[black][b]use[/b][/black] [green]Readonly[/green][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

Readonly [url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$DIR_UID[/blue] => [red]'[/red][purple]./users[/purple][red]'[/red][red];[/red]
Readonly [black][b]my[/b][/black] [blue]$UID_MIN[/blue] => [fuchsia]0[/fuchsia][red];[/red]

[black][b]my[/b][/black] [blue]$uid[/blue] = [maroon]new_uid[/maroon][red]([/red][red])[/red][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]new userid is [blue]$uid[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[gray][i]#######################################[/i][/gray]
[gray][i]# Usage      : new_uid()[/i][/gray]
[gray][i]# Returns    : New Unique UID[/i][/gray]
[gray][i]# Parameters : none[/i][/gray]
[gray][i]# Comments   : Safe from race conditions[/i][/gray]
[url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [maroon]new_uid[/maroon] [red]{[/red]
	[black][b]my[/b][/black] [blue]$uid[/blue] = [maroon]get_uid_max[/maroon][red]([/red][red])[/red][red];[/red]
	
	[black][b]my[/b][/black] [blue]$fh[/blue][red];[/red]
	[black][b]my[/b][/black] [blue]$filepath[/blue][red];[/red]
	[url=http://perldoc.perl.org/functions/do.html][black][b]do[/b][/black][/url] [red]{[/red]
		[black][b]my[/b][/black] [blue]$file[/blue] = [url=http://perldoc.perl.org/functions/sprintf.html][black][b]sprintf[/b][/black][/url] [red]"[/red][purple]%08d.txt[/purple][red]"[/red], ++[blue]$uid[/blue][red];[/red]
		[blue]$filepath[/blue] = File::Spec->[maroon]catfile[/maroon][red]([/red][blue]$DIR_UID[/blue], [blue]$file[/blue][red])[/red][red];[/red]
	[red]}[/red] [olive][b]until[/b][/olive] [red]([/red][blue]$fh[/blue] = IO::File->[maroon]new[/maroon][red]([/red][blue]$filepath[/blue], O_RDWR|O_CREAT|O_EXCL[red])[/red][red])[/red][red];[/red]
	
	[black][b]print[/b][/black] [blue]$fh[/blue] [red]"[/red][purple]new user![/purple][red]"[/red][red];[/red]
	[url=http://perldoc.perl.org/functions/undef.html][black][b]undef[/b][/black][/url] [blue]$fh[/blue][red];[/red]
	
	[url=http://perldoc.perl.org/functions/return.html][black][b]return[/b][/black][/url] [blue]$uid[/blue][red];[/red]
[red]}[/red]


[gray][i]#######################################[/i][/gray]
[gray][i]# Usage      : get_uid_max()[/i][/gray]
[gray][i]# Returns    : Current Maximum UID[/i][/gray]
[gray][i]# Parameters : none[/i][/gray]
[gray][i]# Comments   : This information is subject to race conditions[/i][/gray]
[black][b]sub[/b][/black] [maroon]get_uid_max[/maroon] [red]{[/red]
	[url=http://perldoc.perl.org/functions/local.html][black][b]local[/b][/black][/url][red]([/red][blue]*DIR[/blue][red])[/red][red];[/red] [gray][i]# this handle will be destroyed upon return[/i][/gray]

	[black][b]my[/b][/black] [blue]$uid_max[/blue] = [blue]$UID_MIN[/blue][red];[/red]
	[url=http://perldoc.perl.org/functions/opendir.html][black][b]opendir[/b][/black][/url][red]([/red]DIR, [blue]$DIR_UID[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open [blue]$DIR_UID[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
	[olive][b]while[/b][/olive] [red]([/red]<DIR>[red])[/red] [red]{[/red]
		[olive][b]next[/b][/olive] [olive][b]unless[/b][/olive] [red]/[/red][purple]^0*([purple][b]\d[/b][/purple]+)[purple][b]\.[/b][/purple]txt$[/purple][red]/[/red][red];[/red]
		[blue]$uid_max[/blue] = [maroon]max[/maroon][red]([/red][blue]$uid_max[/blue], [blue]$1[/blue][red])[/red][red];[/red]
	[red]}[/red]
	[url=http://perldoc.perl.org/functions/closedir.html][black][b]closedir[/b][/black][/url][red]([/red]DIR[red])[/red][red];[/red]
	
	[black][b]return[/b][/black] [blue]$uid_max[/blue][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]File::Spec - portably perform operations on file names[/li]
[li]IO::File - supply object methods for filehandles[/li]
[li]List::Util - A selection of general-utility list subroutines[/li]
[/ul]
Other Modules used :
[ul]
[li]Readonly[/li]
[/ul]
[/tt]

- Miller
 
All very useful stuff

I am a bit restricted because I have no control over this server, the hosting service will not allow any modules on the server.

The key I wrote works fine for this one but Millers script looks much better (thanks for spending the time)

Kevin - thanks very much you too.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top