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

Hello,

I'm new to perl so this may be an easy answer.

I am trying to move some common code into a file called stylesheet.pl. Then from my other pl files I want to "require" that file. All files are located in the same directory. However, when I attempt to run the file that requires stylesheet.pl I get the following error:

stylesheet.pl did not return a true value at addfather.pl line 4.

The line I used to "require" this file is here:

#!/usr/bin/perl use CGI; require "stylesheet.pl"; $objCGI = new CGI;

Any clues as to what is going on are appreciated. Thanks.

Just,
Kurious

Replies are listed 'Best First'.
Re: require problems
by Shendal (Hermit) on Sep 19, 2000 at 21:23 UTC
    From the require perldoc page:
    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.
    So, add this to the your stylesheet.pl file:
    1; # return a one

    Cheers,
    Shendal
        Well, it's true in Boolean context but 0 in numeric context. Looking through the docs on fcntl, it looks like it's useful for the Perl calls that are wrappers to C system calls.

        For example, if fcntl returns -1 to signify failure, Perl will massage that into undef. If it returns 0, Perl will use "0 but true" -- which means you can use it either numerically (C-style, expecting 0 to correspond to success) or Booleanifically (Perl-style, expecting 'true' to mean success).

        Close enough?

        my $true = "0 but true"; print "$true\n" if $true == 0;

        Or perhaps even more devilish:

        # return true 0 . 0;

        Well, I fail to see how the string "0 but true" is any more special than "any true value will do".

        They're both simply strings that interpolate in a boolean context to true

        how about:

        0 . "true";

        for something ever so slightly more "evil"...

        (c8=

        My current fave is:

        # grr... "Why should version 5.6 make me do this in a module?"

        Of course I like the capital letter "O"; too =)

        --
        $you = new YOU;
        honk() if $you->love(perl)

      Excellent! Thank you. That was the problem. Just, Kurious
Re: require problems
by japhy (Canon) on Sep 19, 2000 at 22:01 UTC
    If you want evil return values: ( ) = 0;

    Try that on for size. It's true. Isn't that criminal? :)

    $_="goto+F.print+chop;\n=yhpaj";F1:eval

      Well, not really *that* criminal --

      it's a non-empty list...

      <INCORRECT>
      lists in scalar/boolean context return the number of their elements.
      </INCORRECT>;

      Update: I humbly acknowledge the folly of the above statement.

      Should be:

      list assignment returns the number of elements.

      End Update

      Thus:

      perl -e '$val = () = 0; print $val';

      prints 1 -- true!

        lists in scalar/boolean context return the number of their elements.
        Oh. Once again, trying to dispel the myth of that statement. Repeat after me:
        • You can never have a list in a scalar context...
        • You can never have a list in a scalar context...
        • You can never have a list in a scalar context...
        Now, what's really happening is that you have a list assignment operator in a scalar context, which is defined as having a return value of the number of elements copied across, while giving a list context to the right side.

        In this case, the right side is a simple expression, which gets turned into a list of one element. So we get a "1" for the return value.

        Once more, just so you remember:

        • You can never have a list in a scalar context...

        -- Randal L. Schwartz, Perl hacker

        No, that's not true. Lists do not exist in scalar or boolean context.
        $a = (3,2,1); print $a; # 1
        Read my article ("List is a Four-Letter Word") at my web site for stuff I'd rather not rewrite here.
        perl -e '$val = () = (10,20,30); print $val'
        prints 3.

        $_="goto+F.print+chop;\n=yhpaj";F1:eval
        What these two gurus are trying to say is that there is a HUGE difference between lists and arrays. Please don't confuse them.

        An array in scalar context returns the number of elements therein.

Re: require problems
by mirod (Canon) on Sep 19, 2000 at 21:26 UTC

    Did you read the error message?

    stylesheet.pl did not return a true value at addfather.pl line 4.

    The problem is not in the require, it's in stylesheet.pl. Just add a 1 or return 1 in stylesheet.pl, at the end of any code that is executed at compile time. That would typically be before you start defining subroutines.