Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

include file "inline"

by rsiedl (Friar)
on Mar 06, 2006 at 09:13 UTC ( [id://534653]=perlquestion: print w/replies, xml ) Need Help??

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

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) { $main::blah->calc($_); } exit;
I know this code is useless and wont work, it's just to help explain my question :)

Cheers,
Reagen

Replies are listed 'Best First'.
Re: include file "inline"
by davorg (Chancellor) on Mar 06, 2006 at 09:29 UTC

    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"
by jkva (Chaplain) on Mar 06, 2006 at 10:52 UTC
    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"
by ambrus (Abbot) on Mar 06, 2006 at 19:17 UTC
Re: include file "inline"
by NiJo (Friar) on Mar 06, 2006 at 13:36 UTC
Re: include file "inline"
by broquaint (Abbot) on Mar 06, 2006 at 17:36 UTC
    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"
by radiantmatrix (Parson) on Mar 06, 2006 at 19:49 UTC

    do works, to a point, as does require. However, neither will let inline code see the surrounding scope. The only way to do this is with a fairly risky 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 trebuchet
Re: include file "inline"
by kutsu (Priest) on Mar 06, 2006 at 19:28 UTC

    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 perltoot, bless, and perlobj

Re: include file "inline"
by zentara (Archbishop) on Mar 06, 2006 at 13:38 UTC
      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://534653]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-26 03:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found