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();
__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!
I was about to automate the above procedure, but i stumbled upon Inline::Module,
$ 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(); $

In reply to Re: Defining a package inside my script? by Anonymous Monk
in thread Defining a package inside my script? by Plankton

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.