in reply to Aliasing hash-element and scalar?

Sorry it's confusing, I'll try again.

I managed to alias a scalar to one hash-element.

But can I alias hash-elements from different hashes to the same scalar?

i.e. when I change $x it mirrors in $h{x} and $g{x} and vice versa?

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

  • Comment on Re: Aliasing hash-element and scalar? (Not Solved Yet)

Replies are listed 'Best First'.
Re^2: Aliasing hash-element and scalar? (Not Solved Yet)
by Corion (Patriarch) on Sep 03, 2018 at 19:03 UTC

    I'm not really sure that Perl syntax allows you to alias hash entry scalars, but you can achieve the effect by using Data::Alias:

    use Data::Alias; use feature 'say'; $baz{'buf'} = 'Hello'; alias $foo{'bar'} = $baz{'buf'}; say $foo{'bar'}; $foo{'bar'} = 'World'; say $baz{'buf'}; $baz{'new'} = 'old'; say $foo{'old'}; __END__ Hello World Use of uninitialized value in say at ...
      Thanks.

      Background: I was thinking to emulate the Python OO system, and in that language a class attribute can be accessed thru the same mechanism like an instance attribute.

      Hence assigning self.class_attr will change the value for all objects, assigning to self.obj_attr only to that specific object.

      Data::Alias seems like a good idea, unless Python has optional magic __hooks__ to intercept a get/set on class_attr.

      In that case I should rather use Tie::Scalar.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice