http://www.perlmonks.org?node_id=819525

Random_Walk has asked for the wisdom of the Perl Monks concerning the following question:

Wise monks, and the rest ;)

I have a logging routine from a supplier that has the prototype ($$). The first parameter is the log level, the second the message to log. I am overriding this sub already to add a debug feature and all is good. Now I would like to change the prototype to ($@) and then convert the @ with a join before calling the original logger.

# Patch nimlog to echo to STDOUT in debug { no warnings qw(redefine); sub nimLog ($@) { my $msg = join ', ', @_; Nimbus::API::nimLog( $_[0], $msg ); say $msg if $main::debug and $main::setup->{loglevel} >= $_[1] +; } }

of course now I get the error Prototype mismatch. Is there a way to do this or should I just behave and use a new name for my new logger?

Thanks,
R.

Pereant, qui ante nos nostra dixerunt!

Replies are listed 'Best First'.
Re: override prototype
by ikegami (Patriarch) on Jan 25, 2010 at 16:45 UTC

    First, why are you using a prototype at all?

    Second, why do you import nimLog if you're going to replace the imported symbol?

    Finally, the so-called errors is simply an expected warning that can be silenced by changing

    no warnings qw(redefine);
    to
    no warnings qw( redefine prototype );

      I use a prototype because nimLog uses one. I was using ($$) prototype months ago when I first redefined the sub so I got no prototype warning.

      I use nimLog because modules I use from the supplier use nimLog, when debugging I can get their logging in my logfile too (though not in my STDOUT debug stream).

      Thanks for the pragma to fix the warning. I was thinking it was an error because something else in my code was causing it to die prematurely, doh!

      I guess now I suppressed the warning anyway I can also remove the prototype completely.

      Thanks for your help,
      R.

      Pereant, qui ante nos nostra dixerunt!

        I use a prototype because nimLog uses one.

        And I'm asking why it has one.

        I use nimLog because modules I use from the supplier use nimLog

        I'm asking about the module where you replace nimLog with your own function, not some other module that wasn't even mentioned. Compare

        # What you do use Nimbus::API qw( nimLog ); { no warnings ...; sub nimLog { ... Nimbus::API::nimLog( ... ); ... } }

        vs

        # Sanity restored use Nimbus::API qw( ); sub nimLog { ... Nimbus::API::nimLog( ... ); ... }

        My question stands.

Re: override prototype
by cdarke (Prior) on Jan 25, 2010 at 16:59 UTC
    The prototype ($@) is very mis-leading. It means one parameter in scalar context followed by a list which could be empy. It does not mean a scalar followed by an array
    my @fred = qw (The quick brown fox); sub mysub ($@) { print "@_\n"; } mysub(@fred)
    prints:4 but then I guess you knew that because otherwise you would not be using prototypes ;-)