Thread Can't use string as a HASH ref, strict und OOP (2 answers)
Opened by styx-cc at 2009-12-05 21:47

styx-cc
 2009-12-05 21:47
#128994 #128994
User since
2006-05-20
533 Artikel
BenutzerIn

user image
Hallo allerseits.
Hab hier ein kleines Problem, so bald ich im Modul MetalData "use strict;" verwende bekomme ich folgende Fehlermeldung: "Can't use string ("781.30") as a HASH ref while "strict refs" in use at MetalData.pm line 51." lasse ich "use strict;" weg bekomme ich das erwartete Ergebnis..

Code aufrufendes Script:
Code (perl): (dl )
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();


Code Modul:
Code (perl): (dl )
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 =~ /(&euro;.*)?&euro;(.+?)<\/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;


Kann mir jemand sagen woran das liegt? Ich find den Fehler einfach nicht...
Ich hab hier leider keine Bücher zur Hand und google&co haben auch nicht geholfen.

Vielen Dank
Last edited: 2009-12-05 21:59:11 +0100 (CET)
Pörl.

View full thread Can't use string as a HASH ref, strict und OOP