in reply to accessing subs/methods from the module

I am not sure I understood your question, if you simply need to call another class' method, or another package subroutine, or it makes sense to create a Foobar instance just to call another class' method...

Anyway, if you just want to know if it is possible to call another class' method without instanciating it, or another package's subroutine, the reply is:

# Access a Foobar's method Foobar->method(@parameters) ; # Access a Foobar subroutine Foobar::subroutine(@parameters) ;

Be warned that in Perl subroutines and methods are the same thing, but calling Foobar->method(@parameters) is not the same as calling &Foobar::subroutine(@parameters).

Ciao!
--bronto

Update: (after shrubbery's explaination): ok, I understand now:

steves & C. already replied: it is possible, but you should use package or global variables, which should be avoided if possible.


The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
--John M. Dlugosz

Replies are listed 'Best First'.
Re: Re: accessing subs/methods from the module
by shrubbery (Acolyte) on Feb 06, 2003 at 19:00 UTC
    Right sorry if my thing is a bit convoluted. Let me try again. I have two objects using 2 different classes. One is say called $f thats an instance of class "Foobar." The other is $b of class "Blah." They're both modules.

    My question would be, can I inside the module for "Blah" access a variable in the main program? That variable also happens to be $f, an instance of "Foobar."

    Let me fix my example too.

    main code:

    use Blah; use Foobar; $b = Blah->new(); $f = Foobar->new(); $b->doSomething;

    and inside the Blah module:

    sub new { ... } sub doSomething { $f->methodX(); }

    And inside the Foobar module:

    sub new { ... } sub methodX { does some fun things here }

    So basically, method "doSomething" is invoked. I want to be able for "doSomething" to access variable in the main program $f. $f just happens to be an instance of "Foobar."

    Thanks!

      I think it's very simple:

      if you change in the main program,

      $b->doSomething;
      to
      $b->doSomething($f);
      and
      package Blah; sub doSomething { my $self = shift; my $f = shift; $f->methodX; } ...
      That should work for you.

      artist

      It's bad style, but yes:

      package Blah; sub doSomething { $main::f->whatever(); }
      As long as $f is a package variable in main -- i.e., not declared with my, my preference being:
      our $f = Foobar->new();
      ... and as long as I don't have to maintain your code. You have factoring issues here that should be keeping you awake at night.

        Great thanks!

        Not really up to me, I thought it wasn't the best in design either. However, it does beat some other alternatives we thought up.

      $b->doSomething($f); package Blah; sub doSomething { my $self = shift; my ($f) = @_; $f->methodX(); }
      The real question is how do I use a variable in one scope within another scope. The answer is you pass it. This isn't an OO question, it's a scoping question. It's also a pretty basic question.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.