Re: can a script change itself?
by ikegami (Patriarch) on Mar 24, 2005 at 15:07 UTC
|
Data::UUID will create a unique number, without having to know which id was last assigned. The number might be too big for you, though.
Update: This module is a standard implementation of jZed's suggestion.
| [reply] |
Re: can a script change itself?
by sweetblood (Prior) on Mar 24, 2005 at 15:07 UTC
|
| [reply] |
|
|
Inline::Files sounds like a good idea. the UID number attribute will be used for the posixAccount object class (unix logins), so i'm pretty sure it actually has to be a number. using an ancillary file would've been my other option, although i think if i can store the uid in the script it might be the most convenient.
| [reply] |
|
|
Wait a tic...the last time I added Unix accounts, I don't remember the need to specify a UID. The system takes care of this automatically. Am I missing something?
thor
Feel the white light, the light within
Be your own disciple, fan the sparks of will
For all of us waiting, your kingdom will come
| [reply] |
A script can change itself
by ktross (Deacon) on Mar 24, 2005 at 16:21 UTC
|
There are better ways of doing this, but yes, a script can change itself. Here is a self modifying script that keeps track of the number of times it is called.
# Get current file name
$filename = __FILE__;
# get old UID stored after __END__
$uid = <DATA>;
chomp($uid);
print "This script has been run $uid times $/";
# increment UID
$uid++;
# open current running file
open(THIS_IN, $filename) or die "Could not open file";
@code = <THIS_IN>;
close (THIS_IN);
# Store new UID in last line of this code
$size = @code;
@code[$size-1]=$uid;
# open current running file
open(THIS_OUT,"> $filename") or die "Could not output to file";
print THIS_OUT @code;
close (THIS_OUT);
__END__
0
This is presented mostly for interest, as I am from the school of thought that self-modification is bad. It can lead to some very hard to track down bugs. | [reply] [d/l] |
|
|
| [reply] |
|
|
I know "multiple concurrent user safety" wasn't part of the specs of this question, but I thought it would be good to give a word of warning here anyhow. I've been bitten before by race conditions when using cook-book (aka cut-and-paste) style programming before.
I learned one lesson in my intro to concurrency course: "Concurrency is hard." There are few, if any, concurrent debuggers in existance ( one grad student was working on one, with some limited success), and the very convenient rule that states "if I run the same program, with the same inputs, I'll get the same output" goes out the window, because other programs can interfere.
Avoid concurent programs when you can: when you can't, swear loudly and quadruple your delivery date.
--
Ytrew
| [reply] |
Re: can a script change itself?
by jZed (Prior) on Mar 24, 2005 at 15:04 UTC
|
You probably could do some jiggery-pokery with $0, but it sounds like a mess. Why not just create UIDs on the fly e.g. from time-in-seconds + large random number + process id. | [reply] |
Re: can a script change itself?
by perlfan (Parson) on Mar 24, 2005 at 15:43 UTC
|
If it does not have to increment, you can do:
my $id = crypt($firstname,$lastname);
Where "$firstname" is 'crypted' using "$lastname" as the salt. I wouldn't use to this to store any passwords, but it is way to calculate (and retrieve) their record in the future by being able to predict their id rather than searching for them. You could also use md5. | [reply] [d/l] |
|
|
Well, if the OP was satisfied to have unique strings, he could have taken the $firstname.$lastname in the first place. Which actually has a better chance of being unique than your suggestion - as it will return the same string for "John Black" and "John Blueberry".
| [reply] |
Re: can a script change itself?
by FitTrend (Pilgrim) on Mar 24, 2005 at 15:05 UTC
|
If your on windows, you could store it in the registry and then call it each time you need it within your perl file.
Optionally you can use any database module from CPAN to save this data to a database DBD::SQLite, DBD::MySQL, DBI, etc.
Hope this helps
| [reply] |
Re: can a script change itself?
by satchm0h (Beadle) on Mar 24, 2005 at 15:33 UTC
|
You could just use an ancillary file:
use strict;
my $uuid_store = '/tmp/.uuid_store';
my $MY_UID=1;
if (-e $uuid_store) {
open INHANDLE, "<$uuid_store" || die "Can't open $uuid_store for r
+ead\n";
my $var = <INHANDLE>;
close INHANDLE;
$MY_UID = int $var;
}
print "The uid you are looking for : $MY_UID\n";
open OUTHANDLE, ">$uuid_store" || die "Can't open $uuid_store for writ
+e\n";
print OUTHANDLE ++$MY_UID;
close OUTHANDLE;
Output:
% perl gen_uid.pl
The uid you are looking for : 1
% perl gen_uid.pl
The uid you are looking for : 2
% perl gen_uid.pl
The uid you are looking for : 3
% perl gen_uid.pl
The uid you are looking for : 4
% perl gen_uid.pl
The uid you are looking for : 5
% perl gen_uid.pl
The uid you are looking for : 6
| [reply] [d/l] [select] |
Re: can a script change itself?
by thekestrel (Friar) on Mar 24, 2005 at 16:29 UTC
|
Hi,
Just for the sake of putting it in the same file, I guess you could open the script and append a comment like '#NEXT_UID=<num>'. Then when the script runs you can just replace the value. That way you can also do it with more than one variable as it is named. This isn't the prettiest code, but it works =P.
#!/usr/bin/perl
use strict;
use warnings;
sub get_next_number {
open FILE, "<$0" or die "1:Could not open file : $!\n";
my @lines = <FILE>;
my $n = $1 if ( @lines[scalar @lines - 1] =~ /#NEXT_UID=(\d+)/ );
my $m = $n + 1;
pop @lines;
push @lines, "#NEXT_UID=$m\n";
close (FILE);
open FILE, "+<$0" or die "2:Could not open file : $!\n";
print FILE $_ foreach @lines;
close (FILE);
return $n;
}
my $num;
$num = get_next_number();
print "Next available number is : $num\n";
$num = get_next_number();
print "Next available number is : $num\n";
#NEXT_UID=14
Regards Paul
Update:ktross beat me to the punch with the same idea =P.
Cleaned code a little and made it so you can now have multiple variables and have them anywhere in the file also...Version 2 =P
#!/usr/bin/perl
use strict;
use warnings;
#My changeable variables
#NEXT_UID=38
#NEXT_FLUFFY=6
sub get_next_number {
my $var = shift;
my $n = -1;
open FILE, "<$0" or die "1:Could not open file : $!\n";
my @lines = <FILE>;
close (FILE);
for my $linenum ( 1 .. scalar @lines - 1 ) {
if ( $lines[$linenum] =~ /#$var=(\d+)/ ) {
$n = $1;
my $m = $n + 1;
$lines[$linenum] = "#$var=$m\n";
open FILE, ">$0" or die "2:Could not open file : $!\n";
print FILE $_ foreach @lines;
close (FILE);
}
}
return $n;
}
my $num;
$num = get_next_number("NEXT_UID");
print "Next available number is : $num\n";
$num = get_next_number("NEXT_FLUFFY");
print "Next available number is : $num\n";
| [reply] [d/l] [select] |
Re: can a script change itself?
by mojotoad (Monsignor) on Mar 24, 2005 at 19:54 UTC
|
Why do you want to create a unique UID?
When you create an LDAP entry, it is assigned a Distinguished Name attribute (DN) -- this is a unique identifier for that LDAP entry.
The LDAP server itself has these DN's stored at that point, there's no reason to store them separately for your script -- they can be retreived at any time.
However, if you're dead set on storing unique identifiers, store the DN's in a hash for later reference. I like to use Data::Dumper to dump the hash into a data file for later reference and update.
Cheers,
Matt | [reply] |
|
|
| [reply] |
|
|
From the perl LDAP FAQ:
Every entry in a directory has a Distinguished Name, or DN. It is a unique Entry identifier throughout the complete directory. No two Entries can have the same DN within the same directory.
On the other hand, a relative distinguished name (RDN):
Every DN is made up of a sequence of Relative Distinguished Names, or RDNs. The sequences of RDNs are separated by commas (,). In LDAPv2 semi-colons (;) were also allowed. There can be more than one identical RDN in a directory, but they must have different parent entries.
I see your second point. However, I suppose it depends on how you're handling records -- in the event of marriage, for example, you could look at it as one LDAP entry being deleted and a new one created. That's fine if you don't care about tracking the history of name changes.
Cheers,
Matt
| [reply] |
|
|