Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Problem with calling method new

by Sinister (Friar)
on Sep 21, 2001 at 01:46 UTC ( [id://113715]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,
I'm writing a module, which calls upon a base class like this:
package Voice::Calender; use strict; use base qw(Voice); sub new { my ( $invocant, @jada ) = @_; my $class = ref($invocant) || $invocant; my $obj = { }; bless $obj, $class; }
Now when I call in a perl script this new method like this:
use strict; use Voice::Calendar; my $voice = Voice::Calendar->new();
I keep on getting this error: Can't call method "new" without a package or object reference at /www/voice/docs/cgi/calendar.cgi line 45, <PIPE> line 8 What could be my problem here?

Sinister greetings.
"With tying hashes you can do everything God and Larry have forbidden" -- Johan Vromans - YAPC::Europe 2001
perldoc -q $_

Replies are listed 'Best First'.
Re: Problem with calling method new
by chromatic (Archbishop) on Sep 21, 2001 at 02:01 UTC
    Spell "Calendar" consistently. :)

    Update: Seriously, there's a typo somewhere. The only way you get that message is if Perl can't resolve what's on the left side of a method call to a package name or an object.

Re: Problem with calling method new
by jlongino (Parson) on Sep 21, 2001 at 02:04 UTC
    I assume you typo'd package Voice::Calender in the first snippet, but for others trying to cut/paste, it might cause a problem.

    Yes, I know this is probably not the cause of the error you are getting. But it might help if others could see what line 8 of your code is. I assume it is the my $voice = Voice::Calendar->new(); statement.

    @a=split??,'just lose the ego and get involved!';
    for(split??,'afqtw{|~'){print $a[ord($_)-97]}
      The typo's are here only - sorry typed to fast...

      Line 8 is indeed not what it seems, huh? I extracted the code out of my original script and it is indeed the invocing line...

      I tried numerous things and it is bugging me out. Voice::Calendar is in @INC... - I am clueless!

      Sinister greetings.
      "With tying hashes you can do everything God and Larry have forbidden" -- Johan Vromans - YAPC::Europe 2001
      perldoc -q $_
        I don't know much about OOP, but aren't you missing the return statement in your sub new()?

        Update: chromatic explained to me that the constructor returns the value of the last expression. (the bless. Like I said, I don't know much, OOPS! :)

        @a=split??,'just lose the ego and get involved!';
        for(split??,'afqtw{|~'){print $a[ord($_)-97]}
Re: Problem with calling method new
by converter (Priest) on Sep 21, 2001 at 07:44 UTC

    Quoth the Camel (3ed, p927):

    You used the syntax of a method call, but the slot filled by the objec +t reference or package name contains an expression that returns a def +ined value that is neither an object reference nor a package name. So +mething like this will reproduce the error: $BADREF = 42; process $BADREF 1,2,3; $BADREF->process(1,2,3);

    To make a long story short, you probably have a package named Voice, with a method named Calendar(). Instead of a class method invocation, you're calling &Voice::Calendar(), which returns something that is a true value, but not an object reference or package. Example:

    $ perl -e 'package Foo::Bar; sub Foo {1}; sub Foo::Bar {1}; package ma +in; Foo::Bar->Foo' Can't call method "Foo" without a package or object reference at -e li +ne 1.

    To prevent this confusion, you can quote the package name:

    'Foo::Bar'->Foo

    conv

    Update: Of course there's a package named Voice, I should read more carefully.

    For fun: You could also have made the Calendar() method in the Voice package return 'Voice::Calendar', which Perl would take as the package name, but that would probably break other stuff. (:

      Quoting the package name did the trick!

      Thanks, ever so much!
      IOU a beer now!
      I would have never found out myself!

      Sinister greetings.
      "With tying hashes you can do everything God and Larry have forbidden" -- Johan Vromans - YAPC::Europe 2001
      perldoc -q $_
Re: Problem with calling method new
by dragonchild (Archbishop) on Sep 21, 2001 at 02:01 UTC
    Is the directory that contains Voice::Calendar within @INC?

    ------
    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.

Re: Problem with calling method new
by perrin (Chancellor) on Sep 21, 2001 at 02:07 UTC
    You have a typo in your package statement.
Re: Problem with calling method new
by Sweeper (Pilgrim) on Sep 21, 2001 at 09:26 UTC
    Either you correct your typo, or you use Dave Cross's module Symbol::Approx::Sub :-)

    And you could also read his speech at YAPC::Europe-2001 (Perl for the people).

    BTW, he appears in perlmonks as davorg.

      Heh! Thanks for the plug :)

      The Perl For The People talk slides are online and you might also want to read a counter-argument.

      Oh, and there was an article about Symbol::Approx::Sub in the last TPJ.

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you don't talk about Perl club."

Re: Problem with calling method new
by mitd (Curate) on Sep 21, 2001 at 05:13 UTC
    To make it so perl can locate that pesky package.
    1. Put your cgi script and your package files into your cgi-bin directory or if you can make a 'lib' dir off your cgi-bin dir and put your package(s) in there.
    2. Add the following to your script code.
      use FindBin qw($Bin); use lib $Bin; or use lib "$Bin/lib";

    mitd-Made in the Dark
    'My favourite colour appears to be grey.'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://113715]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-19 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found