I have this really bizarre idea about creating a script that can (among many other things) maintain itself and clean itself up. As part of the script, I'd like the script to examine itself and, if necessary, change itself. Obviously, dealing with problems of file access can easily corrupt a program. So i'm stumped as to how to go about doing this.

I have two thoughts as to how this might be done:

1) The script either runs as a copy, or creates a copy of itself and examines the file that is not currently being accessed. If necessary, make changes the the accessible copy and, when finished, the script closes itself and runs the new version of the script (and delete the old version).

2) Load and run the script in memory so that there's no file access violations on the original file, and whenever a change is made, that file is updated. When the script is complete, you will be left with an up-to-date file as a result of the scripts own changes.

Are either of these options possible? Or am I getting into something that is beyond the limitations of Perl? I just think that the possibilities of such a method would be endless.

FYI, I'm using a Redhat Linux OS.

--Coplan


In reply to Run as copy or read/write self by Coplan

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.