in reply to Re: Destringify class
in thread Destringify class

I am using 'require' to execute the script. The only reason I am doing this is that I need the facilities of Filter::cpp, and it has to run on a file, as far as I can tell. If I could run this internally, it'd be a lot easier.

Replies are listed 'Best First'.
Re^3: Destringify class
by kyle (Abbot) on Aug 12, 2008 at 20:55 UTC

    Here's a demonstration of what I was writing about:

    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.