Created
September 4, 2012 08:02
-
-
Save ynonp/3618287 to your computer and use it in GitHub Desktop.
Advanced Perl day1
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 v5.14; | |
use warnings; | |
use Data::Dumper; | |
my $people = []; | |
sub add_new_person { | |
my ( $name, $work, $tagline ) = @_; | |
push $people, { | |
name => $name, | |
work => $work, | |
tagline => $tagline, | |
hobbies => [qw/fishing cooking/], | |
} | |
} | |
add_new_person( 'Homer', 'Factory', 'Doh'); | |
add_new_person( 'Jimmy', 'Jim', "If it's not fun..."); | |
print Dumper($people); | |
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 v5.14; | |
use warnings; | |
use List::Util qw/sum/; | |
use Data::Dumper; | |
sub diff_sum { | |
my ( $l1_ref, $l2_ref ) = @_; | |
my $sum1 = sum ( @$l1_ref ); | |
my $sum2 = sum ( @$l2_ref ); | |
return $sum2 - $sum1; | |
} | |
sub add_to_hash { | |
my ( $h_ref, $key, $value ) = @_; | |
$h_ref->{$key} = $value; | |
} | |
my @l1 = (1, 2, 3, 4); | |
my @l2 = (2, 3); | |
my $diff = diff_sum( \@l1, \@l2 ); | |
say "diff = $diff"; | |
my %family = ( | |
bart => 'yellow', | |
homer => 'yellow', | |
marge => 'blue', | |
); | |
say Dumper( \%family ); | |
say "---- add ---"; | |
add_to_hash( \%family, 'Lisa', 'saxophone'); | |
say Dumper( \%family ); | |
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 v5.14; | |
use Carp; | |
use Data::Dumper; | |
use warnings; | |
use autodie; | |
my %db; | |
open my $modules, '<', 'modules'; | |
while (<$modules>) { | |
chomp; | |
my ($module, $deps) = split /\s*:\s*/; | |
my @deps = split /\s*,\s*/, $deps; | |
$db{$module} = []; | |
foreach my $m (@deps) { | |
push $db{$module}, $m; | |
$db{$m} ||= []; | |
# same as: | |
#if ( ! $db{$m}) { | |
#$db{$m} = []; | |
#} | |
} | |
} | |
close $modules; | |
print Dumper(\%db); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment