epoptai has asked for the wisdom of the Perl Monks concerning the following question:
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:
The child is executed by the parent,open(X,">$time.pl") or die "$!"; print X $child; close(X);
The last thing the child does is (successfully) call the parent via CGI with a parameter like this:system "perl $time.pl";
but the next line in the parent successfully deletes the child!print "Location: $parent_url?d=1\n\n";
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.unlink "$time.pl";
#!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):
thanks - epoptai#!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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(tye)Re: polymorphic perl
by tye (Sage) on Dec 31, 2000 at 10:58 UTC | |
|
Re: polymorphic perl
by tilly (Archbishop) on Dec 31, 2000 at 11:02 UTC | |
by epoptai (Curate) on Jan 01, 2001 at 15:02 UTC | |
|
Re: polymorphic perl
by Dominus (Parson) on Dec 31, 2000 at 23:40 UTC |