sabkat has asked for the wisdom of the Perl Monks concerning the following question:

I have some code in two parts: a pl file and a pm file. I'd like to put these into one pl file, with a minimum of rewriting. Is there some way to do this? I'm thinking of somthing like putting all your (as in c++) .h and .cpp files into one big file.

thanks for any help, links, pointers, etc...


Edited: ~Thu Oct 10 16:31:43 2002 (GMT) by footpad: Retitled (was "modules") and added basic HTML formatting, per Consideration

Replies are listed 'Best First'.
Re: Combining modules into a single file
by Ovid (Cardinal) on Oct 10, 2002 at 05:03 UTC

    Well, you could potentially do something like this:

    package Foo; sub bar { print "Bar\n" }; package main; Foo::bar();

    In other words, you could just cat the files together, add a package declaration before the the code in the main program and it should work with a bit of tweaking (for example, you want to take out use or require statements that pull in Foo).

    However, why do you want to do this? Maybe there's an easier way to solve your actual problem.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      I need to put but files into one for the (silly) reason of coding standards. One file is required, not two. I'm being lazy by not rewriting the files! Thanks for your help. That was exacly what I wanted! Happy Thanksgiving Weekend (in Canada)
Re: Combining modules into a single file
by submersible_toaster (Chaplain) on Oct 10, 2002 at 06:40 UTC
    Just stoking the fire here... I have tried something similar to this, several ways!

    Inserting your module code at the beginning is cool, but I found it aesthetically displeasing - and a nuisance to scroll through to get to the few lines of main code. I can't find the code anymore (blush) , but here's how I remember doing it, slurping everything in __DATA__ then an eval on that. No doubt other monks will be able to explain the pitfalls in doing this.. but it werked 4 me!!

    #!/usr/bin/perl -w use strict; my @code = <DATA>; my $code = join "" , @code; eval $code; my $stuff = Foo->new('RedCar'); print $stuff->{bar}; __DATA__ package Foo; sub new { my $self= shift; my $val = shift; my %hash =( bar=> $val);; return bless { %hash}, $self; }