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

To make a module return a value in a conditional, would I do something like this?:

package checkStuff; sub new { $bar=2; bless $bar; return $bar; }

This is only for example, my actual module code is a fair bit more complicated. The main question is, will that pass $bar back to the script using the module, so that the script can use it too?

Replies are listed 'Best First'.
Re: Modules Returning Values?
by Zaxo (Archbishop) on Aug 31, 2004 at 21:22 UTC

    Beware, you can't bless just any old scalar value: it must be a reference you bless. Also, return is not really necessary unless you are returning from somewhere in the middle of the function's code.

    Is your new() a class constructor method? If so, you should consider writing it as,

    packsge CheckStuff; sub new { my $class = shift; my $bar = 2; bless \$bar, $class; }
    to be called by, my $cs = CheckStuff->new(); That avoids the surprises you will get by having $bar a package global. You would find that with two CheckStuff objects around, their values would both be the same; whatever was set last. It also makes the constructor inheritable, so that if CheckStuff::Rigid has CheckStuff as a base class, my $csr = CheckStuff::Rigid->new(); always returns the right sort of object.

    After Compline,
    Zaxo

      Beware, you can't bless just any old scalar value: it must be a reference you bless.

      What if you threaten the perl with the Holy Hand Grenade of Antioch? ;-)

      A Reading from the Book of Armaments, Chapter 4, Verses 16 to 20: Then did he raise on high the Holy Hand Grenade of Antioch, saying, "Bless this, O Lord, that with it thou mayst blow thine enemies to tiny bits, in thy mercy." And the people did rejoice and did feast upon the lambs and toads and tree-sloths and fruit-bats and orangutans and breakfast cereals ... Now did the Lord say, "First thou pullest the Holy Pin. Then thou must count to three. Three shall be the number of the counting and the number of the counting shall be three. Four shalt thou not count, neither shalt thou count two, excepting that thou then proceedeth to three. Five is right out. Once the number three, being the number of the counting, be reached, then lobbest thou the Holy Hand Grenade in the direction of thine foe, who, being naughty in my sight, shall snuff it." -- Monty Python, "Monty Python and the Holy Grail"

      Jason L. Froebe

      No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

Re: Modules Returning Values?
by reneeb (Chaplain) on Aug 31, 2004 at 20:47 UTC
    It will pass the value of $bar to the script...

    use mypackage; my $value = mypackage::some_sub(); print $value; # output: 2


    and your package:
    package mypackage; sub some_sub{ my $var = 2; return $var; }
      Alright, thanks.
Re: Modules Returning Values?
by davido (Cardinal) on Sep 01, 2004 at 04:48 UTC

    Generally the return value of the new() method of a module is the blessed reference to an object, which is an instance of the class its blessed into. Example:

    package checkStuff; sub new { my $class = shift; my $bar = 2; bless \$bar, $class; return \$bar; # This line is redundant. } 1; package main; use strict; use warnings; my $obj = checkStuff->new(); print $obj, "\n"; print ref $obj, "\n"; __OUTPUT__ checkStuff=SCALAR(0x155ae0c) checkStuff

    $obj is essentially your handle to the object instance created when you blessed $bar into class checkStuff. If you return something else instead of the object ref, you won't have access to the object anymore.

    You can, however, return more than one thing via new() In the following example, a list is returned. The first element is the object reference. The second element is the value of $bar.

    package checkStuff; sub new { my $class = shift; my $bar = 2; bless \$bar, $class; return ( \$bar, $bar ); # returning a list. } 1; package main; use strict; use warnings; my( $obj, $val ) = checkStuff->new(); print $obj, "\n"; print ref $obj, "\n"; print $val, "\n"; __OUTPUT__ checkStuff=SCALAR(0x155ae0c) checkStuff 2

    What we've done here is defined new() to be both a constructor, and an accessor to $bar's value. This works fine, but isn't really convenient for repeated use, since you probably don't want to have to call $obj->new() each time you want to know what $bar holds, and

    For that reason, accessors are usually defined in separate subs. For example, you could define within checkStuff a sub called getbar(). That sub would look like this:

    sub getbar { my $self = shift; return ${$self}; }

    ...or more concisely...

    sub getbar { return ${$_[0]}; }

    Dave

Re: Modules Returning Values?
by herveus (Prior) on Sep 01, 2004 at 11:12 UTC
    Howdy!

    You ask about "modules" returning values. Modules don't return anything. Subs return stuff.

    Yeah, I'm being pedantic, but it's important to use the right terms. That makes it clearer to all what you are asking, and might even make the answer obvious to yourself.

    Taking your question literally, the answer is "no" and "mu" (the canonical answer to "have you stopped beating your wife?"). Substitute "sub" or "subroutine" or "function" for "module" and you have a much better worded question.

    ...but this quibble wasn't anywhere near worth a downvote from me...

    yours,
    Michael