.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Required to disable full justification in groff 1.23.0. .if n .ds AD l .\" ======================================================================== .\" .IX Title "Log::Report::Message 3" .TH Log::Report::Message 3 2026-06-18 "perl v5.42.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Log::Report::Message \- a piece of text to be translated .SH INHERITANCE .IX Header "INHERITANCE" .Vb 2 \& Log::Report::Message is extended by \& Dancer2::Plugin::LogReport::Message .Ve .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 2 \& # Objects created by Log::Report\*(Aqs _\|_ functions \& # Full feature description in the DETAILS section \& \& # no interpolation \& _\|_"Hello, World"; \& \& # with interpolation \& _\|_x"age {years}", years => 12; \& \& # interpolation for one or many \& my $nr_files = @files; \& _\|_nx"one file", "{_count} files", $nr_files; \& _\|_nx"one file", "{_count} files", \e@files; \& \& # interpolation of arrays \& _\|_x"price\-list: {prices%.2f}", prices => \e@prices, _join => \*(Aq, \*(Aq; \& \& # white\-spacing on msgid preserved \& print _\|_"\etCongratulations,\en"; \& print "\et", _\|_("Congratulations,"), "\en"; # same \& \& # Object serialization support \& my $clone = Log::Report::Message\->thaw($msg\->freeze); .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Any use of a constructor function exported by Log::Report, like \&\f(CW\*(C`_\|_()\*(C'\fR (the function is named underscore\-underscore) or \f(CW\*(C`_\|_x()\*(C'\fR (underscore\-underscore\-x) will result in this Message object. It will capture some environmental information as well. .PP The optional translation is delayed until it is really needed. Creating an object first and translating it later, might be slower than translating it immediately. However (by design decissions of Log::Report) on the location where the message is produced, we do not yet know in what language to translate it to: that depends on the actual log dispatcher configurations in the main program. .SH OVERLOADED .IX Header "OVERLOADED" .IP "overload: \fB\*(Aq""""\*(Aq\fR stringification" 4 .IX Item "overload: '""""' stringification" When the object is used in string context, it will get translated. Implemented as \fBtoString()\fR. .IP "overload: \fB\*(Aq&{}\*(Aq\fR used as function" 4 .IX Item "overload: '&{}' used as function" When the object is called as function, a new object is created with the data from the original one but updated with the new parameters. Implemented in \f(CWclone()\fR. .IP "overload: \fB\*(Aq.\*(Aq\fR concatenation" 4 .IX Item "overload: '.' concatenation" An (accidental) use of concatenation (a dot where a comma should be used) would immediately stringify the object. This is avoided by overloading that operation. .SH METHODS .IX Header "METHODS" .SS Constructors .IX Subsection "Constructors" .ie n .IP "$obj\->\fBclone\fR(%options, $variables)" 4 .el .IP "\f(CW$obj\fR\->\fBclone\fR(%options, \f(CW$variables\fR)" 4 .IX Item "$obj->clone(%options, $variables)" Returns a new object which copies info from original, and updates it with the specified \f(CW%options\fR and \f(CW$variables\fR. The advantage is that the cached translations are shared between the objects. .Sp » example: use of \fBclone()\fR .Sp .Vb 5 \& my $s = _\|_x "found {nr} files", nr => 5; \& my $t = $s\->clone(nr => 3); \& my $t = $s\->(nr => 3); # equivalent \& print $s; # found 5 files \& print $t; # found 3 files .Ve .ie n .IP $class\->\fBnew\fR(%options) 4 .el .IP \f(CW$class\fR\->\fBnew\fR(%options) 4 .IX Item "$class->new(%options)" \&\fBEnd\-users: do not use this method directly\fR, but use Log::Report::_\|_(), Log::Report::_\|\fB_x()\fR and friends. The \f(CW%options\fR is a mixed list of object initiation parameters (all with a leading underscore) and variables to be filled in into the translated \f(CW\*(C`_msgid\*(C'\fR string. .Sp .Vb 10 \& \-Option \-\-Default \& _append undef \& _category undef \& _class undef \& _classes undef \& _context undef \& _count undef \& _domain \& _expand false \& _join $" $LIST_SEPARATOR \& _lang \& _msgctxt undef \& _msgid undef \& _plural undef \& _prepend undef \& _tag [] \& _tags [] \& _to undef .Ve .RS 4 .ie n .IP "_append => $text|$message" 2 .el .IP "_append => \f(CW$text\fR|$message" 2 .IX Item "_append => $text|$message" Some \f(CW$text\fR or other \f(CW$message\fR object which need to be pasted after this message object. .IP "_category => INTEGER" 2 .IX Item "_category => INTEGER" The category when the real gettext library is used, for instance LC_MESSAGES. .ie n .IP "_class => $tags|\e@tags" 2 .el .IP "_class => \f(CW$tags\fR|\e@tags" 2 .IX Item "_class => $tags|@tags" Deprecated alternative for \f(CW\*(C`_tag\*(C'\fR. .ie n .IP "_classes => $tags|\e@tags" 2 .el .IP "_classes => \f(CW$tags\fR|\e@tags" 2 .IX Item "_classes => $tags|@tags" Deprecated alternative for \f(CW\*(C`_tag\*(C'\fR. .ie n .IP "_context => $keyword|\e@keywords" 2 .el .IP "_context => \f(CW$keyword\fR|\e@keywords" 2 .IX Item "_context => $keyword|@keywords" [1.00] Set the \f(CW@keywords\fR which can be used to select alternatives between translations. Read the DETAILS section in Log::Report::Translator::Context .IP "_count => INTEGER|ARRAY|HASH" 2 .IX Item "_count => INTEGER|ARRAY|HASH" When defined, the \f(CW\*(C`_plural\*(C'\fR need to be defined as well. When an ARRAY is provided, the length of the ARRAY is taken. When a HASH is given, the number of keys in the HASH is used. .ie n .IP "_domain => $name|$object" 2 .el .IP "_domain => \f(CW$name\fR|$object" 2 .IX Item "_domain => $name|$object" The text\-domain (translation table) to which this \f(CW\*(C`_msgid\*(C'\fR belongs. .Sp With this parameter, your can "borrow" translations from other textdomains. Be very careful with this (although there are good use\-cases) The xgettext msgid extractor may add the used msgid to this namespace as well. To avoid that, add a harmless \*(Aq+\*(Aq: .Sp .Vb 1 \& print _\|_x(+"errors", _domain => \*(Aqglobal\*(Aq); .Ve .Sp The extractor will not take the msgid when it is an expression. The \*(Aq+\*(Aq has no effect on the string at runtime. .IP "_expand => BOOLEAN" 2 .IX Item "_expand => BOOLEAN" Indicates whether variables are to be filled\-in; whether \f(CW\*(C`_\|_x\*(C'\fR or \f(CW\*(C`_\|_\*(C'\fR was used to define the message. .ie n .IP "_join => $separator" 2 .el .IP "_join => \f(CW$separator\fR" 2 .IX Item "_join => $separator" Which \f(CW$separator\fR string to be used then an ARRAY is being filled\-in. .IP "_lang => ISO" 2 .IX Item "_lang => ISO" [1.00] Override language setting from locale, for instance because that is not configured correctly (yet). This does not extend to prepended or appended translated message object. .ie n .IP "_msgctxt => $context" 2 .el .IP "_msgctxt => \f(CW$context\fR" 2 .IX Item "_msgctxt => $context" [1.22] Message \f(CW$context\fR in the translation file, the traditional use. Cannot be combined with \f(CW\*(C`_context\*(C'\fR on the same msgids. .ie n .IP "_msgid => $msgid" 2 .el .IP "_msgid => \f(CW$msgid\fR" 2 .IX Item "_msgid => $msgid" The message label, which refers to some translation information. Usually a string which is close the English version of the message. This will also be used if there is no translation possible/known. .Sp Leading white\-space \f(CW\*(C`\es\*(C'\fR will be added to \f(CW\*(C`_prepend\*(C'\fR. Trailing white\-space will be added before \f(CW\*(C`_append\*(C'\fR. .ie n .IP "_plural => $msgid" 2 .el .IP "_plural => \f(CW$msgid\fR" 2 .IX Item "_plural => $msgid" Can be used together with \f(CW\*(C`_count\*(C'\fR. This plural form of the \f(CW\*(C`_msgid\*(C'\fR text is used to simplify the work of translators, and as fallback when no translation is possible: therefore, this can best resemble an English message. .Sp White\-space at the beginning and end of the string are stripped off. The white\-space provided by the \f(CW\*(C`_msgid\*(C'\fR will be used. .ie n .IP "_prepend => $text|$message" 2 .el .IP "_prepend => \f(CW$text\fR|$message" 2 .IX Item "_prepend => $text|$message" Some \f(CW$text\fR or other \f(CW$message\fR object which need to be glued before this message object. .ie n .IP "_tag => $tags|\e@tags" 2 .el .IP "_tag => \f(CW$tags\fR|\e@tags" 2 .IX Item "_tag => $tags|@tags" When messages are used for exception based programming, you add a \&\f(CW\*(C`_tag\*(C'\fR parameter to the argument list. Later, with for instance Log::Report::Dispatcher::Try::wasFatal(tag), you can check the category (group, class) of the exception. .Sp The \f(CW$tags\fR is interpreted as comma\- and/or blank separated list of class tokens (barewords), the ARRAY lists all tags separately. See \fBtags()\fR. .ie n .IP "_tags => $tags|\e@tags" 2 .el .IP "_tags => \f(CW$tags\fR|\e@tags" 2 .IX Item "_tags => $tags|@tags" Alternative name for \f(CW\*(C`_tag\*(C'\fR .ie n .IP "_to => $dispatcher" 2 .el .IP "_to => \f(CW$dispatcher\fR" 2 .IX Item "_to => $dispatcher" Specify the \f(CW$dispatcher\fR as destination explicitly. Short for \f(CW\*(C`report {to => NAME}, ...\*(C'\fR See \fBto()\fR .RE .RS 4 .RE .SS Attributes .IX Subsection "Attributes" .ie n .IP $obj\->\fBaddTags\fR(@tags) 4 .el .IP \f(CW$obj\fR\->\fBaddTags\fR(@tags) 4 .IX Item "$obj->addTags(@tags)" [1.45] Add zero or more tags to this message. .ie n .IP $obj\->\fBappend\fR() 4 .el .IP \f(CW$obj\fR\->\fBappend\fR() 4 .IX Item "$obj->append()" Returns the string or Log::Report::Message object which is appended after this one. Usually \f(CW\*(C`undef\*(C'\fR. .ie n .IP $obj\->\fBclasses\fR() 4 .el .IP \f(CW$obj\fR\->\fBclasses\fR() 4 .IX Item "$obj->classes()" Deprecated alternative for \fBtags()\fR. .ie n .IP $obj\->\fBcontext\fR() 4 .el .IP \f(CW$obj\fR\->\fBcontext\fR() 4 .IX Item "$obj->context()" Returns an HASH if there is a context defined for this message. .ie n .IP $obj\->\fBcount\fR() 4 .el .IP \f(CW$obj\fR\->\fBcount\fR() 4 .IX Item "$obj->count()" Returns the count, which is used to select the translation alternatives. .ie n .IP $obj\->\fBdomain\fR() 4 .el .IP \f(CW$obj\fR\->\fBdomain\fR() 4 .IX Item "$obj->domain()" Returns the domain of the first translatable string in the structure. .ie n .IP "$obj\->\fBerrno\fR( [$errno] )" 4 .el .IP "\f(CW$obj\fR\->\fBerrno\fR( [$errno] )" 4 .IX Item "$obj->errno( [$errno] )" [1.38] Returns the value of the \f(CW\*(C`_errno\*(C'\fR key, to indicate the error number (to be returned from your script). Usually, this method will return \f(CW\*(C`undef\*(C'\fR. For FAILURE, FAULT, and ALERT, the errno is by default taken from \f(CW$!\fR and \f(CW$?\fR. .ie n .IP $obj\->\fBmsgctxt\fR() 4 .el .IP \f(CW$obj\fR\->\fBmsgctxt\fR() 4 .IX Item "$obj->msgctxt()" The message context for the translation table lookup. .ie n .IP $obj\->\fBmsgid\fR() 4 .el .IP \f(CW$obj\fR\->\fBmsgid\fR() 4 .IX Item "$obj->msgid()" Returns the msgid which will later be translated. .ie n .IP $obj\->\fBprepend\fR() 4 .el .IP \f(CW$obj\fR\->\fBprepend\fR() 4 .IX Item "$obj->prepend()" Returns the string which is prepended to this one. Usually \f(CW\*(C`undef\*(C'\fR. .ie n .IP $obj\->\fBtags\fR() 4 .el .IP \f(CW$obj\fR\->\fBtags\fR() 4 .IX Item "$obj->tags()" [1.44] Returns the LIST of tags which are defined for this message; message group indicators, as often found in exception\-based programming. .ie n .IP "$obj\->\fBto\fR( [$name] )" 4 .el .IP "\f(CW$obj\fR\->\fBto\fR( [$name] )" 4 .IX Item "$obj->to( [$name] )" Returns the \f(CW$name\fR of a dispatcher if explicitly specified with the \*(Aq_to\*(Aq key. Can also be used to set it. Usually, this will return \f(CW\*(C`undef\*(C'\fR, because usually all dispatchers get all messages. .ie n .IP $obj\->\fBvalueOf\fR($parameter) 4 .el .IP \f(CW$obj\fR\->\fBvalueOf\fR($parameter) 4 .IX Item "$obj->valueOf($parameter)" Lookup the named \f(CW$parameter\fR for the message. All pre\-defined names have their own method which should be used with preference. .Sp » example: .Sp When the message was produced with .Sp .Vb 10 \& my @files = qw/one two three/; \& my $msg = _\|_xn \& "found one file: {file}", \& "found {nrfiles} files: {files}", \& scalar @files, \& file => $files[0], \& files => \e@files, \& nrfiles => @files+0, # or scalar(@files) \& _tags => [ \*(AqIO\*(Aq, \*(Aqfiles\*(Aq ], \& _join => \*(Aq, \*(Aq; .Ve .Sp then the values can be takes from the produced message as .Sp .Vb 5 \& my $files = $msg\->valueOf(\*(Aqfiles\*(Aq); # returns ARRAY reference \& print @$files; # 3 \& my $count = $msg\->count; # 3 \& my @tags = $msg\->tags; # \*(AqIO\*(Aq, \*(Aqfiles\*(Aq \& if($msg\->taggedWith(\*(Aqfiles\*(Aq)) # true .Ve .Sp Simplified, the above example can also be written as: .Sp .Vb 7 \& local $" = \*(Aq, \*(Aq; # Perl default \& my $msg = _\|_xn \& "found one file: {files}", \& "found {_count} files: {files}", \& @files, # has scalar context \& files => \e@files, \& _tags => \*(AqIO, files\*(Aq; .Ve .SS Processing .IX Subsection "Processing" .ie n .IP "$obj\->\fBconcat\fR( $text|$message, [$prepend] )" 4 .el .IP "\f(CW$obj\fR\->\fBconcat\fR( \f(CW$text\fR|$message, [$prepend] )" 4 .IX Item "$obj->concat( $text|$message, [$prepend] )" This method implements the overloading of concatenation, which is used to delay translations even longer. When \f(CW$prepend\fR is \f(CW\*(C`true\*(C'\fR, the \f(CW$text\fR or \f(CW$message\fR (another \f(CW\*(C`Log::Report::Message\*(C'\fR) will be prepended, otherwise it is appended in the final display. .Sp » example: of concatenation .Sp .Vb 2 \& print _\|_"Hello" . \*(Aq \*(Aq . _\|_"World!\en"; \& print _\|_("Hello")\->concat(\*(Aq \*(Aq)\->concat(_\|_"World!")\->concat("\en"); .Ve .ie n .IP $obj\->\fBfreeze\fR(%options) 4 .el .IP \f(CW$obj\fR\->\fBfreeze\fR(%options) 4 .IX Item "$obj->freeze(%options)" Convert the data of this message into a (nested) HASH which optimally preserves its content. Pass this to \fBthaw()\fR to get the object back. .Sp The returned HASH can be serialized safely with (for instance) YAML or JSON. Then, it can be reconstructed from the de\-serialized data. At the moment, all facts can be preserved. .Sp .Vb 2 \& \-Option \-\-Default \& serializer undef .Ve .RS 4 .IP "serializer => \*(AqJSON\*(Aq|\*(AqSerial\*(Aq|..." 2 .IX Item "serializer => 'JSON'|'Serial'|..." In the future, the specific serializer may influence the frozen output. .RE .RS 4 .Sp » example: to thaw a frozen .Sp .Vb 1 \& my $clone = Log::Report::Message\->thaw($msg\->freeze); .Ve .RE .ie n .IP $obj\->\fBinClass\fR($tag|Regexp) 4 .el .IP \f(CW$obj\fR\->\fBinClass\fR($tag|Regexp) 4 .IX Item "$obj->inClass($tag|Regexp)" Deprecated alternative for \fBtaggedWith()\fR. .ie n .IP $obj\->\fBtaggedWith\fR($tag|Regexp) 4 .el .IP \f(CW$obj\fR\->\fBtaggedWith\fR($tag|Regexp) 4 .IX Item "$obj->taggedWith($tag|Regexp)" [1.44] Returns \f(CW\*(C`true\*(C'\fR if the message carries the specified \f(CW$tag\fR (string) or matches the Regexp. The trueth value is the (first matching) tag. .ie n .IP "$class\->\fBthaw\fR(\e%data, %options)" 4 .el .IP "\f(CW$class\fR\->\fBthaw\fR(\e%data, \f(CW%options\fR)" 4 .IX Item "$class->thaw(%data, %options)" .ie n .IP "$obj\->\fBtoHTML\fR( [$locale] )" 4 .el .IP "\f(CW$obj\fR\->\fBtoHTML\fR( [$locale] )" 4 .IX Item "$obj->toHTML( [$locale] )" [1.11] Translate the message, and then entity encode HTML volatile characters. .Sp [1.20] When used in combination with a templating system, you may want to use \f(CW\*(C`> in Log::Report::Domain::configure(formatter). .Sp » example: .Sp .Vb 1 \& print $msg\->toHTML(\*(AqNL\*(Aq); .Ve .ie n .IP "$obj\->\fBtoString\fR( [$locale] )" 4 .el .IP "\f(CW$obj\fR\->\fBtoString\fR( [$locale] )" 4 .IX Item "$obj->toString( [$locale] )" Translate a message. If not specified, the default locale is used. .ie n .IP $obj\->\fBuntranslated\fR() 4 .el .IP \f(CW$obj\fR\->\fBuntranslated\fR() 4 .IX Item "$obj->untranslated()" Return the concatenation of the prepend, msgid, and append strings. Variable expansions within the msgid is not performed. .SH DETAILS .IX Header "DETAILS" The message cast by the Log::Report exception framework can be plain strings. This is sufficient for some cases, for instance: which would you bother much about the content of \f(CW\*(C`trace\*(C'\fR messages? Message which are destined for end\-users or log\-files however, need more care; require a higher quality. In this case, you can better use cast message objects which support interpolation. .SS Message\-IDs .IX Subsection "Message-IDs" An exception message as a string is pretty inflexible. It cannot be translated anymore, and it cannot contain related information like an error code. An exception message as an object (as this module implements) is able to contain additional information. .PP The easiest way to create Log::Report::Message objects, is via the \&\f(CW\*(C`_\|_x()\*(C'\fR (and friends) functions which are exported by Log::Report. These are nearly equivalent: .PP .Vb 2 \& my $msg = Log::Report::Message\->new(_msgid => "Hello, World!"); \& my $msg = _\|_"Hello, World!"; .Ve .PP The name \f(CW\*(C`msgid\*(C'\fR is used, because this object can do translations. In the GNU gettext library for translations, the term \f(CW\*(C`msgid\*(C'\fR is used to describe the string which has to be looked\-up in translation tables. When there are no tables, or the \f(CW\*(C`msgid\*(C'\fR is not found, then the output string is equivalent to the \f(CW\*(C`msgid\*(C'\fR. .PP So, without any translation configuration, this happens: .PP .Vb 2 \& my $msg = _\|_"Hello, World!"; \& print "$msg\en"; # Hello, World!⏎ .Ve .PP The \f(CW\*(C`_\|_()\*(C'\fR function can be used with a static string, although you could do this: .PP .Vb 2 \& my $msg = _\|_"Hello $name!"; #XXX Don\*(Aqt do this! \& print "$msg\en"; # Hello Mark!⏎ .Ve .PP With the \f(CW\*(C`_\|_x()\*(C'\fR or \f(CW\*(C`_\|_nx()\*(C'\fR, interpolation will take place on the (optionally) translated \f(CW\*(C`msgid\*(C'\fR string. This is how to write above: .PP .Vb 1 \& my $msg = _\|_x"Hello {name}!", name => $name; .Ve .PP So: the \f(CW\*(C`msgid\*(C'\fR is usually a \fIformat string\fR. Only when you use formats correctly, you will be able to introduce translation later. .SS "Why use format strings?" .IX Subsection "Why use format strings?" Simple perl scripts will use \f(CWprint()\fR with variables in the string. However, when the content of the variable gets more unpredictable or needs some pre\-processing, then it gets tricky. When you do want to introduce translations (in the far future of your successful project) it gets impossible. Let me give you some examples: .PP .Vb 1 \& print "product: $name\en"; # simple perl \& \& # Will not work because "$name" is interpolated too early \& print translate("product: $name"), "\en"; \& \& # This is the gettext solution, with formats \& printf translate("product: %s\en"), $name; \& \& # With named in stead of positional parameters \& print translate("product: {p}\en", p => $name); \& \& # With Log::Report, the translate() is hidden in _\|_x() \& print _\|_x"product: {p}\en", p => $name; .Ve .PP Besides making translation possible, interpolation via format strings is much cleaner than in the simpelest perl way. For instance, these cases: .PP .Vb 4 \& # Safety measures while interpolation \& my $name = undef; \& print "product: $name\en"; # uninitialized warning \& print _\|_x"product: {p}\en", p => $name; # \-\-> product: undef \& \& # Interpolation of more complex data \& my @names = qw/a b c/; \& print "products: ", join(\*(Aq, \*(Aq, @names), "\en"; \& print _\|_x"products: {p}\en", p => \e@names; \& \& # Padded values hard to do without format strings \& print "padded counter: ", \*(Aq \*(Aq x (6\-length $c), "$c\en"; \& printf "padded counter: %6d\en", $counter; \& print _\|_x"padded counter: {c%6d}\en", c => $counter; .Ve .PP So: using formats has many advantages. Advice: use simple perl only in \&\f(CW\*(C`trace\*(C'\fR and \f(CW\*(C`assert\*(C'\fR messages, maybe also with panics. For serious output of your program, use formatted output. .SS "Messages with plural forms" .IX Subsection "Messages with plural forms" The Log::Report::_\|\fB_xn()\fR message constructor is used when you need a different translation based on the count of one of the inserted fields. .PP .Vb 2 \& fault _\|_x"cannot read {file}", file => $fn; \& # \-\-> FAULT: cannot read /etc/shadow: Permission denied\en \& \& print _\|_xn"directory {dir} contains one file", \& "directory {dir} contains {nr_files} files", \& scalar(@files), # (1) (2) (3) \& nr_files => scalar @files, # (4) \& dir => $dir; .Ve .PP (1) this required third parameter is used to switch between the different plural forms. English has only two forms, but some languages have many more. .PP (2) the "scalar" keyword is not needed, because the third parameter is in SCALAR context. You may also pass \f(CW\*(C` \e@files \*(C'\fR there, because ARRAYs will be converted into their length. A HASH will be converted into the number of keys in the HASH. .PP (3) you could also simply pass a reference to the ARRAY: it will take the length as counter. With a HASH, it will count the number of keys. .PP (4) the \f(CW\*(C`scalar\*(C'\fR keyword is required here, because it is LIST context: otherwise all filenames will be filled\-in as parameters to \f(CW\*(C`_\|_xn()\*(C'\fR. See below for the available \f(CW\*(C`_count\*(C'\fR value, to see how the \f(CW\*(C`nr_files\*(C'\fR parameter can disappear. .PP .Vb 3 \& print _\|_xn"directory {dir} contains one file", \& "directory {dir} contains {_count} files", \& \e@files, dir => $dir; .Ve .PP Some languages need more than two translations based on the counter. This is solved by the translation table definition. The two msgids give here are simply the fallback, when there is not translation table active. .PP \fIInterpolation with String::Print\fR .IX Subsection "Interpolation with String::Print" .PP There is no way of checking beforehand whether you have provided all required values, to be interpolated in the translated string. .PP This Log::Report::Message uses String::Print to handle formatted strings. On object of that module is hidden in the logic of \f(CW\*(C`_\|_x()\*(C'\fR and friends. .PP String::Print is a very capable format string processor, and you should really \fBread its manual\fR page to see how it can help you. It would be possible to support an other formatter (pretty simple even), but this is not (yet) supported. .PP » Example: using format features .IX Subsection "» Example: using format features" .PP .Vb 5 \& # This tries to display the $param as useful and safe as possible, \& # where you have totally no idea what its contents is. \& error _\|_x"illegal parameter {p UNKNOWN}.", p => $param; \& # \-\-\-> "illegal parameter \*(Aqaccidentally passed text\*(Aq." \& # \-\-\-> "illegal parameter Unexpected::Object::Type." \& \& # fault() adds ": $!", with $! translated when configured. \& open my($fh), "<:encoding(utf\-8)", $filename \& or fault _\|_x"cannot read {file}", file => $filename; \& \& # Auto\-abbreviation \& trace _\|_x"first lines: \*(Aq{text EL}\*(Aq\en", text => $t; \& # \-\-\-> "first lines: \*(AqThis text is long, we sho⋯\*(Aq.\en" \& \& trace _\|_x"first lines: {text CHOP}\en", text => $t; \& # \-\-\-> "This text is long, we [+3712 chars]\en" \& \& info _\|_x"file {file} size {size BYTES}\en", file => $fn, size => \-s $fn; \& # \-\-> "/etc/passwd size 23kB\en" \& \& # HASH or object values \& print _\|_x"Name: {user.first} {user.surname}\en", user => $login; .Ve .PP There are more nice standard interpolation modifiers, and you can add your own. Besides, you can add serializers which determine how objects are inlined. .SS "Automatic parameters" .IX Subsection "Automatic parameters" Besides the parameters which you specify yourself, Log::Report will add a few which can also be interpolated. The all start with an underscore (\f(CW\*(C`_\*(C'\fR). These are collected when this Message object is instantiated, see the \f(CW%options\fR of \fBnew()\fR. These parameters have a purpose, but you are also permitted tp interpolate them in your message. This may simplify your coding. .PP The useful names are: .IP _msgid 4 .IX Item "_msgid" The MSGID as provided with Log::Report::_\|_() and Log::Report::_\|\fB_x()\fR .IP "_plural, _count" 4 .IX Item "_plural, _count" The plural (second) msgid, respectively the counter value as used with Log::Report::_\|\fB_n()\fR and Log::Report::_\|\fB_nx()\fR .IP _textdomain 4 .IX Item "_textdomain" The label of the textdomain in which the translation takes place. .IP _join 4 .IX Item "_join" The string which is used between elements of an ARRAY, when it gets interpolated in a single field. .IP _tags 4 .IX Item "_tags" [1.44] Tags are to be used to group exceptions, and can be queried with \fBtaggedWith()\fR, \&\fBLog::Report::Exception::taggedWith()\fR, or Log::Report::Dispatcher::Try::wasFatal(tag). .PP » Example: using the _count .IX Subsection "» Example: using the _count" .PP With Locale::TextDomain, you have to do .PP .Vb 7 \& use Locale::TextDomain; \& print _\|_nx( \& "One file has been deleted.\en", \& "{num} files have been deleted.\en", \& $num_files, \& num => $num_files, \& ); .Ve .PP With \f(CW\*(C`Log::Report\*(C'\fR, you can do .PP .Vb 6 \& use Log::Report; \& print _\|_nx( \& "One file has been deleted.\en", \& "{_count} files have been deleted.\en", \& $num_files, \& ); .Ve .PP Of course, you need to be aware that the name used to reference the counter is fixed to \f(CW\*(C`_count\*(C'\fR. The first example works as well, but is more verbose. .PP \fIHandling white\-spaces\fR .IX Subsection "Handling white-spaces" .PP In above examples, the msgid and plural form have a trailing new\-line. In general, it is much easier to write .PP .Vb 1 \& print _\|_x"Hello, World!\en"; .Ve .PP than .PP .Vb 1 \& print _\|_x("Hello, World!") . "\en"; .Ve .PP For the translation tables, however, that trailing new\-line is "ignorable information"; it is an layout issue, not a translation issue. .PP Therefore, the first form will automatically be translated into the second. All leading and trailing white\-space (blanks, new\-lines, tabs, \&...) are removed from the msgid before the look\-up, and then added to the translated string. .PP Leading and trailing white\-space on the plural form will also be removed. However, after translation the spacing of the msgid will be used. .PP \fIAvoiding repetative translations\fR .IX Subsection "Avoiding repetative translations" .PP This way of translating is somewhat expensive, because an object to handle the \f(CW\*(C`Log::Report::_\|_x()\*(C'\fR is created each time. .PP .Vb 3 \& for my $i (1..100_000) \& { print _\|_x "Hello World {i}\en", i => $i; \& } .Ve .PP The suggestion that Locale::TextDomain makes to improve performance, is to get the translation outside the loop, which only works without interpolation: .PP .Vb 6 \& use Locale::TextDomain; \& my $i = 42; \& my $s = _\|_x("Hello World {i}\en", i => $i); \& foreach $i (1..100_000) \& { print $s; \& } .Ve .PP \&\fBOops,\fR not what you mean because the first value of \f(CW$i\fR is captured in the initial message object. With Log::Report, you can do it (except when you use contexts) .PP .Vb 6 \& use Log::Report; \& my $i; \& my $s = _\|_x("Hello World {i}\en", i => \e$i); \& foreach $i (1..100_000) \& { print $s; \& } .Ve .PP Mind you not to write: \f(CW\*(C`for my $i\*(C'\fR in above case!!!! .PP You can also write an incomplete translation: .PP .Vb 5 \& use Log::Report; \& my $s = _\|_x "Hello World {i}\en"; \& foreach my $i (1..100_000) \& { print $s\->(i => $i); \& } .Ve .PP In either case, the translation will be looked\-up only once. .SH "SEE ALSO" .IX Header "SEE ALSO" This module is part of Log\-Report version 1.46, built on June 16, 2026. Website: \fIhttp://perl.overmeer.net/CPAN/\fR .SH LICENSE .IX Header "LICENSE" For contributors see file ChangeLog. .PP This software is copyright (c) 2007\-2026 by Mark Overmeer. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.