For some reason i've always wanted to write a polymorphic perl script. Of course an example was quickly found on perlmonks (Self-Modifying-Perl-Script) and chromatic's code worked perfectly from the command line, but i couldn't make it work via CGI so set out to learn more perl and write my own.

I ended up having to spawn a child script that modifies it's parent. Oddly, i don't really understand a key area of this script i wrote (and got working by fiddling around with what worked). Here's what i mean (full source at eof).

After a child is opened, written to and closed:

open(X,">$time.pl") or die "$!"; print X $child; close(X);
The child is executed by the parent,
system "perl $time.pl";
The last thing the child does is (successfully) call the parent via CGI with a parameter like this:
print "Location: $parent_url?d=1\n\n";
but the next line in the parent successfully deletes the child!
unlink "$time.pl";
How does the child get deleted by the parent after modifying it and printing it's location? Btw this was developed and tested on Win98 with Activestate5.
#!perl -w use strict; use CGI qw(:standard); my$url = url(-relative=>1); my$time=time(); my$d = param('d'); if($d eq '1'){&show;exit} eval("seek DATA,0,0;"); undef$/; $_=<DATA>; # read self into $_ $_ =~ m/__END__\n(\d.*)/; my$num=$1; $num++; # find target in $_ and c +hange # build child script with $url and $num (to open, change, save, and ru +n parent) my$child = "#!perl -w\n\nuse CGI qw(:standard);\nopen(Z,\"+>>$url\") o +r die \"\$!\";\nundef\$/;\n\$_=<Z>;\n\$_=~s|__END__\\n(\\d.*)|__END__ +\\n$num|o;\nseek Z, 0, 0;\ntruncate Z, 0;\nprint Z \$_;\nclose(Z);\np +rint \"Location: $url?d=1\\n\\n\";"; open(X,">$time.pl") or die "$!"; # create and print X $child; close(X); # print and close system "perl $time.pl"; # and run and unlink "$time.pl"; # delete child sub show{ print header,start_html(-title=>'polymorphic perl',-bgcolor=>'#000000' +); while(<DATA>){ print qq~<a href="$url"><font size="+5" color="#FFFFFF" +>$_</font></a>~}} exit; __END__ 0


And here is a child (pp.pl is whatever url() in the parent returned, and the 8 in the regex is the value of $num):

#!perl -w use CGI qw(:standard); open(Z,"+>>pp.pl") or die "$!"; undef$/; $_=<Z>; $_=~s|__END__\n(\d.*)|__END__\n8|o; seek Z, 0, 0; truncate Z, 0; print Z $_; close(Z); print "Location: pp.pl?d=1\n\n";
thanks - epoptai

In reply to polymorphic perl by epoptai

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.