Giter Club home page Giter Club logo

masquerade's Introduction

Masquerade

Let Perl 6 constructs masquerade as other things.

Overview

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).

Examples

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 %.properties;
  has @.affects;
};

my $tornado = Weather::Phenomenon.new(
  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.

===AsIf::YAML

Let's say you have some objects and want to render them as YAML. No problem-- just say them, but AsIf::YAML.

use Masquerade;

my %tornado = { 
  size        => "medium",
  damage      => "high",
  affects     => [<Houses Barns Cars Chickens Cows>],
  attributes  => {
    twistiness  => 100,
    color       => 'brown/grey',
    scariness   => 6,
    speed       => {
      velocity  => '92mph',
      direction => 'at YOU!',
    }   
  },  
};

say %tornado but AsIf::YAML;

This produces the following output:

size:       medium
damage:     high
affects:    
  - Houses
  - Barns
  - Cars
  - Chickens
  - Cows
attributes: 
  twistiness: 100
  color:      brown/grey
  scariness:  6
  speed:      
    velocity:  92mph
    direction: at YOU!

In addition to perl's built-in data structures, you can also do a rendering of custom objects.

use Masquerade;

class TestClass {
  has $.animal = "monkey";
  has $.money  = "dollar";
  has @.veggies;
  has %.cities;
};

my $test = TestClass.new(
  veggies => <zucchini squash broccoli>,
  cities  => {
    London => 'England',
    Durham => 'The United States of America',
  }   
);  

say $test but AsIf::YAML;

This produces the following output:

animal:  monkey
money:   dollar
veggies: 
  - zucchini
  - squash
  - broccoli
cities:  
  London: England
  Durham: The United States of America

masquerade's People

Contributors

sirrobert avatar zoffixznet avatar samcv avatar

Stargazers

Michal Jurosz avatar

Watchers

 avatar James Cloos avatar

masquerade's Issues

Module will not install

Output from last installation attempt:

$ zef install .
===> Testing: Masquerade:auth<Sir Robert Burbridge>
Could not find symbol '&JSON'
  in sub test_json at t/asif-json.t line 15
  in block <unit> at t/asif-json.t line 26

Could not find symbol '&Perl'
  in block <unit> at t/asif-perl6.t line 10

===SORRY!===
Undeclared routine:
    eq used at line 94

Other potential difficulties:
    Useless use of hash composer on right side of hash assignment; did you mean := instead?
    at /home/cbwood/Projects/unbitrot/Masquerade/t/asif-yaml.t:90
    ------> }โ<EOL>

===> Testing [FAIL]: Masquerade:auth<Sir Robert Burbridge>
Aborting due to test failure: Masquerade:auth<Sir Robert Burbridge> (use --force-test to override)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.