Skip to content

Instantly share code, notes, and snippets.

View j1n3l0's full-sized avatar

Nelo Onyiah j1n3l0

  • Goznel Limited
  • Kent UK
  • 16:23 (UTC -12:00)
View GitHub Profile
# candidate for abs() in perl6 - just not sure where it should live
our Num multi method abs is export { self < 0 ?? self * -1 !! self }
# multi abs(Num $number) { $number < 0 ?? $number * -1 !! $number }
# How does one CAST from a Str to an Int in rakudo?
# This is an example going from an Int to a Str ...
multi sub f($x is rw) {
say 'before ' ~ $x ~ ' = ' ~ $x.WHAT;
$x = ~$x;
say 'after ' ~ $x ~ ' = ' ~ $x.WHAT;
}
@j1n3l0
j1n3l0 / gist:80450
Created March 17, 2009 10:34
Casting, Signatures & Return Types in rakudo
multi sub to_num(Str $number) returns Num { +$number }
my $s = "10";
my $i = to_num($s);
say 'before: ' ~ $s.WHAT; # before: Str
say 'after : ' ~ $i.WHAT; # after : Num
=begin pod
=item Casting
#!/usr/bin/env perl
use Test::More;
use MooseX::Types::Moose qw(Str);
# Why does this method return "undef"?
# Is that intentional (because it's rather counter-intuitive)?
ok not( Str->validate('An instance of Str') ), 'returns undef';
#!/usr/bin/env ruby
class Foo
attr_accessor :bar
def initialize
@bar = "bar"
yield( self ) if block_given?
return self
end
end
#!/usr/bin/env perl
use Modern::Perl;
use Test::Most;
{
package RestClient;
use Moose;
use Data::Dumper;
has url => (
#!/usr/bin/env perl
use REST::Client;
use Data::Dumper;
use JSON;
my $client =
REST::Client->new( { host => 'http://username:password@localhost:3000' } );
# Add header "Content-Type"
$client->addHeader( content_type => 'application/json' );
#!/usr/bin/env perl
use 5.10.0;
use warnings;
{
package Foo;
use Moose;
local $SIG{__WARN__} = sub { };
has private => ( isa => 'Int' );
use MooseX::Declare;
role Validation {
requires qw/ GET HEAD responseCode responseContent /;
after GET { confess 'URL does not exist' if $self->responseCode eq 404 };
};
class App with(Validation) {
use Moose::Util::TypeConstraints;
use REST::Client;