Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

require and its subs

by Anonymous Monk
on Nov 22, 2004 at 14:11 UTC ( #409591=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:


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?


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 ./ line 95.

Any idea?

Thanks in advance

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
      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. 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 was
    Name "main::DEF" used only once: possible typo at 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?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://409591]
Approved by Happy-the-monk
and a moth chases the moon...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (12)
As of 2018-06-22 19:00 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.