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

In reply to Re: can a script change itself? by thekestrel
in thread can a script change itself? by deathbygoats

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.