[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Updating a Tied Database



seanc@mediatek.co.za (Sean Carte) wrote:
>I'm trying to write a script that will change the value of a single 
>field of a record in a tied database.

[...]

>tie(%db, 'MLDBM', $dbFile, O_RDWR|O_CREAT, 0666)
>		or die("dbopen $dbFile: $!");
>
>$db{fruit} = ({
>	apple => "red",
>	orange => "orange",
>	banana => "yellow",
>	tomato => "red"
>});
>
>$db{fruit}{tomato} = 'green';
>
>print $db{fruit}{tomato};
>
>untie(%db);
>--
>This prints 'red'.


This is covered in the MLDBM manpage:

  1.  Adding or altering substructures to a hash value is not entirely
      transparent in current perl.  If you want to store a reference or
      modify an existing reference value in the DBM, it must first be
      retrieved and stored in a temporary variable for further modifications.
      In particular, something like this will NOT work properly:

              $mldb{key}{subkey}[3] = 'stuff';        # won't work

      Instead, that must be written as:



              $tmp = $mldb{key};                      # retrieve value
              $tmp->{subkey}[3] = 'stuff';
              $mldb{key} = $tmp;                      # store value

      This limitation exists because the perl TIEHASH interface currently has
      no support for multidimensional ties.




# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org