in reply to How to implement printf like functions

Hi,

Could be something like this...

sub recordStatus { if($m_bUseLog and $iLogLevel <= $m_iLogLevel) { my $format = shift; my $string = sprintf $format, @_; return length $string if(print $m_sSourceFile $string); return -1; } }

This would be something like you have, having $m_bUseLog, $iLogLevel, $m_iLogLevel, $m_sSourceFile, as global variables. Where $m_sSourceFile is the FILEHANDLE...

In the C++ as in the Perl, you should add some errors check, from input and also define a common error return (if return -> croack)...

Regards,

|fire| at irc.freenode.net

Replies are listed 'Best First'.
Re^2: How to implement printf like functions
by Tanktalus (Canon) on Aug 29, 2005 at 14:12 UTC

    Close, I think. However, given that we're coming from C++ to Perl, chances are that "m_" means "member variable". In other words, we're in an object method here.

    sub recordStatus { my $self = shift; if($self->{UseLog} and $iLogLevel <= $self->{LogLevel}) { my $format = shift; my $fh = $self->{SourceFile}; my $string = sprintf $format, @_; return length $string if(print $fh $string); return -1; } }

    Although, to be honest, the return code from the function probably doesn't matter. I've left it in as a strict interpretation of the OP would mandate it, but likely we could bypass the intermediate sprintf and just printf directly to the output, returning the boolean value that printf returns. It would change the signature by a small bit, but it would probably be a good bit. ;-) Strict porting isn't that great of an idea in the long term anyway.

      Hi,

      Oh, yeah, I forgot the ++ from the C ;-)

      Regards,

      |fire| at irc.freenode.net