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: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' );
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' );