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

Hi there ! I am trying to write a simple program to add some messages to an MBOX. Here is my code:
use strict; use English; use warnings; use Mail::Box::Mbox; my $foldername = 'test.mbox'; my $folder = Mail::Box::Mbox->create($foldername, access => 'rw'); my $msg; my $n =4; while ($n--) { my $msg = Mail::Box::Message->build(From => 'me', data => "Message $n +\n only two\nlines\n---\n"); $msg->print; $folder->addMessage( $msg ); }
Here is the error I get:
Can't use string ("Mail::Box::Mbox") as a HASH ref while "strict refs" + in use at I:/Perl/site/lib/Mail/Box.pm line 816.
Thanks !

Replies are listed 'Best First'.
Re: Can't use string ("MODULE") as a HASH ref while "strict refs" in use
by Joost (Canon) on Aug 10, 2004 at 18:04 UTC
    Line 816 of Mail/Box.pm reads:
    $self->{MB_message_type}->coerce($message);
    running the code in the debugger reveals that this is called at the $folder->addMessage($msg); line.

    the problem is that create() does not return an object, (even though the docs claim it's a constructor).

    To work around the problem:

    Mail::Box::Mbox->create($foldername, access => 'rw'); my $folder = Mail::Box::Mbox->new(folder => $foldername);
      The method documentation was in the wrong section. For the next release, it will move from "Constructors" to "Internals". The right way to open a file, and create it if is doesn't exist yet is very file-open like:
       my $mbox = Mail::Box::Mbox->new(folder => $f,
         access => 'rw', create => 1);
      
      Besides, if you create a message, you always create a Mail::Message, not some Mail::Box::Message, because Mail::Box::Message have some knowledge about location in them, where the message you have created has none yet. It gets a location at $f->addMessage($msg), and will change type at that moment (in this case into a Mail::Box::Mbox::Message).
      Joost, Your solution works. Thanks ! mm.
Re: Can't use string ("MODULE") as a HASH ref while "strict refs" in use
by Aristotle (Chancellor) on Aug 10, 2004 at 17:58 UTC

    Guessing from the Mail::Box docs, it seems you want to instantiate a Mail::Message, not a Mail::Box::Message.

    Makeshifts last the longest.

Re: Can't use string ("MODULE") as a HASH ref while "strict refs" in use
by dragonchild (Archbishop) on Aug 10, 2004 at 17:18 UTC
    1. Look at the docs for Mail::Box and Mail::Box::Mbox. Maybe you're messing up?
    2. Look at the code at the line specified in the file specified. Maybe you're messing up and the docs are wrong?
    3. Create a very simple test case (similar to the on you have) and email it to the author. Be very careful to specify your OS and Perl version. Most bugs in CPAN modules are actually OS differences.

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

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested