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

I've been digging around, but I've been unable to find a solution for my specific situation. Of course it occurs to me that I may be approaching this incorrectly from the start...

In a nutshell...

package Foo::Bar; use base 'Foo'; sub foosub { my $self = shift; my $FooObj = Some::Module->new; ... elsewhere ... barsub( \$FooObj ); ... more code ... } sub barsub { # now, how do I access $FooObj # from here? my $FooObj = shift; # ??? $FooObj->some_method; # results in: Can't call method "some_method" on # unblessed reference at ... }

I'm not sure what to do from here. This will give me an error about $FooObj being an unblessed reference in the barsub routine. If I add bless $FooObj; to the barsub routine, it then errors out with Can't locate object method "some_method".... If I try barsub( $FooObj ) without the \ backslash it doesn't make much difference.

Any advice or suggestions would be appreciated. Thanx!

--
"This alcoholism thing, I think it's just clever propaganda produced by people who want you to buy more bottled water." -- pedestrianwolf

Replies are listed 'Best First'.
Re: Object passing within a package
by Mutant (Priest) on Jan 17, 2005 at 15:00 UTC

    You shouldn't need to pass $FooObj as a reference to barsub() (it should already be a reference).

    Within barsub(), try doing this:

    warn ref $FooObj;

    (Or print if STDERR isn't convenient) and make sure Some::Module->new() is returning a blessed reference.

      Thanx for the tip. That got me going in the right direction. It shed some light on yet another thing that I was doing wrong... doh!

      Thanx!

      --
      "This alcoholism thing, I think it's just clever propaganda produced by people who want you to buy more bottled water." -- pedestrianwolf

Re: Object passing within a package
by Arunbear (Prior) on Jan 17, 2005 at 15:05 UTC
    $FooObj is already a reference, so you shouldn't have to call barsub with a reference to it.

    What error message did you get when you tried barsub( $FooObj )? Maybe you need to check that it is defined before passing it to barsub :

    my $FooObj = Some::Module->new or die '$FooObj should not be undef';

Re: Object passing within a package
by dragonchild (Archbishop) on Jan 17, 2005 at 15:02 UTC
    Try barsub( $FooObj );

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

        You're assuming that barsub() is a method within the Some::Module package. It could just be a regular function that takes, as its first argument, a Some::Module object or a reference to a data structure that $FooObj is blessed into.

        The moral of the story is that TMTOWTDI really means "Assuming will make an ASS out of U and ME."

        Being right, does not endow the right to be rude; politeness costs nothing.
        Being unknowing, is not the same as being stupid.
        Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
        Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Object passing within a package
by brian_d_foy (Abbot) on Jan 17, 2005 at 15:58 UTC

    This situation looks a bit odd. Why are you creating another object inside a method call and using that as an argument to another method?

    You don't need to create a reference to your $FooObj reference, so you can lose the backslash in front of it when you call barsub. If you are seeing odd behaviour, ensure $FooObj is actually an object.

    package Foo::Bar; use base 'Foo'; sub foosub { my $self = shift; my $FooObj = Some::Module->new; die "Not an object!" unless ref $FooObj eq 'Some::Module'; $self->barsub( $FooObj ); } sub barsub { my $self = shift; my $FooObj = shift; ... }
    --
    brian d foy <bdfoy@cpan.org>

      Yes, your code is pretty much identical to what I ended up using to get past that particular problem.

      Specifically, what I'm trying to write is a Curses::UI interface to a sort of psuedo-autodiscovery script. I was attempting to figure out how to pass the Curses::UI objects around without declaring full-blown globals. I'm also trying to write it in such a way that it could potentially interface with a different autodiscover script.

      So essentially I have MyModule, MyModule::Interface::CLI, and MyModule::Autodiscover. I wanted to contain all the C::UI stuff in M::I::CLI, which is why I was trying to pass those C::UI objects to different subs within the M::I::CLI package. If I'm poorly reinventing a wheel, I certainly welcome input.

      And yes, I do spend too much time monkeying with RT.

      --
      "This alcoholism thing, I think it's just clever propaganda produced by people who want you to buy more bottled water." -- pedestrianwolf

        Hmmm... if all things need to access the Curses::UI object, you might consider creating giving each of their objects a reference to the Curses::UI object. It should be the same object (so you don't have multiple ones), but then every time you call a method, you already have it in the object data. In that case you don't need to pass it around.

        Sometimes the Singleton design pattern can solve this little sanfu, too. There is a Perl Singletons on Perl Monks, or my article about Singletons in The Perl Review, Issue 0.1

        --
        brian d foy <bdfoy@cpan.org>
      This situation looks a bit odd. Why are you creating another object inside a method call and using that as an argument to another method?

      First, why must barsub() be a method? Why isn't it a function?

      I can think of at least two reasons to do this:

      1. barsub() is a utility function defined to do something useful. barsub() may actually be _barsub().
      2. barsub() is a utility function that comes from some other library of functions.

      As for why foosub() instantiates the Some::Module object then passes it to barsub(), you could have foosub2() that instantiates an object from Other::Module and passes it to barsub().

      You just never know.

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

        In the example, it looks like barsub() in that same package as foosub(), which is a method. If barsub() has nothing to do with that same package or object, it's in the wrong place. If the situation is different, then the example code has way too much detail, and we don't need to know anything about anything else than barsub().

        I don't know what the original poster is doing, but it looks like an odd design. That's why I asked. I'm not trying to guess what the he is actually doing. I'm just asking why he's doing it that way, and I'll wait to see what he says, and then I'll know (which is sooner than never ;).

        --
        brian d foy <bdfoy@cpan.org>