#!/usr/bin/perl
use strict;
use warnings;
my $qidx = shift @ARGV; # start from 0
my $tgtfn = shift @ARGV;
open(my $fh, "<", $tgtfn) or die;
while (<$fh>) {
next if $. != $qidx + 1;
print;
last;
}
close $fh;
% cat test.dic Japan Tokyo Yokohama This is a pen Hello World % ./getline-naive.pl 2 test.dic Yokohama
#!/usr/bin/perl
use strict;
use warnings;
my $ip = 0;
while (<>) {
print pack("N", $ip);
$ip += length($_);
}
% ./mkidx.pl test.dic > test.dic.ary % od -t x1 test.dic.ary 0000000 00 00 00 00 00 00 00 06 00 00 00 0c 00 00 00 15 0000020 00 00 00 23 0000024
#!/usr/bin/perl
use strict;
use warnings;
my $len_of_N = 4;
my $qidx = shift @ARGV; # start from 0
my $tgtfn = shift @ARGV;
my $idxfn = shift @ARGV || $tgtfn.".ary";
my $tfsz = -s $tgtfn;
my $ifsz = -s $idxfn;
die if $qidx < 0 or $qidx * $len_of_N >= $ifsz;
my $buf = "";
open(my $fi, "<", $idxfn) or die;
seek $fi, $qidx * $len_of_N, 0;
read $fi, $buf, $len_of_N;
my $ixf = unpack("N", $buf);
my $ixt = (tell $fi < $ifsz) ? do {
read $fi, $buf, $len_of_N;
unpack("N", $buf);
} : $tfsz;
close $fi;
open(my $fh, "<", $tgtfn) or die;
seek $fh, $ixf, 0;
read $fh, $buf, $ixt - $ixf;
close $fh;
print $buf;
% ./getline.pl 2 test.dic Yokohama

人気の投資信託にはプロの仕掛けた巧妙なワナがたくさん隠されている。本書では、広告例を通じて「投信のワナ」の見破り方を解説し、その後に、数少ない良質な投信の使い方をやさしく解説した。