Sym2Name.pm

filter: Statementstmt 79.6% 51 / 64 Branchbran 66.6% 8 / 12 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 90.0% 9 / 10 Podpod 33.3% 1 / 3 totaltotal 78.7% 74 / 94 CC 11 SCAR 17.0
CC
11
Cov
78%
sym2name
10.2
fetch
3.0
full2half
2.0
CRAP 5.5
↑ Summary Build.pl »
1 package Finance::YahooJPN::Sym2Name;
2
3 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 1 our $VERSION = '0.04'; # 2011-06-14 (since 2002-03-26)
9 1 our @ISA = qw(Exporter);
10 1 our @EXPORT = qw(sym2name);
11
12 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
15
16 1 my $Server = 'stocks.finance.yahoo.co.jp';
17
18 =head1 NAME
19
20 Finance::YahooJPN::Sym2Name - converts a Japanese stock symbol to the name
21
22 =head1 SYNOPSIS
23
24 use Finance::YahooJPN::Sym2Name;
25
26 # get the name of company of stock symbol code '6758'
27 my $stockname = sym2name('6758');
28
29 print $stockname; # it prints 'SONY CORPORATION'
30
31 =head1 DESCRIPTION
32
33 This module converts a Japanese stock symbol code to the name of company. Japanese stock markets use 4-digit code number as stock symbol. You can get either English or Japanese name of company.
34
35 =head1 FUNCTIONS
36
37 =over
38
39 =item sym2name($symbol [, $lang])
40
41 This function returns a string of the name of company from C<$symbol> (a stock symbol code of 4-digit number).
42
43 In case it were a missing code number, the module returns string '(n/a)';
44
45 C<$lang> attribute specifies language mode of the company's name. A C<$lang> value is 'eng' (in English) or 'jpn' (in Japanese). This attribute is omittable and the default value is 'eng'.
46
47 Note that string data under 'jpn' C<$lang> mode is encoded with UTF-8 character encoding.
48
49 =cut
50
51 4 ▶sub sym2name ($;$) {
Subroutine sym2name: called CC=9 SCAR=23.2
Pod documented
52 4 my($symbol, $lang) = @_;
53
54 4 ▶ unless ($symbol =~ /^\d{4}$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($symbol =~ /^\d{4}$/) 0 4 4
55 0 croak "stock symbol code must be specified with 4-digit code number";
56 }
57
58 4 my $url = "http://stocks.finance.yahoo.co.jp/stocks/profile/?code=$symbol";
59 4 my @html = fetch($url);
60
61 # in case it were a missing code number
62 4 foreach my $line (@html) {
63 664 ▶ if ($line =~ m/一致する銘柄は見つかりませんでした/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\x{4e00}\x{81f4}\x{3059}\x{308b}\x{9298}\x{67c4}\x{306f}\x{898b}\x{3064}\x{304b}\x{308a}\x{307e}\x{305b}\x{3093}\x{3067}\x{3057}\x{305f}/) 0 664 664
64 0 return '(n/a)';
65 }
66 }
67
68 4 my %name;
69 # find and extract the name of company in Japanese
70 4 foreach my $line (@html) {
71 664 $line =~ m/<title>(.+?)【$symbol】.*?<\/title>/;
72 664 utf8::decode($name{'jpn'} = $1);
73 }
74 4 $name{'jpn'} =~ s/\(株\)//;
75 4 $name{'jpn'} =~ s/^\s*//;
76 4 $name{'jpn'} =~ s/\s*$//;
77
78 # find and extract the name of company in English
79 4 ▶ for (my $i = 0; $i < @html; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @html) 664 4 668
80 664 ▶ if ($html[$i] eq '<th nowrap>英文社名</th>') {
Branch: 1 branch
BranchTrueFalseTotal
if ($html[$i] eq "<th nowrap>\x{82f1}\x{6587}\x{793e}\x{540d}</th>") 0 664 664
81 0 $html[$i + 1] =~ m/<td colspan="3">(.+?)<\/td>/;
82 0 $name{'eng'} = $1;
83 0 $name{'eng'} = full2half($name{'eng'});
84 0 last;
85 }
86 }
87
88 4 ▶ if ($lang and lc($lang) eq 'jpn') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang and lc $lang eq 'jpn') { } 1 3 4
Condition100%3/3
$lang and lc $lang eq "jpn"
!ll&&!rl&&r
211
MC/DC100%2/2
$lang and lc $lang eq "jpn"
$lang lc $lang eq "jpn"
Truth table100%3/3
A and B
A= $langB= lc $lang eq "jpn"
ABresult
0X0
100
111
89 1 return $name{'jpn'};
90 }
91 else {
92 3 return $name{'eng'};
93 }
94 }
95
96 4 ▶sub fetch ($) {
Subroutine fetch: called CC=3 SCAR=11.0
Pod undocumented
97 4 my $abs_path = shift;
98
99 4 ▶ my $sock = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
my $sock = "IO::Socket::INET"->new("PeerAddr", $Server, "PeerPort", "http(80)", "Proto", "tcp") or die "Couldn't connect to $Server" 4 0 4
100 PeerAddr => $Server,
101 PeerPort => 'http(80)',
102 Proto => 'tcp',
103 ) or die "Couldn't connect to $Server";
104
105 4 print $sock <<"EOF";
106 GET $abs_path HTTP/1.1
107 Host: $Server
108
109 EOF
110
111 4 chomp(my @html = <$sock>);
112 4 close $sock;
113
114 4 foreach my $line (@html) {
115 664 utf8::decode($line);
116 }
117 4 return @html;
118 }
119
120 0 ▶sub full2half ($) {
Subroutine full2half: not called CC=1 SCAR=6.9
Pod undocumented
121 0 my $string = shift;
122
123 0 $string =~ tr/0-9A-Za-z/0-9A-Za-z/;
124 0 $string =~ tr[―~ _|.,:;!?][-~ _|.,:;!?];
125 0 $string =~ tr/()[]{}<>《》【】‘’“”/()[]{}<><>[]`\'""/;
126 0 $string =~ tr[-+/*%=×][-+/*%=*];
127 0 $string =~ tr/‐@#$¥&^・/-@#$\\&^-/;
128
129 0 return $string;
130 }
131
132 1 1;
133 __END__
↑ Summary Build.pl »