Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
126 lines (83 sloc) 2.59 KB


Let Perl 6 constructs masquerade as other things.


Sometimes you want to be able to use Perl 6 objects as if they were something else, such as JSON. Masquerade allows for quick, painless (but simple) re-renderings.

AsIf::* Masquerade provides a set of roles. Each role allows objects to masquerade as if other objects. The roles do 'not' have the Masquerade prefix. For example, if you

use Masquerade;

you will get a role called AsIf::JSON (rather than Masquerade::AsIf::JSON).


Sometimes you have objects you want to render differently.

"Primitives" (hashes, arrays, strings, etc.)

Let's look at how we could use Masquerade to emit a json rendering of a hash containing an array.

use Masquerade;

my %agenda = {
  parties => 2,
  meetings => 3,
  color-choices => <red blue green>,

say %agenda but AsIf::JSON;
# output:
# { "parties" : 2, "meetings" : 3, "color-choices" : [ "red", "blue", "green" ] }

Class-based objects

That's well and good, but what about more complex stuff? The following example shows how to render a class instance as if it were JSON.

use Masquerade;

# Try a more complex perl object.
class Weather::Phenomenon {
  has $!id;
  has $.name;
  has $.description;
  has @.affects;

my $tornado =
  id          => 123,
  name        => 'tornado',
  description => 'a twister!',
  properties  => {
    twistiness  => 100,
    windiness   => 88, 
    size        => 40, 
  affects     => <Houses Barns Couches Chickens>

say $tornado but AsIf::JSON;

# output (note that it won't actually be pretty-printed; that's just for illustrative purposes here).
# { 
#   "name" : "tornado",
#   "description" : "a twister!",
#   "properties" : { 
#     "twistiness" : 100, 
#     "windiness" : 88,
#     "size" : 40 
#   }, 
#   "affects" : [ "Houses", "Barns", "Couches", "Chickens" ]
# }

Notice that the private property $!id is not rendered in the JSON. The JSON is considered to be a JSON rendering of the public interface to the object.

On the other hand, sometimes you want to go the other way. You can also extract useful bits out of things (only JSON is supported right now) as if they were Perl.

The following examples let you access JSON strings as if it were Perl:

use Masquerade;

my $json = '{"foo": "bar"}';
say ($json but AsIf::Perl)<foo>;  # bar
use Masquerade;

my $json = '[12.4, "pickle"]';
say ($json but AsIf::Perl)[1];  # pickle

These are read-only operations right now.

Something went wrong with that request. Please try again.