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

Hi,

I'm trying to split a large chunk of code that uses shared variables and I can't get past the error "lock can only be used on shared values". Here's the reduced version of that code, in two files:

***mybase.pm:
package mybase; use threads; use threads::shared; my $mode : shared; 1;
***derived.pm:
#!/usr/bin/perl package derived; use strict; use Exporter; use threads; use threads::shared; use mybase; use vars qw(@ISA); @ISA = ('mybase'); sub foo { lock($mybase::mode); $mybase::mode = 1; } foo(); 1;
***
[root@nf-power2 perltest]# ./derived.pm lock can only be used on shared values at ./derived.pm line 12.

Any ideas on why a variable declared as shared is not appearing as a one?

Thanks,
b

Replies are listed 'Best First'.
Re: can't lock an object from a base class in a derived class
by Corion (Patriarch) on Jun 05, 2008 at 20:24 UTC

    You're using two different variables. In mybase you declare a lexical variable named $mode :

    my $mode : shared;

    ... but in the package derived, you try to access a global variable named $mode:

    $mybase::mode = 1;

    The two variables are different (and the lexical variable is unreachable from derived anyway) and hence Perl complains.