Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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:

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

Comment on require and its subs
Select or Download Code
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 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?
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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2014-10-23 01:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (123 votes), past polls