This is a followup to a question from 20 years ago.
I've got this test script for conditionally loading debug subroutines from a module I wrote:
#! /usr/bin/env perl use v5.14; use warnings; use if $ENV{MYMOD_DEBUG_LEVEL}, 'Test::Utils::Dump' => qw(d d0 d1 dd d +i $doff); d('hi'); di('hi'); dd('hi');
If the debug level is unset, the script will throw errors because the debug subroutines were not loaded. I could do this:
defined &d && d('hi'); defined &di && di('hi'); defined &dd && dd('hi');
But this is ugly. A less ridiculous solution is to write stubs for each sub I want to possibly load:
sub d {} sub d0 {} sub d1 {} sub dd {} sub di {}; use if $ENV{MYMOD_DEBUG_LEVEL}, 'Test::Utils::Dump' => qw(d d0 d1 dd d +i); d 'hi'; di 'hi'; dd 'hi';
But this still feels a big hacky. So I'm wondering if Perl Gods have bestowed upon us a gift for accomplishing this feat more cleanly and elegantly in recent years. Thanks.
EDIT: Using sub AUTOLOAD will catch subs that don't exist but from what I can tell, I would still need to wrap subroutine args in parens which I'd rather not do.
$PM = "Perl Monk's";
$MC = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar Parson";
$nysus = $PM . ' ' . $MC;
Click here if you love Perl Monks
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |