These are several different questions
First let me warn you that your code has an error
This will fail if you don't care about indentation:
if ( $line =~ /ObjectType1/ ) {
$context = "ObjectType1" ;
}
if ( $line =~ /\}/ ) {
$context = "" ;
}
Here you rather want to test for /$\}/ at lines start!
My suggestions
- separate parsing of syntax logic from processing of semantic logic
- parse all lines of an object into a cache ( a string or nested hashes) before handling it
- with nested objects use recursion
- keep track of the indentation level, like counting open and closed braces
- you should handle parsing errors in case the input is corrupted
- use functions and packages instead of piling up if cases
- use a function dispatcher if you need to handle semantics of different "ObjectTypes"
Like this you will get reusable and maintainable code!
edit
some may miss example code, but you got a generic answer for a generic question.
Feel free to pick some points and ask for clarification.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.