Skip to content

Instantly share code, notes, and snippets.

@ynonp
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ynonp/8931379 to your computer and use it in GitHub Desktop.
Save ynonp/8931379 to your computer and use it in GitHub Desktop.
References examples and syntax
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();
# 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;
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();
# 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;
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 => 'bob@gmail.com',
tim => 'tim@yahoo.com',
);
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';
use strict;
use warnings;
use v5.16;
use Test::More;
###########################################
my @contacts = (
{ name => 'joe', email => 'joe@gmail.com', city => 'Haifa' },
{ name => 'bob', email => 'bob@gmail.com', city => 'Yakum' },
{ name => 'mike', email => 'mike@yahoo.com', city => 'Jerusalem' },
{ name => 'jane', email => 'jane@walla.com', city => 'Haifa' },
);
my @gmail = grep { $_->{email} =~ /gmail.com/ } @contacts;
grep { $_->{city} eq 'Haifa' } @contacts;
is_deeply(
\@gmail,
[ { name => 'joe', email => 'joe@gmail.com', city => 'Haifa' },
{ name => 'bob', email => 'bob@gmail.com', 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