Thread Funktion aus Modul im skalaren Kontext aufrufen? (3 answers)
Opened by Phrosch at 2012-03-26 12:29

Phrosch
 2012-03-26 12:29
#157042 #157042
User since
2012-03-20
9 Artikel
BenutzerIn
[default_avatar]
Hallo zusammen,

folgender Code ist gegeben:

Code: (dl )
1
2
3
4
5
6
7
8
9
10
use NetAddr::IP;
use Data::Dumper;

my $n = NetAddr::IP->new( '192.168.0.4/30' );

for my $ip( @{$n->hostenumref} ) {
print Dumper($ip->numeric());
print $ip->numeric(), "\n";
}
}


Ausgabe:

Code: (dl )
1
2
3
4
5
6
$VAR1 = '175079429';
$VAR2 = '4294967295';
1750794294294967295
$VAR1 = '175079430';
$VAR2 = '4294967295';
1750794304294967295



Beschreibung der Funktion ->numeric() innerhalb des Moduls:

When called in a scalar context, will return a numeric representation of the address part of the IP address. When called in an array contest, it returns a list of two elements. The first element is as described, the second element is the numeric representation of the net mask.

Okay, ich verstehe das ich mit dem Print-Befehl die Funktion -numeric() im Array-Kontext aufrufe und deswegen sowohl die IP-Adresse als auch Netzmaske als ein String ausgegeben wird.

Wie muss ich aber -numeric() im Skalaren Kontext aufrufen um nur den ersten Wert (IP-Adresse als numerischen Wert - hier 175079430) auszugeben?

Ich habe es mit

Code: (dl )
print $ip->numeric->[0], "\n";


probiert, bekomme aber nur die folgende Fehlermeldung zurück:

Can't use string ("175079429") as an ARRAY ref while "strict refs" in use at ./NetAddr.IP.v.05.pl line 16.

Wie rufe ich also -numeric() im skalaren Kontext auf?

Vielen Dank schon einmal im voraus.

P.

View full thread Funktion aus Modul im skalaren Kontext aufrufen?