localize values of an object's hash key

J

Joly, Patrick: IAC

I need to localise (dynamically scope) the value of a hash key in an
object but I am facing a hurdle. I have been stuck on this problem for
5 days so any help would be greatly appreciated.

First, some background: my module is a parser to read binary files from
a database/statistics software. (See below for what a typical object
looks like.) The source data contains mnemonics for each variable in a
database and my methods allow one to perform operations by invoking just
those mnemonic names, e.g.

$oo->read('c:\auto2.dta'); # see below for example of data
$oo->generate('newvar', 'mpg + 2')
$oo->replace('mpg', 'mpg/100')

I wrote a method to return a mnemonic name guaranteed to be unique and
want to subsquently use this name in any method to perform operations on
this temporary hash key. That is,

$tempname = $oo->tempvar() # generate mnemonic guaranteed to be
# unique, i.e. generally returns that
# look like '__000001'

followed by,

local $oo->{DATA}{"$tempname"};
$oo->any_method( ... );

would allows the user to have access to dynamically scoped values for
the temporary hash key (all methods called within that scope have access
to it) but where it would vanish once its localized values go out of
scope (since it wasn't defined in the first place); Obviously, I don't
want users to access by refering to the key explicitely. I initially
thought I could modify tempvar() to return the literal string
'$oo->{DATA}{__000001}' rather than simply '__000001' but local() won't
accept any subsequent

local $tempname;
or
local (eval $tempname);

Thus I am looking for other solutions and am wondering:

1) is there any way in Perl I can declare variables (or hash keys in
this instance) in the scope of the *calling* program rather than in the
current scope. That way, I could ask tempvar() to localize the hash key
entry for '__000001'; or,

2) any other ideas?


Thanks, I am really desperate. Everything was going great so far, until
I got stumped by this.


Patrick Joly, Economist
Industry Canada


A typical object for my module (Stata::Data)
--------------------------------------------
Once dumped using Data::Dumper, it looks like,


$VAR1 = bless( {
'LABEL' => 'Automobile Data',
'SORTEDBY' => [
'mpg',
'turn'
],
<snip>
'TIMESTAMP' => ' 2 Jul 2003 11:45',
'FNAME' => 'c:\\auto2.dta',
'DATA' => {
'mpg' => {
'FORMAT' => '%8.0g',
'CELLS' => [
14,
14,
15,
16,
],
'LABEL' => 'Mileage (mpg)',
'TYPE' => 'int'
}
'make' => {
'FORMAT' => '%-18s',
'CELLS' => [
'Toyota Echo',
'Nissan Sentra',
'Ford Focus',
'Honda Civic',
],
'LABEL' => 'Make and Model',
'TYPE' => 'str13'
}
<snip>
}
}, 'Stata::Data' );
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I need to localise (dynamically scope) the value of a hash key in an
object but I am facing a hurdle. I have been stuck on this problem
for 5 days so any help would be greatly appreciated.

2) any other ideas?


Thanks, I am really desperate. Everything was going great so far,
until I got stumped by this.

I'm not sure this is a *great* solution, but I've used this technique in
the past:

Have your tempvar() method return an object in a different class.
Foo::TempValue or something. This object will have a reference to the
object (of type Stata::Data) that created it, so that it can communicate
with it. When the object goes out of scope, its DESTROY method invokes
a method on the "parent" (or "factory") object to tell it that its value
is no longer to be used.

The user of this object can access its value in one of several ways.
You could make them call a method:

$foo = $data_obj->tempvar;
print "I'm using ", $foo->value, "!!!";

Or you the child object could override stringification, so that

print "I'm using $foo!!!"
would work, but
print "I'm using ", $foo, "!!!";
would not.

Or you could return the child object as a tied scalar, so that the
calling user never need know it's an object.

- - --
Eric
$_ = reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBPxyMkGPeouIeTNHoEQJOMQCeLplZNFBe04RARq85HuvD/UzezqEAn0D8
rbDjMLiUFjQN5HONw3JQzUeh
=EBBA
-----END PGP SIGNATURE-----
 
J

John Porter

I need to localise (dynamically scope) the value of a hash key in an
object...

$tempname = $oo->tempvar();

followed by,

local $oo->{DATA}{"$tempname"};
$oo->any_method( ... );

How about:

{
local $oo->{'access_key'} = $oo->tempvar;
$oo->any_method(...);
}

In other words, have a slot in the object for the value of the
temporary access key. This you can localize directly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,839
Latest member
MartinaBur

Latest Threads

Top