Do you mean something like this?

https://rosettacode.org/wiki/Compiler/AST_interpreter#Perl

This reads in a flattened Abstract Syntax Tree (AST) and generates from it a tree of perl objects it can then run with a simple ->run method and polymorphism.
This does have a hash %variables that is shared between several of the subs in different packages(classes).

There is also this, a simpler tree based expression parser and evaluator that again uses perl polymorphism to calculate a result from a tree of objects. I'm considering submitting this to https://rosettacode.org/wiki/Arithmetic_evaluation but I'm not sure if I will.

#!/usr/bin/perl use strict; # https://rosettacode.org/wiki/Arithmetic_evaluation use warnings; sub node { bless [ splice @_, 1 ], shift } sub error { die s/\G.*//sr =~ tr/\t/ /cr, "^ $_[0] !\n" } sub want { /\G$_[1]/gc ? shift : error pop } sub expr { /\G\h+/gc; my $tree = /\G\d+/gc ? node NUMBER => $& : /\G\(/gc ? want expr(0), qr/\)/, 'Missing Right Paren' : error 'Operand Expected'; $tree = /\G\h+/gc ? $tree : $_[0] <= 0 && /\G\+/gc ? node ADD => $tree, expr(1) : $_[0] <= 0 && /\G\-/gc ? node SUBTRACT => $tree, expr(1) : $_[0] <= 1 && /\G\*/gc ? node MULTIPLY => $tree, expr(2) : $_[0] <= 1 && /\G\//gc ? node DIVIDE => $tree, expr(2) : return $tree while 1; } sub ADD::value { $_[0][0]->value + $_[0][1]->value } sub SUBTRACT::value { $_[0][0]->value - $_[0][1]->value } sub MULTIPLY::value { $_[0][0]->value * $_[0][1]->value } sub DIVIDE::value { $_[0][0]->value / $_[0][1]->value } sub NUMBER::value { $_[0][0] } sub NUMBER::show { "$_[0][0]\n" } sub UNIVERSAL::show { ref($_[0]) . "\n" . join('', map $_->show, @{$_[0]}) =~ s/^/ /g +mr } while( <DATA> ) { eval { print; my $tree = want expr(0), "\n", 'Incomplete Parse'; print $tree->show, "value of tree = ", $tree->value, "\n\n"; } or print "$@\n"; } __DATA__ (1+3)*7 42 + ( 33 + ( 7 * 8 ) 123 456 foobar 7 / foobar 7 foobar 10 - 3 - 1 10 - (3 - 1) 2 * 3 + 4 * 5 2 + 3 * 4 + 5 ((((( 7 / 4 ))))) ((((( 7 / 4 )))))))) 2 + (3 + 4 no_names_allowed ) 7 ) 1 + 3 )

In reply to Re: Use cases for 'sub Pckg::func { }' ? by tybalt89
in thread Use cases for 'sub Pckg::func { }' ? by LanX

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.