I've got a program, that's a combination of procedural and object-oriented code. (Basically: It reads through a file, picking objects out as it goes, to verify structure.) It's all in one file, to make distribution/use easier. (And because it's small enough.)

Here's a couple of sections: (All under use strict; use warnings;, of course.)

use constant ERROR => 1; use constant VERBOSE_ERROR => -1; use constant WARNING => 2; use constant VERBOSE_WARNING => -2; # Handles all output of errors/warnings, etc. sub output ($$) { my ($message, $level) = @_; if ( $error_flag && $level == ERROR ) { print "ERROR: $message\n"; } if ( $warning_flag && $level == WARNING ) { print "WARNING: $message\n"; } return; }

And, later in the same file:

{ package State::Object; use Scalar::Util qw(refaddr); my %dses_name; my %first_name; my %last_name; my %mid_intial; my %internal_email; my %full_name; my %dn; my %display_name; my %line_address; sub DESTROY { my ($self) = @_; no warnings qw(uninitialized); if ( defined($first_name{$$self}) or defined($last_name{$$self +}) or defined($mid_intial{$$self}) or defined($full_name{$$se +lf}) or defined($display_name{$$self}) ) { &main::output("Record for: '".$dn{$$self}."' starting at line: + ".$line_address{$$self}.' never ended.', 2); } delete $dses_name{$$self}; delete $dn{$$self}; delete $line_address{$$self}; } # Other object code ... }

Which works just fine, so far. (It's checking to see if we got an 'end of object' for each object here. All output is funnelled through the one function, with the intent of allowing multiple output formats at some point in the future.) I just don't like line 23 of the second section: It works, but I was hoping for some expert way to avoid using magic numbers,extended package specifications, or re-defining constants all over the place.

So, my question is: What's the best magic to use here? (And yes: I know that the 'output' function is non-optimal and incomplete, in lots of ways. At the moment it's basically a placeholder to make sure everything else works, then I'll go back an make it work well.)


In reply to Mixing procedural and OO code, one file. by DStaal

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.