Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

override prototype

by Random_Walk (Prior)
on Jan 25, 2010 at 16:16 UTC ( #819525=perlquestion: print w/replies, xml ) Need Help??
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?


Pereant, qui ante nos nostra dixerunt!

Replies are listed 'Best First'.
Re: override prototype
by ikegami (Pope) 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);
    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,

      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( ... ); ... } }


        # 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)
    but then I guess you knew that because otherwise you would not be using prototypes ;-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://819525]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2018-06-21 05:43 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.