Last active
December 18, 2015 15:19
-
-
Save ynonp/5803711 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
use strict; | |
use warnings; | |
use v5.14; | |
package Contacts; | |
sub init { | |
my %data; | |
return \%data; | |
} | |
sub add_contact { | |
my ( $db_ref, $name, $info_ref ) = @_; | |
$db_ref->{$name} = $info_ref; | |
} | |
sub contacts_by_country { | |
my ( $db_ref, $country ) = @_; | |
grep { $db_ref->{$_}->{lives_in} eq $country } keys %$db_ref; | |
} | |
1; | |
=pod | |
=head1 NAME | |
Contacts is a module for managing contacts | |
=head1 SYNOPSIS | |
use v5.14; | |
use warnings; | |
use Contacts; | |
my $home_book = Contacts::init(); | |
my $work_book = Contacts::init(); | |
Contacts::add_contact( $home_book, 'Tom', { lives_in => 'USA', email => '[email protected]' }); | |
Contacts::add_contact( $home_book, 'Bob', { lives_in => 'USA', email => '[email protected]' }); | |
Contacts::add_contact( $work_book, 'Mike', { lives_in => 'Mars', email => '[email protected]' }); | |
# prints Tom, Bob | |
print Contacts::contacts_by_country( $home_book, 'USA' ); | |
=head1 DESCRIPTION | |
A long description of the module | |
=over | |
=item add_contact | |
add a new contact | |
=item init | |
return a new address book | |
=back | |
This file contains hidden or 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
use strict; | |
use warnings; | |
use v5.14; | |
use List::Util qw/first/; | |
my %deps; | |
sub build { | |
my ( $next ) = @_; | |
delete $deps{$next}; | |
while ( my ( $from, $to_ref ) = each %deps ) { | |
$deps{$from} = [ grep { $_ ne $next } @$to_ref ]; | |
} | |
} | |
sub get_next { | |
first { @{ $deps{$_} } == 0 } keys %deps; | |
} | |
sub add_dependency { | |
my ( $from, $to ) = @_; | |
$deps{$from} ||= []; | |
$deps{$to} ||= []; | |
push @{ $deps{$from} }, $to; | |
} | |
while(<DATA>) { | |
chomp; | |
my ( $from, $to ) = split /[ ]*:[ ]*/; | |
next if ! $from; | |
my @to = split /[ ]*,[ ]*/, $to; | |
add_dependency( $from, $_) for @to; | |
} | |
print "-------->\n"; | |
while ( my $next = get_next() ) { | |
print "$next "; | |
build( $next ); | |
} | |
print "\n"; | |
print "-------->\n"; | |
if ( 1 > 7 ) { | |
use Hello; | |
} | |
__DATA__ | |
Luke: Han Solo, Leia, Yoda | |
Leia: Padme Amidala, Anakin Skywalker | |
Obi-Wan: Yoda | |
Darth Vader: Anakin Skywalker | |
This file contains hidden or 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
use strict; | |
use warnings; | |
use List::Util qw/sum/; | |
use Data::Dumper; | |
sub add_to_hash { | |
my ( $h_ref, $key, $value ) = @_; | |
$h_ref->{$key} = $value; | |
} | |
sub diff_sum { | |
my ( $l1_ref, $l2_ref ) = @_; | |
my $sum1 = sum( @$l1_ref ); | |
my $sum2 = sum( @$l2_ref ); | |
return $sum2 - $sum1; | |
} | |
my $l1_ref = [ 10, 20 ]; | |
my $l2_ref = [10, 30]; | |
print diff_sum( $l1_ref, $l2_ref ), "\n"; | |
my $h_ref = { | |
a => 10, | |
b => 20, | |
foo => [20, 30] | |
}; | |
add_to_hash( $h_ref, c => 30 ); | |
while ( my ( $k, $v ) = each %$h_ref ) { | |
print "k = $k, v = $v\n"; | |
} | |
print Dumper( $h_ref ); |
This file contains hidden or 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
use strict; | |
use warnings; | |
use Data::Dumper; | |
my %db; | |
while(<>) { | |
my ( $key, $value ) = /(\w+) *= *(\w+)/; | |
next if ! $key; | |
$db{$ARGV}{$key} = $value; | |
} | |
print Dumper( \%db ); | |
print grep { $db{$_}{age} } keys %db; | |
This file contains hidden or 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
use strict; | |
use warnings; | |
use v5.14; | |
package Person; | |
use Mouse; | |
use Carp; | |
has 'name', is => 'ro', required => 1; | |
has 'hobbies', is => 'rw'; | |
sub hello { | |
my ( $self, $times ) = @_; | |
croak "MISSING TIMES" if ! $times; | |
for (1..$times) { | |
say "Hi! My name is ", $self->name; | |
} | |
} | |
package main; | |
my $p1 = Person->new( name => 'Bob' ); | |
my $p2 = Person->new( name => 'Jim' ); | |
$p1->hello( 5 ); | |
$p2->hello(); | |
$p1->hobbies('Skiing and Diving'); | |
say $p1->hobbies; | |
This file contains hidden or 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
use strict; | |
use warnings; | |
use v5.14; | |
package Module; | |
use Mouse; | |
has 'name', is => 'ro', required => 1, isa => 'Str'; | |
has 'built', is => 'rw', isa => 'Bool', default => 0; | |
has 'dependencies' => ( | |
is => 'rw', | |
isa => 'ArrayRef[Module]', | |
default => sub {[]}, | |
); | |
sub add_dependency { | |
my ( $self, $other ) = @_; | |
my @deps = @{ $self->dependencies }; | |
push @deps, $other; | |
$self->dependencies( \@deps ); | |
} | |
sub build { | |
my ( $self ) = @_; | |
return if $self->built; | |
$_->build for @{$self->dependencies}; | |
say $self->name; | |
$self->built( 1 ); | |
} | |
package ModuleRegistry; | |
use Mouse; | |
has 'modules', is => 'ro', isa => 'HashRef[Module]', default => sub {{}}; | |
sub get { | |
my ( $self, $name ) = @_; | |
$self->modules->{$name} ||= Module->new( name => $name ); | |
} | |
package main; | |
my $stash = ModuleRegistry->new; | |
my $root = Module->new( name => '>>>>> All Done <<<<<<' ); | |
while(<DATA>) { | |
chomp; | |
my ( $from, $to ) = split /\s*:\s*/; | |
next if ! $from; | |
$from = $stash->get( $from ); | |
my @to = map { $stash->get($_) } split /\s*,\s*/, $to; | |
$from->add_dependency( $_ ) for @to; | |
$root->add_dependency( $from ); | |
} | |
$root->build; | |
__DATA__ | |
Luke: Han Solo, Leia, Yoda | |
Leia: Padme Amidala, Anakin Skywalker | |
Obi-Wan: Yoda | |
Darth Vader: Anakin Skywalker | |
This file contains hidden or 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
my @people = ( | |
{ name => 'Tim', age => 17, email => '[email protected]' }, | |
{ name => 'Bob', age => 22, email => '[email protected]' }, | |
{ name => 'Jim', age => 43, email => '[email protected]' }, | |
{ | |
name => 'mike', | |
hobbies => ["skiing", "diving"] | |
} | |
); | |
my @elder = grep { $_->{age} > 20 } @people; | |
my @gmail = grep { $_->{email} =~ /gmail.com$/ } @people; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment