use strict;
use warnings;
package My::Self;
sub new { bless $_[1] }
package main;
my $obj = My::Self->new({ authentic => int rand 1_000_000 });
my $to_be_required = './deleteme.pl';
open my $out_fh, '>', $to_be_required
or die "Can't write '$to_be_required': $!";
print {$out_fh} <<"END_OF_SUBSCRIPT";
print "My name is '$to_be_required'\n";
print "I've heard of this self: ",
\$main::self_everyone_knows->{authentic},
"\n";
print "Have a nice day!\n";
END_OF_SUBSCRIPT
;
close $out_fh;
$main::self_everyone_knows = $obj;
require $to_be_required;
print "Did it know $obj->{authentic}?\n";
unlink $to_be_required or die "Can't unlink '$to_be_required': $!";
__END__
My name is './deleteme.pl'
I've heard of this self: 507287
Have a nice day!
Did it know 507287?
Some things to note:
- This tosses out a warning about $main::self_everyone_knows being used only once.
- I've set $main::self_everyone_knows after I wrote the file. Its value isn't used when writing the file actually. In the file, there's a literal "$main::self_everyone_knows", but the first line will be "print "My name is './deleteme.pl'\n"". $main::self_everyone_knows just has to have its value before the require.
- You can do whatever you want to the file between when you write it and when you require it.
|