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

I'm using Win32::MMF::Shareable 0.9 under ActiveState Perl 5.8.3, and the documented ability to select the namespace at 'tie' time does not appear to work. Is this a bug in Win32:MMF or am I somehow barking up the wrong tree?

In short, when using the following syntax:

use Win32::MMF::Shareable; my $Var2; tie $Var2, "Win32::MMF::Shareable", "Var2", { namespace => 'test' };

The namespace will be "shareable" (the default namespace), not "test". This appears to contradict the "Description" section of Win32::MMF::Shareable, which states:

Note that if you try to tie a variable without specifying the namespace, the default namespace 'shareable' will be used. If you want to change how the default namespace is created, provide the namespace, swapfile and size options when you tie the first variable.

 use Win32::MMF::Shareable;
 tie $scalar, "Win32::MMF::Shareable", "var_1",
              { namespace = 'MyNamespace', size = 1024 * 1024,
                swapfile = 'C:\private.swp' };

I have thrown together simple example programs that demonstrate this (read more below). Can anyone find anything I'm doing wrong? Alternately, does anyone know what the OO-interface equivalent of "push @tiedMMFvariable, $var;" would be, since choosing a namespace after "use" time seems to work better with OO?

Here are two "master" programs and three "client" programs:

If I run shared_master_use.pl in one window:

C:\ctl\mmf>shared_master_use.pl
Var2 is "Kenneth, what's my namespace?"
  sleeps for 5 minutes to hold Var2 in memory... 

Then I run the client programs:

C:\ctl\mmf>shared_client_use.pl
Var2 is "Kenneth, what's my namespace?"

C:\ctl\mmf>shared_client_tie.pl
Var2 is ""

C:\ctl\mmf>shared_client_use_shareable.pl
Var2 is ""

Thus we see that, when we specify namespace at 'use' time, it is set to what we specify and not the default 'shareable'. Now let's run the other master program, which specifies namespace at 'tie' time:

C:\ctl\mmf>shared_master_tie.pl
Var2 is "Kenneth, what's my namespace?"
  sleeps for 5 minutes to hold Var2 in memory... 

and I get the opposite results from my clients:

C:\ctl\mmf>shared_client_use.pl
Var2 is ""

C:\ctl\mmf>shared_client_tie.pl
Var2 is "Kenneth, what's my namespace?"

C:\ctl\mmf>shared_client_use_shareable.pl
Var2 is "Kenneth, what's my namespace?"

Here's the code to the various programs:

shared_master_use.pl
#!perl use Win32::MMF::Shareable { namespace => 'test' }; my $Var2; tie $Var2, "Win32::MMF::Shareable", "Var2"; $Var2 = "Kenneth, what's my namespace?"; print "Var2 is \"$Var2\"\n"; sleep 300;

shared_master_tie.pl
#!perl use Win32::MMF::Shareable; my $Var2; tie $Var2, "Win32::MMF::Shareable", "Var2", { namespace => 'test' }; $Var2 = "Kenneth, what's my namespace?"; print "Var2 is \"$Var2\"\n"; sleep 300;

shared_client_use.pl
#!perl use Win32::MMF::Shareable {namespace => "test"}; my $Var2; tie $Var2, "Win32::MMF::Shareable", "Var2"; print "Var2 is \"$Var2\"\n";

shared_client_tie.pl
#!perl use Win32::MMF::Shareable; my $Var2; tie $Var2, "Win32::MMF::Shareable", "Var2", {namespace => "test"}; print "Var2 is \"$Var2\"\n";

shared_client_use_shareable.pl
#!perl use Win32::MMF::Shareable {namespace => "shareable"}; my $Var2; tie $Var2, "Win32::MMF::Shareable", "Var2"; print "Var2 is \"$Var2\"\n";

Replies are listed 'Best First'.
Re: Win32::MMF::Shareable tie namespaces don't seem to work?
by meetraz (Hermit) on May 28, 2004 at 20:42 UTC
    I read through the source code for this module... There are at least two bugs I can see where any parameters you pass in the call to "tie" will not be used.

    You should contact the author and let him know what is happening.

    I think pushing onto a shared array would have to be done like this:

    if ($ns->lock()) { my $var1 = $ns->getvar('var1'); push (@$var1, 'another item'); push (@$var1, 'yet another'); $ns->setvar('var1', $var1); $ns->unlock(); }

    However, that seems to trigger a bug somewhere too.

    FWIW, you should be able to use the "reuse => 1" parameter to warn yourself of the problem you were having (creating a new namespace under a different name when you meant to open an existing namespace)

Re: Win32::MMF::Shareable tie namespaces don't seem to work?
by Roger (Parson) on May 29, 2004 at 00:30 UTC
    The namespace naming issue has been fixed. I have released the new version 0.09e to CPAN.