tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
3
ZUGRIFFE
460
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    downset04 downset04 ist offline Mitglied Gold
    Registriert seit
    Dec 2004
    Beiträge
    167
    hallo

    wie mach ich aus dem code ->
    Code :
    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
    
    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 :
    1
    2
    3
    4
    5
    6
    
    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
     

  2. #2
    renee renee ist offline Mitglied Brokat
    Registriert seit
    Jan 2004
    Beiträge
    332
    Code :
    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
    
    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 :
    1
    2
    3
    4
    5
    6
    7
    8
    
    #!/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/vi...lleIchEinModul
    http://de.selfhtml.org/perl/module/intro.htm
     

  3. #3
    downset04 downset04 ist offline Mitglied Gold
    Registriert seit
    Dec 2004
    Beiträge
    167
    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
     
    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?
     

  4. #4
    renee renee ist offline Mitglied Brokat
    Registriert seit
    Jan 2004
    Beiträge
    332
    Das Modul muss einen "wahren" Wert zurückliefern. Der Übersichtlichkeit wegen schreibt man das 1; normalerweise ans Ende des Moduls...
     

Ähnliche Themen

  1. Problem mit SSH2 modul
    Von J0hn B0y im Forum Hosting & Webserver
    Antworten: 0
    Letzter Beitrag: 25.11.10, 12:31
  2. gallery / modul problem....
    Von digifoho im Forum PHP
    Antworten: 7
    Letzter Beitrag: 10.07.06, 18:37
  3. Antworten: 9
    Letzter Beitrag: 26.07.05, 13:37
  4. Problem mit Perl Modul in CGI Script
    Von blackbirdthefirst im Forum CGI, Perl, Python, Ruby, Power Shell
    Antworten: 1
    Letzter Beitrag: 26.11.04, 12:26
  5. Problem mit mod_auth_mysql Modul
    Von DJ_Apfel im Forum Hosting & Webserver
    Antworten: 0
    Letzter Beitrag: 07.06.04, 13:05