The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Petal::Hash - Magical hash for Petal objects

SYNOPSIS

  my $object = new My::WonderfulObject;
  my $hash   = new Petal::Hash (
    object => $object,
    time   => time()
  );

  # invokes $object->some_method();
  $hash->{'object.some_method'};

  # invokes $object->other_method ('foo', 'bar');
  $hash->{'object.other_method foo bar'};

  # invokes $object->other_method ($hash->{'time'});
  $hash->{'object.other_method $time'}

  # is $object->foo() true?
  $hash->{'true:foo'}

  # is $object->foo() false?
  $hash->{'false:foo'}

  # finally, these two lines are *EXACTLY* the same:
  $hash->{foo} = $hash->{'object.bar'};
  $hash->{'set:foo object.bar'};

DESCRIPTION

Petal::Hash turns a hash into a much more powerful black magic powered structure which can invoke object methods, encode the method results to XML, etc. etc. using the various Petal::Hash::* modules.

EXTENDING

Let's say you want to build a modifier that returns the length of a string, so that you could do:

  $hash->{string} = 'foo';
  print $hash->{'length:foo'};

You would write the following module:

  package MyMod::Length;

  sub process
  {
      my $class = shift;
      my $hash  = shift;
      my $argument = shift;
      return length ($hash->fetch ($argument));
  }

And at the beginning of your program do:

  $Petal::Hash::MODIFIERS->{'length:'} = 'MyMod::Length';

And that's it! Easy, huh?

AUTHOR

Jean-Michel Hiver <jhiver@mkdoc.com>

This module is redistributed under the same license as Perl itself.

SEE ALSO

The standard modifier modules:

  Petal::Hash::VAR
  Petal::Hash::SET
  Petal::Hash::TRUE
  Petal::Hash::FALSE