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

my program is not executing properly... im using the require statement to call another file.
require otherfile; hello();
and in the otherfile
sub hello{ print "Hello"; }
when it executes everything comes up blank...doesnt execute would i need to package the otherfile? how would i do that? thx Zer

Edit davorg: added code tags

Replies are listed 'Best First'.
Re: using other files with program
by davorg (Chancellor) on Oct 30, 2001 at 19:44 UTC

    Two problems:

    1. When you give require a bareword, it assumes it's the name of a module and appends '.pm'. As your's is the full name of the file you need to quote it.
    2. A required file needs to return a true value. This is normally achieved by putting 1; as the last line of the file.

    Both of these errors give obvious error messages. Didn't you see them?

    It's well worth the time learning how to bundle this stuff u pinto a real module.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."

Re: using other files with program
by doc (Scribe) on Oct 30, 2001 at 20:16 UTC

    This works fine:

    # 'main script' require 'foo.pl'; foo(); # this is the content of foo.pl sub foo{ print"foo" } 1;

    Perl will look for 'foo.pl' in @INC which will usually contain these three directories:

    /Perl/lib 
    /Perl/site/lib
    .

    The . dir is the current dir. For more details on @INC see Simple Module Tutorial by tachyon Error messages are always useful when thing don't work. see also New Monks and the link on asking questions

    doc

    print(s<>ecode?scalar reverse :p)

Re: using other files with program
by traveler (Parson) on Oct 30, 2001 at 20:08 UTC
    If you don't put a newline (\n) after the Hello, some shells (e.g. most of the UNIX/Linux ones) will overwrite the Hello with the prompt hiding the result. Try using "Hello\n" and seeing if that helps.

    HTH, --traveler