Skip to content

Instantly share code, notes, and snippets.

@seamountain
Created April 20, 2012 08:00
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 seamountain/2427005 to your computer and use it in GitHub Desktop.
Save seamountain/2427005 to your computer and use it in GitHub Desktop.
Teach Yourself Perl 2nd Edition
#!/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);
#!/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";
}
# 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/*";
#!/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);
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