-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added AsIf::Perl and module ecosystem stuff
- Loading branch information
Showing
5 changed files
with
80 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,8 @@ | |||
|
|||
module Masquerade; | |||
|
|||
use Masquerade::AsIf::JSON; | |||
use Masquerade::AsIf::Perl; | |||
|
|||
|
|||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,20 @@ | |||
# JSON::Tiny is used to render basic stuff like hashes. | |||
use JSON::Tiny; | |||
|
|||
role AsIf::Perl; | |||
|
|||
# Tries to interpret the thing you're doing as a JSON object string. | |||
# Obviously this won't work if it's not one, so don't do that. | |||
multi method postcircumfix:<{ }> (Str $str) { | |||
my %hash = from-json(self); | |||
return %hash{$str}; | |||
} | |||
|
|||
# Tries to interpret the thing you're doing as a JSON array string. | |||
# Obviously this won't work if it's not a JSON string, so don't do that. | |||
multi method postcircumfix:<[ ]> (Int $num) { | |||
my $array = from-json(self); | |||
return $array[$num]; | |||
} | |||
|
|||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,12 @@ | |||
use v6; | |||
use Test; | |||
plan 2; | |||
|
|||
# Load the module | |||
use Masquerade; | |||
|
|||
# At this point this is pretty simple: just take a json object and turn it | |||
# into a corresponding perl6 object. | |||
ok (('{"a":"foo"}' but AsIf::Perl)<a> eq 'foo'), "can access object elements"; | |||
ok (('["a","b","c"]' but AsIf::Perl)[0] eq 'a'), "can access array elements"; | |||
|