in reply to Self-Modifying-Perl-Script

You could do this with the __END__ tag. Just keep your number there. Something like the following would work (actually tested and updated, but occasionally I get one right):
#!/usr/bin/perl -w use strict; chomp(my $num = <DATA>); print "My counter is set to $num.\n"; $num++; { local *FILE; local $/ = "\n__END__"; open (FILE, "counter.pl") || die "Can't open my file for reading: +$!\n"; chomp(my $file = <FILE>); close FILE; open (FILE, ">counter.pl") || die "Can't open file for writing: $! +"; print FILE $file, "\n__END__\n$num"; close FILE; } __END__ 0
I wouldn't do it this way, though. It's easy to get these wrong and overwrite your program in the blink of an eye, and it's easy to use one of the DBM modules that comes with Perl and get things right. Still, it's good to know some of these techniques.

Replies are listed 'Best First'.
Re^2: Self-Modifying-Perl-Script
by dse (Novice) on Nov 04, 2015 at 23:26 UTC
    Here's something I whipped up for fun:
    #!/usr/bin/env perl -pi.orig use warnings; use strict; BEGIN { @ARGV = $0; while (<>) { do { print; last; } if /^__DATA__$/; } continue { print; } } tr/A-Z/N-ZA-M/; tr/a-z/n-za-m/; __DATA__ Guvf vf n cebtenz gung zbqvsvrf vgfrys va cynpr. Vg ebg13 rapbqrf vgf bja __QNGN__ frpgvba jura lbh eha vg. Nalguvat orsber gur __QNGN__ frpgvba vf yrsg hazbqvsvrq. Lbh jvyy frr gur sbyybjvat qvntabfgvp reebe, juvpu lbh pna vtaber: -v hfrq jvgu ab svyranzrf ba gur pbzznaq yvar, ernqvat sebz FGQVA.