From 4b6c5b71a6f91571ffb5b698ede0cb82e71326b3 Mon Sep 17 00:00:00 2001 From: "mascip (Pierre Masci)" Date: Tue, 21 May 2013 12:33:23 +0100 Subject: [PATCH 1/6] now possible to use an Alias for the debuggit() function --- lib/Debuggit.pm | 14 +++++++++++--- t/alias.t | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 t/alias.t diff --git a/lib/Debuggit.pm b/lib/Debuggit.pm index 0c737c1..ba990c0 100644 --- a/lib/Debuggit.pm +++ b/lib/Debuggit.pm @@ -110,7 +110,7 @@ This POD explains just the basics of using C. For full details, see L #### #################### main pod documentation end ################### -my ($debuggit, $add_func); +my ($debuggit, $add_func, $debuggit_alias); ##################################################################### @@ -144,12 +144,13 @@ Only L is exported. ## ##################################################################### - sub import { my ($pkg, %opts) = @_; my $caller_package = $opts{PolicyModule} ? caller(1) : caller; + my $debuggit_alias = $opts{Alias} if defined $opts{Alias}; + my $master_debug = eval "Debuggit::DEBUG()"; my $debug_value = defined $opts{DEBUG} ? $opts{DEBUG} : defined $master_debug ? $master_debug : 0; unless (defined $master_debug) @@ -181,8 +182,11 @@ sub import { *{ join('::', $caller_package, 'debuggit') } = sub {}; *Debuggit::add_func = sub {} unless Debuggit->can('add_func'); + *{ join('::', $caller_package, $debuggit_alias) } = sub {} + if $debuggit_alias; # Alias for the debuggit() function } -} + +} # End import() sub _setup_funcs @@ -204,10 +208,14 @@ sub _setup_funcs { *Debuggit::debuggit = eval $debuggit unless Debuggit->can('debuggit'); *{ join('::', $caller_package, 'debuggit') } = \&debuggit; + *{ join('::', $caller_package, $debuggit_alias) } = \&debuggit + if $debuggit_alias; # Alias for the debuggit() function } else { *{ join('::', $caller_package, 'debuggit') } = eval $debuggit; + *{ join('::', $caller_package, $debuggit_alias) } = eval $debuggit + if defined $debuggit_alias; # Alias for the debuggit() function } unless (Debuggit->can('add_func')) diff --git a/t/alias.t b/t/alias.t new file mode 100644 index 0000000..e303a97 --- /dev/null +++ b/t/alias.t @@ -0,0 +1,38 @@ +my $DESC = ''; + +package WithoutAlias; + +use strict; +use warnings; +use Test::More 0.88 ; +use Test::Exception 0.31 ; + +# Handy test function +sub TEST { $DESC = shift } + +use Debuggit; +TEST "The dbg() function does not exist natively"; { + throws_ok { dbg("any message") } qr/Undefined subroutine/, $DESC; +} + +package WithAlias; + +use strict; +use warnings; +use Test::More 0.88 ; +use Test::Exception 0.31 ; + +# Handy test function +sub TEST { $DESC = shift } + +use Debuggit Alias => 'dbg'; +TEST "The dbg() function is defined, if created with the Alias argument"; { + lives_ok { dbg("any message") } $DESC; +} + +TEST "The bad_name() function does not exist natively"; { + throws_ok { bad_name("any message") } qr/Undefined subroutine/, $DESC; +} + +done_testing; + From 53d12ac204dcffd333507f17dbaf03e3a46b99f8 Mon Sep 17 00:00:00 2001 From: "mascip (Pierre Masci)" Date: Tue, 21 May 2013 12:56:55 +0100 Subject: [PATCH 2/6] more tests for the Alias feature (in t/alias.t) --- lib/Debuggit.pm | 7 ++++--- t/alias.t | 24 ++++++++++++++---------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/Debuggit.pm b/lib/Debuggit.pm index ba990c0..496bdf3 100644 --- a/lib/Debuggit.pm +++ b/lib/Debuggit.pm @@ -149,7 +149,8 @@ sub import my ($pkg, %opts) = @_; my $caller_package = $opts{PolicyModule} ? caller(1) : caller; - my $debuggit_alias = $opts{Alias} if defined $opts{Alias}; + $debuggit_alias = $opts{Alias} if defined $opts{Alias}; +# say $debug my $master_debug = eval "Debuggit::DEBUG()"; my $debug_value = defined $opts{DEBUG} ? $opts{DEBUG} : defined $master_debug ? $master_debug : 0; @@ -181,9 +182,9 @@ sub import else { *{ join('::', $caller_package, 'debuggit') } = sub {}; - *Debuggit::add_func = sub {} unless Debuggit->can('add_func'); *{ join('::', $caller_package, $debuggit_alias) } = sub {} if $debuggit_alias; # Alias for the debuggit() function + *Debuggit::add_func = sub {} unless Debuggit->can('add_func'); } } # End import() @@ -215,7 +216,7 @@ sub _setup_funcs { *{ join('::', $caller_package, 'debuggit') } = eval $debuggit; *{ join('::', $caller_package, $debuggit_alias) } = eval $debuggit - if defined $debuggit_alias; # Alias for the debuggit() function + if $debuggit_alias; # Alias for the debuggit() function } unless (Debuggit->can('add_func')) diff --git a/t/alias.t b/t/alias.t index e303a97..de58887 100644 --- a/t/alias.t +++ b/t/alias.t @@ -1,37 +1,41 @@ my $DESC = ''; +## Without Alias package WithoutAlias; +use Debuggit; use strict; use warnings; use Test::More 0.88 ; use Test::Exception 0.31 ; +sub TEST { $DESC = shift } # Handy test function -# Handy test function -sub TEST { $DESC = shift } - -use Debuggit; TEST "The dbg() function does not exist natively"; { throws_ok { dbg("any message") } qr/Undefined subroutine/, $DESC; } +## With an alias package WithAlias; +use Debuggit DEBUG => 1, Alias => 'dbg'; use strict; use warnings; use Test::More 0.88 ; use Test::Exception 0.31 ; +use Test::Output 0.16 ; +sub TEST { $DESC = shift } # Handy test function -# Handy test function -sub TEST { $DESC = shift } +TEST "The bad_name() function does not exist natively"; { + throws_ok { bad_name("any message") } qr/Undefined subroutine/, $DESC; +} -use Debuggit Alias => 'dbg'; -TEST "The dbg() function is defined, if created with the Alias argument"; { +TEST "The dbg() function exists, if it was created with the Alias argument"; { lives_ok { dbg("any message") } $DESC; } -TEST "The bad_name() function does not exist natively"; { - throws_ok { bad_name("any message") } qr/Undefined subroutine/, $DESC; +TEST "Display correct output with dbg() alias (simple use case)"; { + my $message = 'expected output'; + stderr_is { debuggit($message); } "$message\n", $DESC; } done_testing; From f5d28cd00bf285d79f13b9ca8ee0dc2c272aa151 Mon Sep 17 00:00:00 2001 From: "mascip (Pierre Masci)" Date: Tue, 21 May 2013 12:59:22 +0100 Subject: [PATCH 3/6] nothing (deleted a comment) --- lib/Debuggit.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Debuggit.pm b/lib/Debuggit.pm index 496bdf3..0ddfc33 100644 --- a/lib/Debuggit.pm +++ b/lib/Debuggit.pm @@ -144,13 +144,13 @@ Only L is exported. ## ##################################################################### + sub import { my ($pkg, %opts) = @_; my $caller_package = $opts{PolicyModule} ? caller(1) : caller; $debuggit_alias = $opts{Alias} if defined $opts{Alias}; -# say $debug my $master_debug = eval "Debuggit::DEBUG()"; my $debug_value = defined $opts{DEBUG} ? $opts{DEBUG} : defined $master_debug ? $master_debug : 0; From 02fa76bedc6e4a09a3be0332197f9158f78595fe Mon Sep 17 00:00:00 2001 From: "mascip (Pierre Masci)" Date: Tue, 21 May 2013 13:43:08 +0100 Subject: [PATCH 4/6] Added documentation about the Alias feature --- lib/Debuggit.pm | 8 +++++++- lib/Debuggit/Manual.pm | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/Debuggit.pm b/lib/Debuggit.pm index 0ddfc33..1134cdb 100644 --- a/lib/Debuggit.pm +++ b/lib/Debuggit.pm @@ -136,7 +136,13 @@ included modules. See L for full details =head2 Functions exported -Only L is exported. +The L function is exported. The Alias parameter can be used to provide a shorter alias for calling this function, for example: + + use Debuggit Alias => 'dbg'; # The dbg() function is now an alias to debuggit() + # And then: + dbg("This message will be printed"); + +The user is warned that the alias that they choose might conflict with other modules. More warning and advice are given in the L page. =cut diff --git a/lib/Debuggit/Manual.pm b/lib/Debuggit/Manual.pm index d2645e3..230046c 100644 --- a/lib/Debuggit/Manual.pm +++ b/lib/Debuggit/Manual.pm @@ -146,6 +146,18 @@ lot of flexibility has been built into it as well. Read on to see how to adjust of C. +=head2 Creating an alias for the debuggit function + +If you are feeling lazy, you can create an alias for the debuggit function. "debuggit" is not that long to type, but some of us are just very lazy... So here is how to do it: + + use Debuggit Alias => 'dbg'; # The dbg() function is now an alias to debuggit() + # And then: + dbg("This message will be printed"); + +The user is warned that the alias that they choose might conflict with other modules. If you call your debugging function "print" or "eval", expect some surpises: this module will not warn you if this happens. + +The smaller the alias, the more convenient it is but the more likely it is to create conflicts. That is why the name "debuggit" was chosen as a default. At least one user of this module uses the alias "dbg", because it's shorter. It's up to you. + =head2 The basics =head4 debuggit([level =>] arg[, ...]) From e99cbdec4123c940510b68c1e179d8f8efbd8aae Mon Sep 17 00:00:00 2001 From: "mascip (Pierre Masci)" Date: Tue, 21 May 2013 14:57:42 +0100 Subject: [PATCH 5/6] Refactored: the Alias code is in one place, and uses a "goto" --- lib/Debuggit.pm | 16 +++++++--------- t/alias.t | 1 - 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/lib/Debuggit.pm b/lib/Debuggit.pm index 1134cdb..8833303 100644 --- a/lib/Debuggit.pm +++ b/lib/Debuggit.pm @@ -110,7 +110,7 @@ This POD explains just the basics of using C. For full details, see L #### #################### main pod documentation end ################### -my ($debuggit, $add_func, $debuggit_alias); +my ($debuggit, $add_func); ##################################################################### @@ -156,8 +156,6 @@ sub import my ($pkg, %opts) = @_; my $caller_package = $opts{PolicyModule} ? caller(1) : caller; - $debuggit_alias = $opts{Alias} if defined $opts{Alias}; - my $master_debug = eval "Debuggit::DEBUG()"; my $debug_value = defined $opts{DEBUG} ? $opts{DEBUG} : defined $master_debug ? $master_debug : 0; unless (defined $master_debug) @@ -188,11 +186,15 @@ sub import else { *{ join('::', $caller_package, 'debuggit') } = sub {}; - *{ join('::', $caller_package, $debuggit_alias) } = sub {} - if $debuggit_alias; # Alias for the debuggit() function *Debuggit::add_func = sub {} unless Debuggit->can('add_func'); } + # User-defined Alias for the debuggit() function + if ( my $debuggit_alias = $opts{Alias} ) { + *{ join('::', $caller_package, $debuggit_alias) } + = sub { goto &{ join('::', $caller_package, 'debuggit') } } + } + } # End import() @@ -215,14 +217,10 @@ sub _setup_funcs { *Debuggit::debuggit = eval $debuggit unless Debuggit->can('debuggit'); *{ join('::', $caller_package, 'debuggit') } = \&debuggit; - *{ join('::', $caller_package, $debuggit_alias) } = \&debuggit - if $debuggit_alias; # Alias for the debuggit() function } else { *{ join('::', $caller_package, 'debuggit') } = eval $debuggit; - *{ join('::', $caller_package, $debuggit_alias) } = eval $debuggit - if $debuggit_alias; # Alias for the debuggit() function } unless (Debuggit->can('add_func')) diff --git a/t/alias.t b/t/alias.t index de58887..f8c0915 100644 --- a/t/alias.t +++ b/t/alias.t @@ -1,7 +1,6 @@ my $DESC = ''; ## Without Alias -package WithoutAlias; use Debuggit; use strict; From b5551da817d098d5b8472a5e698cf264d96a51e9 Mon Sep 17 00:00:00 2001 From: "mascip (Pierre Masci)" Date: Tue, 21 May 2013 15:14:15 +0100 Subject: [PATCH 6/6] Added tests for Alias feature, for override and fallthrough --- t/fallthrough.t | 12 ++++++++++-- t/override.t | 13 ++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/t/fallthrough.t b/t/fallthrough.t index 10b90d9..b815755 100644 --- a/t/fallthrough.t +++ b/t/fallthrough.t @@ -13,19 +13,27 @@ eval { use strict; use warnings; - use Debuggit; + use Debuggit(Alias => 'dbg'); sub test { debuggit(2 => $_[0]); } + sub test_with_alias + { + dbg(2 => $_[0]); + } + 1; }; my $output = 'expected output'; -stderr_is { Fallthrough::test($output); } "$output\n", "got fallthrough output"; +stderr_is { Fallthrough::test($output); } "$output\n", + "got fallthrough output"; +stderr_is { Fallthrough::test_with_alias($output); } "$output\n", + "got fallthrough output with_alias"; done_testing; diff --git a/t/override.t b/t/override.t index 4fae7ed..a10634b 100644 --- a/t/override.t +++ b/t/override.t @@ -13,7 +13,7 @@ eval { use strict; use warnings; - use Debuggit DEBUG => 2; + use Debuggit DEBUG => 2, Alias => 'dbg'; sub print_it { @@ -25,6 +25,11 @@ eval { debuggit(2 => $_[0]); } + sub test_with_alias + { + dbg(2 => $_[0]); + } + 1; }; @@ -32,7 +37,9 @@ eval { stdout_is { Override::print_it() } 'DEBUG is 2', "DEBUG overrides successfully"; my $output = 'expected output'; -stderr_is { Override::test($output) } "$output\n", "got override output"; - +stderr_is { Override::test($output) } "$output\n", + "got override output"; +stderr_is { Override::test_with_alias($output) } "$output\n", + "got override output, with Alias"; done_testing;