#!/usr/bin/perl use SOME::Module; my $blah = new SOME::Module; $blah->start; use 'guts.pl'; $blah->end; exit;and guts.pl might look like:
#!/usr/bin/perl
my %data = ( lots of data );
foreach (keys %data) {
$blah->calc($_);
}
exit;
I know this code is useless and wont work, it's just to help explain my question :)You're probably looking for something like do. But you'd almost be certainly be better off creating a "real" module.
"The first rule of Perl club is you do not talk about
Perl club."
-- Chip Salzenberg
Cheers - L~R
See this tutorial: Including files.
Update: see also How to include files in perl? and File inclusion.
Since everyone else has meantioned how to inline perl code, and seeing the use SOME::Module; and OOish construct, I'm assuming you might be asking how to create an OO module. If that's the case your half way there:
The calling perl file (ie. foo.pl)
#!/usr/bin/perl use warnings; use strict; @data = qw(gotta have guts to code perl); use Guts; my $Gs = Guts->new; $Gs->gutsy(@data);
The module (ie. Guts.pm):
pakage Guts;
sub new
{
my $class = shift;
my $self = {};
$self = bless($self, $class);
return $self;
}
sub gutsy
{
$self = shift;
print join(" ", @_);
}
1;
The key points being look at [doc://perltoot], [doc://bless], and [doc://perlobj]
[doc://do] works, to a point, as does [doc://require]. However, neither will let inline code see the surrounding scope. The only way to do this is with a fairly risky [doc://eval]:
use File::Slurp; #!important!
my $blah = new SOME::Module;
$blah->start;
eval read_file('guts.pl');
$blah->end;
This is not recommended, partly because it's slow. Eval can be risky, too, so read up on it before choosing something.
It's easy to build modules, so I strongly recommend that as an alternative course of action.
perlmonks.org content © perlmonks.org and ambrus, broquaint, davorg, jkva, kutsu, Limbic~Region, NiJo, radiantmatrix, rsiedl, zentara
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03