1 package CMS::Simple::Parse ;
15 my $lines = [ $text =~ m{(.*?$/)}sg ] ;
17 return parse_lines( {}, $lines ) ;
22 my( $curr_hash, $lines ) = @_ ;
27 while( my $line = shift @{$lines} ) {
31 # next unless $line =~ /\S/ ;
33 # look for tag:: lines and parse them out.
34 # ignore leading white space, grad for a word followed by 1 or 2 :'s.
35 # also grab any optional content following the tag
37 unless( $line =~ /^\s*(\w+)(::?)\s+(.*)\z/s ) {
39 # no tag found so just add this line to the current scalar content
47 # save any existing scalar as we found a new tag entry
50 _store_value( $curr_hash, $scalar_tag, $content ) ;
54 # see we at the end of a structure. if so, return what we have parsed
56 return $curr_hash if $tag eq 'END' ;
58 # see if this a start of a structure. if so, recursively parse and
59 # store it any content on the structure tag line is ignored. its value
60 # is always a hash ref of the structure data.
64 my $new_val = parse_lines( {}, $lines ) ;
65 _store_value( $curr_hash, $tag, $new_val ) ;
69 # now it must be a new scalar entry. save any new content on this line
75 #print "TAG $scalar_tag\n" ;
76 _store_value( $curr_hash, $scalar_tag, $content ) if $scalar_tag ;
78 #print Dumper $curr_hash ;
85 my( $curr_ref, $tag, $val ) = @_ ;
87 # NOTE: always chomping scalar content
89 chomp $val unless ref $val ;
91 my $curr_val = $curr_ref->{$tag} ;
94 unless( defined $curr_val ) {
96 #print "NEW TAG $tag [$val]\n" ;
98 $curr_ref->{$tag} = $val ;
102 if ( ref $curr_val eq 'ARRAY' ) {
104 #print "PUSH TAG $tag [$val]\n" ;
106 push( @{$curr_val}, $val ) ;
111 #print "ARRAY TAG $tag [$val]\n" ;
112 $curr_ref->{$tag} = [ $curr_val, $val ] ;
116 # cheapo csv tab file parser
122 my @lines = split m{(?<=$/)}, $text ;
126 return [ map [ split /\t/ ], @lines ] ;