Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > localize values of an object's hash key

Reply
Thread Tools

localize values of an object's hash key

 
 
Joly, Patrick: IAC
Guest
Posts: n/a
 
      07-21-2003
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:ata)
--------------------------------------------
Once dumped using Data:umper, 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:ata' );


 
Reply With Quote
 
 
 
 
Eric J. Roode
Guest
Posts: n/a
 
      07-22-2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

"Joly, Patrick: IAC" <(E-Mail Removed)> wrote in
news:3E3612E8D41DD3118DCC0600000000000AEA96CB@pubg ate-01.ic.gc.ca:

> 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:ata) 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-----
 
Reply With Quote
 
 
 
 
Malte Ubl
Guest
Posts: n/a
 
      07-22-2003
Eric J. Roode wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> "Joly, Patrick: IAC" <(E-Mail Removed)> wrote in
> news:3E3612E8D41DD3118DCC0600000000000AEA96CB@pubg ate-01.ic.gc.ca:
>
>
>>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:


It works for breaking circular references, too and is described here in
detail:
http://www.perl.com/pub/a/2002/08/07...ct.html?page=1

bye
malte

 
Reply With Quote
 
John Porter
Guest
Posts: n/a
 
      07-22-2003
"Joly, Patrick: IAC" <(E-Mail Removed)> wrote:
> 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.

--
John Douglas Porter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
hash key to var name of value hash key value Une bévue Ruby 5 08-10-2006 04:05 PM
Localize global resource using SqlResourceProviderFactory walter ASP .Net 0 03-22-2006 03:35 PM
Localize one control only MattB ASP .Net 2 05-10-2005 03:21 PM



Advertisments