M
Martin Bley
Hallo NG,
folgendes Problem:
Ich habe ein Package mit dem Namen DhcpParser. In diesem Package gibt
es eine Methode parseFile. Innerhalb der Grammatik möchte ich nun die
geparsten Werte in einen Array schreiben und aus der Methode
via return Anweisung zurückgeben. Leider habe ich außerhalb der
Grammatik keinen Zugriff auf die Variablen
(namentlich @hosts).
In der Doku steht, dass die Variablen im Kontext von Parse::RecDescent
zu sehen sind, wie kann ich
aber darauf aus der Methode parseFile() zugreifen? Da steh ich
wirklich
auf dem Schlauch. Weiss jemand Rat?
Ich habe auch schon versucht, die Variable @hosts mit our ganz oben im
package zu deklarieren, leider auch
ohne Erfolg. Hier meine package (gekürzt)
--Schnipp
package DhcpParser;
[...]
use Parse::RecDescent;
[...]
sub new {
[...]
}
sub parseFile {
my $self = shift;
my %tmp = ();
my @hosts;
my $grammar = <<'EOGRAMMAR';
IDHOST : /[a-zA-Z-0-9]+/
IDMAC : /[a-fA-F0-9:]+/
IDIP : /[0-9.]+/
IDCOMMENT : /.*/
file : host(s?)
host : /^host/ IDHOST
{
$::tmp{'name'} = $item[2];
}
/{/ comment(?) option(s?) terminator(1)
comment : <skip: qr/[ \t]*/> word(s?) newline
word : /#/ IDCOMMENT
{
$::tmp{'comment'} = $item[2];
}
newline : /\n/
option : mac | ip | other
mac : /hardware ethernet/ IDMAC /;/
{
$::tmp{'mac'} = $item[2];
}
ip : /fixed-address/ IDIP /;/
{
$::tmp{'ip'} = $item[2];
}
other : /[^}]/
terminator : /}/
{
push(@::hosts, {
'name' => $::tmp{'name'},
'comment' => $::tmp{'comment'},
'mac' => $::tmp{'mac'},
'ip' => $::tmp{'ip'} }
);
%::tmp = ();
}
EOGRAMMAR
my $parse = new Parse::RecDescent($grammar);
# Datei einlesen
my $text;
open( IN, "<" . $self->{FILENAME} ) or die "Fehler: parseFile: ($!)";
while (<IN>) {
next if ( $_ =~ /^#/ || $_ =~ /^\s+$/ );
$text .= $_;
}
close(IN);
my $tree = $parse->file($text);
if (not $tree) {
exit 1;
} else {
my $cnt= (@hosts); # <-- @hosts ist hier leer (im Kontext der
Grammatik allerdings nicht)
print $cnt, "\n";
}
[...]
}
[...]
1;
--Schnapp
Danke und Gruß,
Martin
folgendes Problem:
Ich habe ein Package mit dem Namen DhcpParser. In diesem Package gibt
es eine Methode parseFile. Innerhalb der Grammatik möchte ich nun die
geparsten Werte in einen Array schreiben und aus der Methode
via return Anweisung zurückgeben. Leider habe ich außerhalb der
Grammatik keinen Zugriff auf die Variablen
(namentlich @hosts).
In der Doku steht, dass die Variablen im Kontext von Parse::RecDescent
zu sehen sind, wie kann ich
aber darauf aus der Methode parseFile() zugreifen? Da steh ich
wirklich
auf dem Schlauch. Weiss jemand Rat?
Ich habe auch schon versucht, die Variable @hosts mit our ganz oben im
package zu deklarieren, leider auch
ohne Erfolg. Hier meine package (gekürzt)
--Schnipp
package DhcpParser;
[...]
use Parse::RecDescent;
[...]
sub new {
[...]
}
sub parseFile {
my $self = shift;
my %tmp = ();
my @hosts;
my $grammar = <<'EOGRAMMAR';
IDHOST : /[a-zA-Z-0-9]+/
IDMAC : /[a-fA-F0-9:]+/
IDIP : /[0-9.]+/
IDCOMMENT : /.*/
file : host(s?)
host : /^host/ IDHOST
{
$::tmp{'name'} = $item[2];
}
/{/ comment(?) option(s?) terminator(1)
comment : <skip: qr/[ \t]*/> word(s?) newline
word : /#/ IDCOMMENT
{
$::tmp{'comment'} = $item[2];
}
newline : /\n/
option : mac | ip | other
mac : /hardware ethernet/ IDMAC /;/
{
$::tmp{'mac'} = $item[2];
}
ip : /fixed-address/ IDIP /;/
{
$::tmp{'ip'} = $item[2];
}
other : /[^}]/
terminator : /}/
{
push(@::hosts, {
'name' => $::tmp{'name'},
'comment' => $::tmp{'comment'},
'mac' => $::tmp{'mac'},
'ip' => $::tmp{'ip'} }
);
%::tmp = ();
}
EOGRAMMAR
my $parse = new Parse::RecDescent($grammar);
# Datei einlesen
my $text;
open( IN, "<" . $self->{FILENAME} ) or die "Fehler: parseFile: ($!)";
while (<IN>) {
next if ( $_ =~ /^#/ || $_ =~ /^\s+$/ );
$text .= $_;
}
close(IN);
my $tree = $parse->file($text);
if (not $tree) {
exit 1;
} else {
my $cnt= (@hosts); # <-- @hosts ist hier leer (im Kontext der
Grammatik allerdings nicht)
print $cnt, "\n";
}
[...]
}
[...]
1;
--Schnapp
Danke und Gruß,
Martin