Within the subroutine lex, @output refers to the local lexical declared with my. In argout as Fletch noted it refers to the package variable.

The use of $buffer also refers to the package variable. If you were to use strict you would get error messages like:

Global symbol "$buffer" requires explicit package name at ... Global symbol "@output" requires explicit package name at ... Global symbol "%states" requires explicit package name at ...
To get rid of these messages simply declare the variables at the beginning of your source file with either:
my ($buffer, @output, %states);
or
our ($buffer, @output, %states);
if they need to be visible from outside the current package.
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,

To implement a more object-oriented approach like you might be inclined to do in Java, one usually would proceed like this:

package DFA; sub new { my $dfa = {}; bless $dfa, shift; # associates $dfa with the current package $dfa->{buffer} = ''; $dfa->{output} = []; # note: output is now an array reference $dfa->{states} = {}; # note: states is now a hash ref $dfa; # return the new object } ...

And here is an example of how a subroutine like argout would be implemented as method of the DFA class:

sub argout { my $dfa = shift; # $dfa here is equivalent to 'this' in Java my $temp = { type => 'ARG', value => $dfa->{buffer} }; $dfa->{buffer} = ''; push(@{$dfa->{output}}, $temp); } # example of constructing a DFA object and calling argout: my $dfa = DFA->new(); $dfa->argout();
Also note how references to the variables $buffer and @output have changed.

This is just a very cursory overview of how to create a class in perl. For more details, see perldoc perlobj.


In reply to Re: Help with Variable Scope by pc88mxer
in thread 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.