Thread Hash als Option missbrauchen (118 answers)
Opened by bloonix at 2005-12-19 02:24

bloonix
 2005-12-19 17:25
#60980 #60980
User since
2005-12-17
1615 Artikel
HausmeisterIn
[Homepage]
user image
ok, das scheint nicht so einfach zu sein, dass @_ Array auf seine Werte zu
prüfen ... ich hatte vorher nicht so ganz verstanden, wie Perl die Argumente
an eine Funktion übergibt und gerade gelesen, dass => als Kommaersatz
dient.

Wenn ich also - angenommen - prüfen möchte, dass mir auf jeden Fall ein
Hash und keine Referenz übergeben wird, dann könnte ich doch wie folgt
vorgehen:

Code: (dl )
1
2
3
4
5
6
7
sub Statistics {
  for (@_) {
     warn "Bad argument $_" if ref $_;
  }
}

Statistics( 'Key','Value',{ 'Key1' => 'Value1','Key2' => 'Value2' },[ 'Value1','Value2' ] );


aber dann bleibt noch folgendes Problem offen:

Code: (dl )
1
2
3
4
5
6
7
8
9
sub Statistics {
  for (@_) {
     die "Bad argument $_" if ref $_;
  }

  my %param = @_; # line 10
}

Statistics( 'Key1','Value2','Key2' );


"Odd number of elements in hash assignment at ./testsub line 10."

Kann ich diese Warnung irgendwie abfangen, so dass das Skript stirbt?
What is a good module? That's hard to say.
What is good code? That's also hard to say.
One man's Thing of Beauty is another's man's Evil Hack.

View full thread Hash als Option missbrauchen