Created
April 20, 2012 08:00
-
-
Save seamountain/2427005 to your computer and use it in GitHub Desktop.
Teach Yourself Perl 2nd Edition
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
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
my @day = qw(Sun Mon Tue Wed Thu Sat); | |
my @mon = qw(Jun Feb Mar Apl May Jun Jul Aug Sep Oco Nov Dec); | |
my($sec, $min, $hour, $mday, $mon, $year, $wday) = localtime; | |
printf("Today is %s, %s %s, %d", $mon[$mon], $day[$wday], $mday, $year += 1900); |
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
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use IO::Dir; | |
my $dir_fh = IO::Dir->new('.') || die "could not open dirhandle! $!"; | |
while( defined( my $file = $dir_fh->read) ) { | |
print $file; | |
my $fh = IO::File->new($file, "r"); | |
while (<$fh>) { | |
print; | |
} | |
print "\n"; | |
} |
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
# alpaca | |
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
my @sorted = sort { -s $a <=> -s $b } glob "/bin/*"; | |
print @sorted; | |
# schwartzian | |
my @sorted_s = | |
map $_ -> [0], | |
sort { $a->[1] <=> $b->[1] } | |
map [ $_, get_size($_) ], | |
glob "/bin/*"; | |
sub get_size { | |
my ($file) = $_; | |
return -s $file; | |
} | |
print "\n"; | |
print @sorted_s; | |
# answer | |
my @sorted_s = | |
map $_ -> [0], | |
sort { $a->[1] <=> $b->[1] } | |
map [ $_, -s $_ ], | |
glob "/bin/*"; | |
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
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
my $words = "ab cd ef gh ij kl mn"; | |
print $& . "\n" while ($words =~ /[a-zA-Z]+/g); |
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 @list = (1, 3, 4, 6, 9); | |
for (1..10) { | |
my ($num) = $_; | |
if (grep {$_ == $num} @list) { | |
print $_; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment