I've been learning a little about Discrete Finite Automata and I'm excited about using them for lexing mathematical expressions. I have a question about why the following code seems to work.
sub argout { my $temp = {'type'=>'ARG', 'value'=>$buffer}; $buffer = ''; push(@output,$temp); } sub otherout { my $c = shift; my $temp = {'type'=>'OTHER', 'value'=>$c}; push(@output,$temp); } %states = ( 'start'=>{'s'=>{'nextstate'=>'var1.1'}}, 'var1.1'=>{'i'=>{'nextstate'=>'var1.2'}}, 'var1.2'=>{'s'=>{'action'=>'argout', 'nextstate'=>'var1.1'}, 'eof'=>{'action'=>'argout'}} ); sub lex { my $inputstring = shift; my @input = split('',$inputstring); my @output = (); $buffer = ''; my $s = 'start'; foreach my $c (@input,'eof') { if( my $t = $states{$s}->{$c} ) { eval( $t->{'action'} ); $s = $t->{'nextstate'}; } else { otherout($c); return 0; } $buffer = $buffer.$c; } return 1; } if( lex('sisisi') ) { print("success!\n"); } else {print("failure...")} foreach my $tok (@output) { print("$tok->{'type'},$tok->{'value'}\n"); }
This code produces the following output.
success! ARG,si ARG,si ARG,si
It seems to rely on the $buffer being a sort of "global" variable (global vars are bad right?), so that the argout function can access it. Now if I add a 'my' keyword in front of the $buffer declaration (inside the lex subroutine) then the output is the following.
success! ARG, ARG, ARG,
Presumably this is because the $buffer variable is no longer accessible from the 'argout' subroutine. Why is the @output variable accessible from the 'argout' subroutine even though it is declared local to 'lex'?

Knowing this would be one thing, but what I really want is that the $buffer, the @input, the @output, and possibly even the $c would be data inside of a "Dfa" object, and all of those other methods could just access those variables whenever they wanted. The reason I haven't done this yet is that the only way I know how to make objects in Perl is to make sort of a "special hash" and bless it with functions that have intimate access to it. I didn't want to do this because I didn't want to have to use constructs like $self->{'buffer'} everytime I wanted to use the buffer variable. I feel like I'm still stuck in a sort of "Java Mode" of thinking right now...Is there a better, "Perler" way of doing all this?

Any help will be greatly appreciated,
~Terry


In reply to Help with Variable Scope by tford

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.