localize values of an object's hash key

Discussion in 'Perl Misc' started by Joly, Patrick: IAC, Jul 21, 2003.

  1. 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' );
    Joly, Patrick: IAC, Jul 21, 2003
    #1
    1. Advertising

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

    "Joly, Patrick: IAC" <> wrote in
    news::

    > 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-----
    Eric J. Roode, Jul 22, 2003
    #2
    1. Advertising

  3. Joly, Patrick: IAC

    Malte Ubl Guest

    Eric J. Roode wrote:

    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > "Joly, Patrick: IAC" <> wrote in
    > news::
    >
    >
    >>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/proxyobject.html?page=1

    bye
    malte
    Malte Ubl, Jul 22, 2003
    #3
  4. Joly, Patrick: IAC

    John Porter Guest

    "Joly, Patrick: IAC" <> 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
    John Porter, Jul 22, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MattB

    Localize one control only

    MattB, May 9, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    371
    MattB
    May 10, 2005
  2. walter
    Replies:
    0
    Views:
    713
    walter
    Mar 22, 2006
  3. Trapulo

    Localize themed label

    Trapulo, May 3, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    397
    Steven Cheng[MSFT]
    May 4, 2006
  4. rp
    Replies:
    1
    Views:
    517
    red floyd
    Nov 10, 2011
  5. Une bévue
    Replies:
    5
    Views:
    149
    Une bévue
    Aug 10, 2006
Loading...

Share This Page