in reply to extract all subroutines from .pm's and include in .pl

Some of the suggestions already made may be better, but if you really must inline a module, some more work will be required. Also note that Exporter will not work in an inlined module.

Basically, you can write a program with an inline OO module/class by putting its package into a BEGIN block. Copying an existing module into a BEGIN block should work with appropriate code adjustments. YMMV and you will need to experiment a bit.

#!/usr/bin/perl use mymodules::foo; # Remove this line # main program here exit; # copy your modules into BEGIN blocks # be prepared to tweak & debug BEGIN{ package mymodules::foo; sub new { ... } sub bar { ... } etc ... }

Replies are listed 'Best First'.
Re^2: extract all subroutines from .pm's and include in .pl
by dsheroh (Monsignor) on May 19, 2009 at 10:36 UTC
    You don't even need the BEGIN block:
    #!/usr/bin/perl use strict; use warnings; my $foo = Inline::Foo->new('Hello, world!'); $foo->display; package Inline::Foo; sub new { my $class = shift; return bless { text => shift }, $class; } sub display { my $text = $_[0]->{text}; print "$text\n"; }
      You don't even need the BEGIN block:

      Maybe not in the examples shown, but what if Inline::Foo has a package global, or has some initialization code outside of the subs? Then the package must be either moved to the top of the program or enclosed in a BEGIN block. Also, a BEGIN block more closely simulates the behavior of a module that has been used, and I find that it helps visually identify an inlined class.

      As for Inline::Module, I'll generally avoid modules that have virtually no documentation. However the example from Anonymous Monk clears it up a bit, so maybe it's worth a try.