in reply to Fun with Prototypes

More fun for you: ;)

Some people want debug functions to disappear without footprint in production.

Like with Smart::Comments but without source filter.

Try to find a way where changing the prototype of a function &dmp will turn the call to a no op.

Your bar /#(\w+)/, @a; is already quite close, but it's still involving a division. :)

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

Replies are listed 'Best First'.
Re^2: Fun with Prototypes
by tobyink (Canon) on Jul 22, 2017 at 11:22 UTC

    Isn't this sufficiently clear?

    use constant DEBUG => 0; use Data::Dumper; # ... warn Dumper($myvar) if DEBUG;

    Or PerX::Assert.

      > Isn't this sufficiently clear?

      > ...

      > warn Dumper($myvar) if DEBUG;

      clear but unnecessarily verbose.

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

Re^2: Fun with Prototypes
by LanX (Saint) on Jul 20, 2017 at 12:49 UTC
    an example avoiding a sub-call, unfortunately the division in void context is not optimized away.

    use warnings; use strict; use B::Deparse; use Data::Dump; use constant DEBUG =>1; BEGIN { unless (DEBUG) { *dmp = sub() { 1; } } else { *dmp = sub($;@){ shift; dd \@_; } } } sub test { #$_="#"; my @a=1..3; dmp/#/, @a; } test(); warn "DEBUG = ",DEBUG,"\n"; print B::Deparse->new()->coderef2text(\&test);

    OUTPUT

    DEBUG = 0 { use warnings; use strict; my(@a) = 1..3; 1 / @a; }

    Use of uninitialized value $_ in pattern match (m//) at ... line 27. DEBUG = 1 [1, 2, 3] { use warnings; use strict; my(@a) = 1..3; dmp /#/, @a; }

    and the warning will create runtime overhead, too :/

    update
    But I think a little overhead is acceptable when debugging.

    another edge case are empty arrays causing a division by zero...

    update:

    fixed minor bug

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