in reply to Re: Overriding Global Variables
in thread Overriding Global Variables

Can the caller package and its global variables be accessed without it being known to the "Debug" module via 'use' or 'require? I'd love to see an example if you get around to it. Thanks LanX.

Replies are listed 'Best First'.
Re^3: Overriding Global Variables (accessing vars in caller package)
by LanX (Saint) on Nov 19, 2017 at 19:40 UTC
    Here a simple demonstration without importing ( ::debug is short for main::debug )

    use strict; use warnings; use Carp; package main; sub debug { my ($pkg) = caller; { no strict 'refs'; return unless ${ "${pkg}::DEBUG_FLAG" }; } carp "@_"; } package My_First; our $DEBUG_FLAG = 1; ::debug( "Print " . __PACKAGE__ ); # prints package My_Second; our $DEBUG_FLAG = 0; ::debug( "Print " . __PACKAGE__ ); # doesn't print

    Print My_First at d:/Users/RolfLangsdorf/pm/pm_debug_caller.pl line 23 +.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

    Update

    improved code with Carp to report file and line from callers perspective.