#!/usr/bin/env perl
use v5.20;
use warnings;
use feature 'signatures';
no warnings 'experimental::signatures';
# ------ Debugging function definition:
our %debug;
our $d_area = "(default)";
sub dsay( @args ) {
return
unless $debug{ uc $d_area }
|| $debug{'ALL'};
my $prefix =
$d_area ne "(default)"
? "DEBUG $d_area: "
: "DEBUG: ";
say $prefix, @args;
}
# ------ Application program:
sub common_sub {
say "call to common sub (d_area is $d_area)";
dsay "debugging output from common_sub";
}
sub one {
say "call to one:";
local $d_area = "one";
dsay "debugging output from one";
common_sub;
dsay "more debugging output from one";
}
sub two {
say "call to two:";
local $d_area = "two";
dsay "debugging output from two";
common_sub;
dsay "more debugging output from two";
}
# Switch on debugging for area 'two'
# (I'll use command line switches with Getopt::Long later).
$debug{TWO} = 1;
say "hello from main";
dsay "debugging output from main";
one;
two;
####
hello from main
call to one:
call to common sub (d_area is one)
call to two:
DEBUG two: debugging output from two
call to common sub (d_area is two)
DEBUG two: debugging output from common_sub
DEBUG two: more debugging output from two
####
#
# Dsay.pm
#
package Dsay;
use parent 'Exporter';
our @EXPORT = qw( %debug $d_area dsay );
our %debug;
our $d_area = "(default)";
sub dsay( @args ) {
return
unless $debug{ uc $d_area }
|| $debug{'ALL'};
my $prefix =
$d_area ne "(default)"
? "DEBUG $d_area: "
: "DEBUG: ";
say $prefix, @args;
}
1;
####
use Dsay;
####
local $Dsay::d_area = '...';