Problem ein Modul zu machen

downset04

Erfahrenes Mitglied
hallo

wie mach ich aus dem code ->
Code:
use XML::Parser;
# initialize the parser
my $parser = XML::Parser->new( Handlers =>
                                      {
                                        Start=>\&handle_start,
                                        End=>\&handle_end,
                                      });
$parser->parsefile( shift @ARGV );
my @element_stack;                 # remember which elements are open
# process a start-of-element event: print message about element
#
sub handle_start {
    my( $expat, $element, %attrs ) = @_;
    # ask the expat object about our position
    my $line = $expat->current_line;
    print "I see an $element element starting on line $line!\n";
    # remember this element and its starting position by pushing a
    # little hash onto the element stack
    push( @element_stack, { element=>$element, line=>$line });
    if( %attrs ) {
        print "It has these attributes:\n";
        while( my( $key, $value ) = each( %attrs )) {
            print "\t$key => $value\n";
        }
    }
}
# process an end-of-element event
#
                                                        
  sub handle_end {
        my( $expat, $element ) = @_;
        # We'll just pop from the element stack with blind faith that
        # we'll get the correct closing element, unlike what our
        # homebrewed well-formedness did, since XML::Parser will scream
        # bloody murder if any well-formedness errors creep in.
        my $element_record = pop( @element_stack );
        print "I see that $element element that started on line ",
              $$element_record{ line }, " is closing now.\n";
  }
(kommt aus einem Buch) ein Modul?

ich glaub das Problem liegt irgendwie hier ->
Code:
my $parser = XML::Parser->new( Handlers =>
                                      {
                                        Start=>\&handle_start,
                                        End=>\&handle_end,
                                      });
$parser->parsefile( shift @ARGV );
wenn ich über das eine Funktion mache und versuche die Funktion aufzurufen dann krieg ich Fehler - ich glaube sub hande_start und sub handle_end werden darum nicht mehr aufgerufen - das was ich aber will ist eine Funktion machen die ich dann von einem Perl file aufrufen kann und den ganzen parse Prozess starten!

thx
 
Code:
package Downset04Test;

use XML::Parser;
# initialize the parser
my $parser = XML::Parser->new( Handlers =>
                                      {
                                        Start=>\&handle_start,
                                        End=>\&handle_end,
                                      });
my @element_stack;                 # remember which elements are open

sub parse_file{
    my ($file) = @_;
    $parser->parsefile($file);
}


# process a start-of-element event: print message about element
#
sub handle_start {
    my( $expat, $element, %attrs ) = @_;
    # ask the expat object about our position
    my $line = $expat->current_line;
    print "I see an $element element starting on line $line!\n";
    # remember this element and its starting position by pushing a
    # little hash onto the element stack
    push( @element_stack, { element=>$element, line=>$line });
    if( %attrs ) {
        print "It has these attributes:\n";
        while( my( $key, $value ) = each( %attrs )) {
            print "\t$key => $value\n";
        }
    }
}
# process an end-of-element event
#
                                                        
  sub handle_end {
        my( $expat, $element ) = @_;
        # We'll just pop from the element stack with blind faith that
        # we'll get the correct closing element, unlike what our
        # homebrewed well-formedness did, since XML::Parser will scream
        # bloody murder if any well-formedness errors creep in.
        my $element_record = pop( @element_stack );
        print "I see that $element element that started on line ",
              $$element_record{ line }, " is closing now.\n";
  }

1;

Dann im Skript:
Code:
#!/usr/bin/perl

use strict;
use warnings;
use Downset04Test;

my $file = '/path/to/file.xml';
Downset04Test::parse_file($file);

Zum Thema Module schreiben: http://wiki.perl-community.de/bin/view/Wissensbasis/WieErstelleIchEinModul
http://de.selfhtml.org/perl/module/intro.htm
 
Code:
use XML::Parser;
# initialize the parser


my $parser = XML::Parser->new( Handlers =>
                                      {
                                        Start=>\&handle_start,
                                        End=>\&handle_end,
                                      });
my @element_stack;                 # remember which elements are open


    my ($file) = @_;
    $parser->parsefile($file);
}
1;

eine andere version da bin ich mit probieren draufgekommen es muss einfach der 1; am ende der funktion sein! ist das gut? oder 1; immer nur am ende des files?
 
Das Modul muss einen "wahren" Wert zurückliefern. Der Übersichtlichkeit wegen schreibt man das 1; normalerweise ans Ende des Moduls...
 
Zurück