http://www.perlmonks.org?node_id=409591

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

Hi,

I'm successfully loading 'StringDef.req' (it's fist line ist printed, and I can access its vars) which contains vars, def and functions dealing with the vars
but the sub mkDec() hereafter is not found?

the check.pl:

require 'StringDef.req'; ok ? .. my $def = $DEF{$itm}; # ok .. mkDec($Sym, \@qts); # fails :(
StringDef.req looks like:
print "I'm loaded\n"; # ok %DEF = ( .. ); # ok .. sub mkDec { # not ok ???? ... }
This causes:
Undefined subroutine &main::mkDec called at ./check.pl line 95.

Any idea?

Thanks in advance
Carl

Replies are listed 'Best First'.
Re: require and its subs
by borisz (Canon) on Nov 22, 2004 at 14:24 UTC
    Try do instead of require.
    do 'StringDef.req';
    See perldoc -f do
    Boris
      Thanks, that (do) worked :) Carl
Re: require and its subs
by Roy Johnson (Monsignor) on Nov 22, 2004 at 14:50 UTC
    Did you end your required file with a non-zero value? According to perldoc -f require:
    The file must return true as the last statement to indicate successful execution of any initialization code, so it's customary to end such a file with "1;" unless you're sure it'll return true otherwise. But it's better just to put the "1;", in case you add more statements.
    I whipped up a couple of files to test, and had no problem. try.pl looked like this:
    use strict; use warnings; require 'try.req'; my $def = $::DEF{'two'}; mkDec($def);
    and try.req looked like this:
    print "I'm loaded\n"; %DEF = (one => 1, two => 2); sub mkDec{ print "MkDec received ($_[0]) OK\n"; } 1;
    Output from running try.pl was
    Name "main::DEF" used only once: possible typo at try.pl line 7 I'm loaded MkDec received (2) OK

    Caution: Contents may have been coded under pressure.
Re: require and its subs
by matija (Priest) on Nov 22, 2004 at 14:33 UTC
    Why aren't you using use StringDef; instead? use is equivalent to
    BEGIN { require Module; import Module LIST; }
    so maybe you need to add an import to your script?