in reply to can a script change itself?

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";