Leser: 21
1 2 3 4 5 6 7 8 9 10
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use lib 'Stock/'; use MetalData; my $md = new MetalData(-metals => 'ag,au,pt'); print Dumper $md->get_metal_data();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
package MetalData; use strict; use warnings; use LWP::Simple; use CGI::Carp qw/croak/; # constructor sub new { my $class = shift; my $self = {@_}; bless $self, $class; } # new sub get_metal_data { my $self = shift; $self->set_exchange_price(); #$self->set_bullion_price(); return $self->metal_data(); } # get_metal_data sub set_exchange_price { my $self = shift; my $content = get('http://www.goldmoney.com'); if ($content) { # get single html-parts, which includes the price of metals if($content =~ /<table>.+>Gold:(.+?)><\/table>.*<table>.+>Silver:(.+?)><\/table>.*<table>.+>Platinum:(.+?)><\/table>/s) { $self->price_breakdown('au', $1); $self->price_breakdown('ag', $2); $self->price_breakdown('pt', $3); } else { croak "Cannot parse goldmoney.com senseful.\n"; } } else { croak "Cannot fetch goldmoney.com.\n"; } } # set_exchange_price sub price_breakdown { my $self = shift; my ($metal, $html) = @_; $html =~ /(€.*)?€(.+?)<\/p>/s; my ($price, undef) = split /\//, $2; # price in html has an '/unit' at the end $self->metal_data($metal, $price); } # price_breakdown sub metal_data { my $self = shift; my $metal = shift; if (@_) { $self->{$metal}{'oz_price'} = shift; $self->{$metal}{'oz_price'}{'ts'} = time(); } else { my @data; for ($self->metals) { #print; push(@data, [$_, $self->{$_}{'oz_price'}, $self->{$_}{'oz_price'}{'ts'}]); } return \@data; } } # metal_data ## getter/setter ## sub metals { my $self = shift; if (@_) { $self->{'-metals'} = shift; } else { return(split/,/, $self->{'-metals'}); } } # metals 1;
1 2 3 4
# 781.30 zuweisen $self->{$metal}{'oz_price'} = shift; # 781.30 als Hash referenz benutzen.... $self->{$metal}{'oz_price'}{'ts'} = time();