Skip to content

Instantly share code, notes, and snippets.

@timo
Created September 30, 2018 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timo/b3853b19c19419ba9630f583f02824bf to your computer and use it in GitHub Desktop.
Save timo/b3853b19c19419ba9630f583f02824bf to your computer and use it in GitHub Desktop.
#perl6 use a hash as if it were an object
unit class HashyObject;
has %!storage;
submethod TWEAK(*%data) { %!storage = %data }
submethod FALLBACK($name, |c) is rw { %!storage{$name} };
method perl {
self.^name ~ ".new(" ~ %!storage.pairs.map(*.perl).join(", ") ~ ")"
}
use HashyObject;
my HashyObject $a .= new(cookie => "keks");
$a.hello = "grüße";
$a.goodbye = "auf wiedersehen";
dd $a;
# OUTPUT:
# HashyObject $a = HashyObject.new(:hello("grüße"), :goodbye("auf wiedersehen"), :cookie("keks"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment