in reply to Defining a package inside my script?
BEGIN { $INC{'MyModule.pm'} = __FILE__; package MyModule; use strict; my @things = ( 'thing1' , 'thing2' , 'thing3' , ); sub new { my( $c, $t ) = @_; $c = bless { thing => $t }, $c; $c } sub is_thing { my $self = shift; my $t = $self->{'thing'}; return grep /^$t$/, @things; } 1; } package main; #!/usr/bin/perl -w use strict; use lib('.'); use MyModule; my $thingy = new MyModule ( 'thing2' ); print "yeah!\n" if $thingy->is_thing(); print "boo!\n" unless $thingy->is_thing();
I was about to automate the above procedure, but i stumbled upon Inline::Module,__END__ $ echo BEGIN { >newfile.pl $ echo \$INC{'MyModule.pm'} = __FILE__; >>newfile.pl $ cat MyModule.pm >>newfile.pl $ echo } >>newfile.pl $ echo package main; >>newfile.pl $ cat myscript.pl >>newfile.pl $ perl newfile.pl yeah!
$ cat myscript2.pl #!/usr/bin/perl -w use strict; use lib('.'); use Inline::Module 'MyModule'; my $thingy = new MyModule ( 'thing2' ); print "yeah!\n" if $thingy->is_thing(); print "boo!\n" unless $thingy->is_thing(); $ $ cat myscript2.plc # Generated by Inline::Module 0.10 (Module::Compile 0.20) - do not edi +t! ################((( 32-bit Checksum Validator III )))################ #line 1 BEGIN { use 5.006; local (*F, $/); ($F = __FILE__) =~ s!c$!!; open(F) or die "Cannot open $F: $!"; binmode(F, ':crlf'); if (unpack('%32N*', $F=readline(*F)) != 0x5F5C4198) { use Filter::Util::Call; my $f = $F; filter_add(sub { filter_del(); 1 while &filter_read; $_ = $f; 1; })}} #line 1 # 13362d28eaf259978a36faeb9815166d7294ada5 use strict; use lib('.'); # use Inline::Module 'MyModule'; BEGIN { $INC{'MyModule.pm'} = 'Inline::Module' } BEGIN { # begin Inline::Module MyModule package MyModule; use strict; my @things = ( 'thing1' , 'thing2' , 'thing3' , ); sub new { my( $c, $t ) = @_; $c = bless { thing => $t }, $c; $c } sub is_thing { my $self = shift; my $t = $self->{'thing'}; return grep /^$t$/, @things; } 1; } # end Inline::Module MyModule use MyModule ; my $thingy = new MyModule ( 'thing2' ); print "yeah!\n" if $thingy->is_thing(); print "boo!\n" unless $thingy->is_thing(); $
|
|---|