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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |