include file "inline"
rsiedl
created: 2006-03-06 04:13:55
Hey Monks,

Can anyone tell me if its possble to include a file "inline" in a perl script?
i.e.
#!/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 :)

Cheers,
Reagen
Re: include file "inline"
created: 2006-03-06 04:29:06

You're probably looking for something like do. But you'd almost be certainly be better off creating a "real" module.

--
<http://dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Re: include file "inline"
created: 2006-03-06 05:52:29
You can also make use of require. This does not make the methods / variables in there "inline", though. If you truly want to import methods / variables into your script go with davorg's advice.
Re: include file "inline"
created: 2006-03-06 08:36:10
Maybe you are looking for 'here doc's':
Re: include file "inline"
created: 2006-03-06 08:38:58
Inline::Files

I'm not really a human, but I play one on earth. flash japh
Re^2: include file "inline"
created: 2006-03-06 09:17:19
zentara,
The way I interpret the question, Inline::Files won't help. I believe rsiedl is asking for a way to inline code. Given the example provided, I think davorg is more on tract.

Cheers - L~R

Re: include file "inline"
created: 2006-03-06 12:36:17
If you can get past the caveats of source filtering then use Filter::Include to truly inline your code.
HTH

_________
broquaint

Re: include file "inline"
created: 2006-03-06 14:17:52

See this tutorial: Including files.

Update: see also How to include files in perl? and File inclusion.

Re: include file "inline"
created: 2006-03-06 14:28:57

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]

Re: include file "inline"
created: 2006-03-06 14:49:52

[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.

<-radiant.matrix->
A collection of thoughts and links from the minds of geeks
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed [http://en.wikipedia.org/wiki/Trebuchet|trebuchet]

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