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

I'm trying to invoke a subroutine from a module that I had made. But I keep on getting the error:

Can't use string ("SessionTool") as a HASH ref while "strict refs" in use at SessionTool.pm line 17

How can I create an OOP module that allows me to invoke a method directly without creating the object?
(i.e.)
my $foo = Foo->new(); object created
my $value = Foo->baz("FooBar") baz is a method

Thank you for enlightenment my brothers,
BMaximus

Replies are listed 'Best First'.
Re (tilly) 1: OOP question
by tilly (Archbishop) on Mar 24, 2001 at 05:45 UTC
    The name of the package the method was called for is always the first argument. If you don't care about that, just shift away one argument.

    But if you are trying to define functions in one package and access in another without creating objects, then OO is probably not how you want to do it. Instead look into Exporter to export the functions to the package they are used in. The basic template for a procedural module that I use looks like this:

    package Demo; use Exporter; @ISA = 'Exporter'; @EXPORT_OK = qw(list @things %that can $be exported); use strict; use vars ($whatever %needed @variables); # Nifty stuff here 1;
    Then when you pull it in do a use and pass the list of things you want to import:
    use Demo qw(list @things);
Re: OOP question
by merlyn (Sage) on Mar 24, 2001 at 05:48 UTC
    This smells of trying to say something like:
    sub baz { my $self = shift; $self->{smiley} += 25; .. }
    when called as a class method. When you're called as a class method (without an object), the value of $self is no longer a hashref: it's just a string. You'll need to store "class instance" variables instead of "instance" variables. See Damian's excellent book on this.

    -- Randal L. Schwartz, Perl hacker