<?xml version="1.0" encoding="windows-1252"?>
<node id="852765" title="Re: How to load the subs without running the script" created="2010-08-03 18:49:29" updated="2010-08-03 18:49:29">
<type id="11">
note</type>
<author id="414875">
snoopy</author>
<data>
<field name="doctext">
How about returning up front? Say for example, if monster.pl contains:
&lt;code&gt;
#!/usr/bin/perl

sub mysub {print "I want to see this\n"};

print "but I don't want to see this\n";

exit;  # don't want to exit either
&lt;/code&gt;
Then:
&lt;code&gt;
#!/usr/bin/perl
use warnings;
use strict;

my $prog_code = do {local $/ = undef;
		    open (my $fh, '&lt;', 'monster.pl')
			or die "open error: $!";
		    &lt;$fh&gt;};

eval 'return;'.$prog_code ;
die "eval error: $@" if $@; 

warn "trying mysub";
mysub();
&lt;/code&gt;

The return gets executed as soon as compilation has finished, avoiding execution of the main body:
&lt;code&gt;
perl do.pl
trying mysub at do.pl line 13.
I want to see this
Compilation finished at Wed Aug  4 10:08:26
&lt;/code&gt;
&lt;p&gt;
&lt;b&gt;Update:&lt;/b&gt; Removed intermediate subroutine and added sample output.</field>
<field name="root_node">
852746</field>
<field name="parent_node">
852746</field>
</data>
</node>
