Last active
August 29, 2015 13:56
-
-
Save ynonp/8931379 to your computer and use it in GitHub Desktop.
References examples and syntax
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 Critter; | |
my $c = Critter->new('joe', 19); | |
my $d = Critter->new('bob', 100); | |
my $e = Critter->new('jane', 12); | |
$c->eat(); | |
$c->eat(); | |
$d->eat(); | |
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
# Critter.pm | |
# | |
package Critter; | |
use strict; | |
use warnings; | |
use v5.14; | |
sub new { | |
my ( $cls, $name, $age ) = @_; | |
my $self = { | |
name => $name, | |
age => $age, | |
}; | |
bless $self, $cls; | |
} | |
sub eat { | |
my ( $self ) = @_; | |
my $name = $self->{name}; | |
say "$name: Yummy..."; | |
} | |
1; |
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.16; | |
# Notice we import the hit_aliens subroutine | |
# from Plants package | |
use Plants qw/hit_aliens/; | |
use Aliens; | |
# So now we can call it directly | |
hit_aliens(); | |
# Or using the full name | |
Plants::hit_aliens(); |
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
# Plants.pm | |
# Plants module example | |
package Plants; | |
use strict; | |
use warnings; | |
use v5.16; | |
use base 'Exporter'; | |
our @EXPORT_OK = qw/hit_aliens/; | |
sub secret_weapon { "plants win" } | |
sub hit_aliens { say secret_weapon() } | |
1; |
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.12; | |
my @l = (1, 2, 5, 9, 19); | |
my $l_ref = \@l; | |
say "list = ", @$l_ref; | |
push @$l_ref, 7; | |
my %h = ( | |
bob => '[email protected]', | |
tim => '[email protected]', | |
); | |
my $h_ref = \%h; | |
say $h_ref->{bob}; | |
my $sz = scalar keys %$h_ref; | |
sub hello { say "hello" } | |
my $hello_ref = \&hello; | |
$hello_ref->(); | |
$hello_ref->(7); | |
################################ | |
my $g_ref = [1, 2, 7]; | |
my @game = ( | |
[ ' ', 'x', 'o' ], | |
[ ' ', 'o', 'x' ], | |
[ ' ', 'x', 'x' ], | |
); | |
$game[2]->[0] = 'x'; | |
# same as ... | |
$game[2][0] = 'x'; | |
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.16; | |
use Test::More; | |
########################################### | |
my @contacts = ( | |
{ name => 'joe', email => '[email protected]', city => 'Haifa' }, | |
{ name => 'bob', email => '[email protected]', city => 'Yakum' }, | |
{ name => 'mike', email => '[email protected]', city => 'Jerusalem' }, | |
{ name => 'jane', email => '[email protected]', city => 'Haifa' }, | |
); | |
my @gmail = grep { $_->{email} =~ /gmail.com/ } @contacts; | |
grep { $_->{city} eq 'Haifa' } @contacts; | |
is_deeply( | |
\@gmail, | |
[ { name => 'joe', email => '[email protected]', city => 'Haifa' }, | |
{ name => 'bob', email => '[email protected]', city => 'Yakum' }, | |
]); | |
########################################### | |
sub sum { | |
my $sum = 0; | |
$sum += $_ for @_; | |
$sum; | |
} | |
sub diffsum { | |
my ( $l1_ref, $l2_ref ) = @_; | |
my @l1 = @$l1_ref; | |
my @l2 = @$l2_ref; | |
sum(@l1) - sum(@l2) | |
} | |
my @data = ( | |
[ [10, 8, 2], [ 10, 8, 1 ], 1 ], | |
[ [11, 2], [10 ], 3 ], | |
[ [1, 2, 3], [], 6 ], | |
); | |
for my $t_ref (@data) { | |
is( diffsum($t_ref->[0], $t_ref->[1]), $t_ref->[2] ); | |
} | |
sub add_to_hash { | |
my ( $h_ref, $k, $v ) = @_; | |
$h_ref->{$k} = $v; | |
} | |
my %data = ( a => 1, b => 2 ); | |
add_to_hash(\%data, c => 3); | |
is( $data{c}, 3, "Item 3 added to hash"); | |
done_testing; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment