File Coverage

blib/lib/Finance/GeniusTrader/Eval.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Finance::GeniusTrader::Eval;
2              
3             # Copyright 2000-2002 Raphaël Hertzog, Fabien Fulhaber
4             # This file is distributed under the terms of the General Public License
5             # version 2 or (at your option) any later version.
6              
7 1     1   45585 use strict;
  1         2  
  1         75  
8 1     1   6 use vars qw(@EXPORT @ISA);
  1         2  
  1         715  
9             #ALL# use Log::Log4perl qw(:easy);
10              
11 1     1   597 use Finance::GeniusTrader::Conf;
  1         4  
  1         29  
12 1     1   574 use Finance::GeniusTrader::ArgsTree;
  0            
  0            
13             use Finance::GeniusTrader::Tools qw(:conf);
14              
15             require Exporter;
16              
17             @ISA = qw(Exporter);
18             @EXPORT = qw(create_standard_object create_db_object get_standard_name get_long_name);
19              
20             =head1 NAME
21              
22             Finance::GeniusTrader::Eval - Create unknown standard objects at run-time
23              
24             =head1 DESCRIPTION
25              
26             This modules provides several functions to manipulate objects
27             based on their type name.
28              
29             =over
30              
31             =item C<< $object = create_standard_object($object_type, ...) >>
32              
33             This will create an object of type $object_type. The following parameters
34             will be passed to the object at creation time.
35              
36             =cut
37             sub create_standard_object {
38             my ($type, @rawparams) = @_;
39            
40             $type = long_name($type);
41             $type =~ s#/\d+##g;
42              
43             my ($name, @args) = Finance::GeniusTrader::ArgsTree::parse_args(join(" ", @rawparams));
44             if ($type =~ /^@(\S+)$/) {
45             my $def = resolve_object_alias(long_name($1), @args);
46             #DEB# DEBUG "Alias $1 maps to $def\n";
47             if ($def =~ /^\s*{(.*)}\s*$/) {
48             $def = $1;
49             }
50             if ($def =~ /^\s*(\S+)\s*(.*)\s*$/) {
51             $type = long_name($1);
52             ($name, @args) = Finance::GeniusTrader::ArgsTree::parse_args($2);
53             }
54             }
55            
56             my $object;
57             my $eval = "use Finance::GeniusTrader::$type;\n";
58             $eval .= "\$object = Finance::GeniusTrader::$type->new(";
59             if (scalar(@args))
60             {
61             $eval .= "[" . join(",", map { if (/^\d+$/) { $_ } else { "'$_'" } }
62             Finance::GeniusTrader::ArgsTree::args_to_ascii(@args)) . "]";
63             }
64             $eval .= ");";
65             #DEB# DEBUG "create_standard_object with: $eval";
66             eval $eval;
67             die $@ if ($@);
68              
69             return $object;
70             }
71              
72             =item C<< create_db_object() >>
73              
74             Return a Finance::GeniusTrader::DB object created based on Finance::GeniusTrader::Conf data. Thus Finance::GeniusTrader::Conf::load()
75             should be called before this function. If DB::module doesn't exist in the
76             config, it tries to load the user configuration (supposing it has never been done
77             before).
78              
79             =cut
80             our $db;
81             sub create_db_object {
82             my $db_module = Finance::GeniusTrader::Conf::get("DB::module");
83             if (! defined($db_module)) {
84             Finance::GeniusTrader::Conf::load();
85             }
86             if (! defined($db)) {
87             $db = create_standard_object("DB::" . Finance::GeniusTrader::Conf::get("DB::module"));
88             }
89             return $db;
90             }
91              
92             =item C<< get_standard_name($object, $shorten, $number) >>
93              
94             Return the standard name of an object that can be later used to
95             create it again.
96              
97             =cut
98             sub get_standard_name {
99             my ($object, $shorten, $number) = @_;
100             $shorten = 1 if (! defined($shorten));
101             my $n = ref($object);
102             $n =~ s/Finance::GeniusTrader:://g;
103             if ($shorten)
104             {
105             $n = short_name($n);
106             }
107             if (defined($number) && $number) {
108             $n .= "/" . ($number + 1);
109             }
110             if (ref($object->{'args'}) =~ /Finance::GeniusTrader::ArgsTree/) {
111             $n .= " " . join(" ", $object->{'args'}->get_arg_names());
112             } elsif (scalar(@{$object->{'args'}})) {
113             $n .= " " . join(" ", @{$object->{'args'}});
114             }
115             return $n;
116             }
117              
118             =item C<< get_long_name ($code) >>
119              
120             Returns the long name of the market (if defined).
121              
122             See also ~/.gt/sharenames which contains lines of the form
123             \t
124             mapping a market code to its long name.
125              
126             =cut
127             sub get_long_name {
128             my $code = shift;
129            
130             $db = create_db_object() unless $db;
131             return $db->get_name($code);
132             }
133              
134             =pod
135              
136             =back
137              
138             =cut
139             1;