In essence it would look like this:
#!/usr/bin/perl # lives tracks the number of times the program has run. # the line number is stored for later use in an array # called @program, so if consensual hackers decide to # rearrange, the program can still find the variable. $lives = 0; $line{lives} = (__LINE__ - 1); # The source code of the file is read into @program open( THIS, "$0" ); @program = <THIS>; close( THIS ); # The line number from earlier points at the line of code # storing the variable '$lives'. # Render the line and store it back into @program over the # old line of code. $program[ $line{lives} ] = '$lives = ' . ($lives+1) . '; $line{lives} = (__LINE__ - 1);' . "\n"; # Output the program's slightly modified source # code over the old file. open( THIS, ">$0" ); print THIS @program; close( THIS ); # Clear the screen buffer and 'exec' the modified source. system( 'clear' ); exec( $0 ); # exec() does not return to this program. print "This does not get displayed.";
abatek~

In reply to Re: Self-Modifying-Perl-Script by Anonymous Monk
in thread Self-Modifying-Perl-Script by crazyinsomniac

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.