• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1@c This file is part of the GNU gettext manual.
2@c Copyright (C) 1995-2020 Free Software Foundation, Inc.
3@c See the file gettext.texi for copying conditions.
4
5@node Perl
6@subsection Perl
7@cindex Perl
8
9@table @asis
10@item RPMs
11perl
12
13@item Ubuntu packages
14perl, libintl-perl
15
16@item File extension
17@code{pl}, @code{PL}, @code{pm}, @code{perl}, @code{cgi}
18
19@item String syntax
20@itemize @bullet
21
22@item @code{"abc"}
23
24@item @code{'abc'}
25
26@item @code{qq (abc)}
27
28@item @code{q (abc)}
29
30@item @code{qr /abc/}
31
32@item @code{qx (/bin/date)}
33
34@item @code{/pattern match/}
35
36@item @code{?pattern match?}
37
38@item @code{s/substitution/operators/}
39
40@item @code{$tied_hash@{"message"@}}
41
42@item @code{$tied_hash_reference->@{"message"@}}
43
44@item etc., issue the command @samp{man perlsyn} for details
45
46@end itemize
47
48@item gettext shorthand
49@code{__} (double underscore)
50
51@item gettext/ngettext functions
52@code{gettext}, @code{dgettext}, @code{dcgettext}, @code{ngettext},
53@code{dngettext}, @code{dcngettext}, @code{pgettext}, @code{dpgettext},
54@code{dcpgettext}, @code{npgettext}, @code{dnpgettext},
55@code{dcnpgettext}
56
57@item textdomain
58@code{textdomain} function
59
60@item bindtextdomain
61@code{bindtextdomain} function
62
63@item bind_textdomain_codeset
64@code{bind_textdomain_codeset} function
65
66@item setlocale
67Use @code{setlocale (LC_ALL, "");}
68
69@item Prerequisite
70@code{use POSIX;}
71@*@code{use Locale::TextDomain;} (included in the package libintl-perl
72which is available on the Comprehensive Perl Archive Network CPAN,
73https://www.cpan.org/).
74
75@item Use or emulate GNU gettext
76platform dependent: gettext_pp emulates, gettext_xs uses GNU gettext
77
78@item Extractor
79@code{xgettext -k__ -k\$__ -k%__ -k__x -k__n:1,2 -k__nx:1,2 -k__xn:1,2
80-kN__ -kN__n:1,2 -k__p:1c,2 -k__np:1c,2,3 -kN__p:1c,2 -kN__np:1c,2,3}
81
82@item Formatting with positions
83Both kinds of format strings support formatting with positions.
84@*@code{printf "%2\$d %1\$d", ...} (requires Perl 5.8.0 or newer)
85@*@code{__expand("[new] replaces [old]", old => $oldvalue, new => $newvalue)}
86
87@item Portability
88The @code{libintl-perl} package is platform independent but is not
89part of the Perl core.  The programmer is responsible for
90providing a dummy implementation of the required functions if the
91package is not installed on the target system.
92
93@item po-mode marking
94---
95
96@item Documentation
97Included in @code{libintl-perl}, available on CPAN
98(https://www.cpan.org/).
99
100@end table
101
102An example is available in the @file{examples} directory: @code{hello-perl}.
103
104@cindex marking Perl sources
105
106The @code{xgettext} parser backend for Perl differs significantly from
107the parser backends for other programming languages, just as Perl
108itself differs significantly from other programming languages.  The
109Perl parser backend offers many more string marking facilities than
110the other backends but it also has some Perl specific limitations, the
111worst probably being its imperfectness.
112
113@menu
114* General Problems::            General Problems Parsing Perl Code
115* Default Keywords::            Which Keywords Will xgettext Look For?
116* Special Keywords::            How to Extract Hash Keys
117* Quote-like Expressions::      What are Strings And Quote-like Expressions?
118* Interpolation I::             Invalid String Interpolation
119* Interpolation II::            Valid String Interpolation
120* Parentheses::                 When To Use Parentheses
121* Long Lines::                  How To Grok with Long Lines
122* Perl Pitfalls::               Bugs, Pitfalls, and Things That Do Not Work
123@end menu
124
125@node General Problems
126@subsubsection General Problems Parsing Perl Code
127
128It is often heard that only Perl can parse Perl.  This is not true.
129Perl cannot be @emph{parsed} at all, it can only be @emph{executed}.
130Perl has various built-in ambiguities that can only be resolved at runtime.
131
132The following example may illustrate one common problem:
133
134@example
135print gettext "Hello World!";
136@end example
137
138Although this example looks like a bullet-proof case of a function
139invocation, it is not:
140
141@example
142open gettext, ">testfile" or die;
143print gettext "Hello world!"
144@end example
145
146In this context, the string @code{gettext} looks more like a
147file handle.  But not necessarily:
148
149@example
150use Locale::Messages qw (:libintl_h);
151open gettext ">testfile" or die;
152print gettext "Hello world!";
153@end example
154
155Now, the file is probably syntactically incorrect, provided that the module
156@code{Locale::Messages} found first in the Perl include path exports a
157function @code{gettext}.  But what if the module
158@code{Locale::Messages} really looks like this?
159
160@example
161use vars qw (*gettext);
162
1631;
164@end example
165
166In this case, the string @code{gettext} will be interpreted as a file
167handle again, and the above example will create a file @file{testfile}
168and write the string ``Hello world!'' into it.  Even advanced
169control flow analysis will not really help:
170
171@example
172if (0.5 < rand) @{
173   eval "use Sane";
174@} else @{
175   eval "use InSane";
176@}
177print gettext "Hello world!";
178@end example
179
180If the module @code{Sane} exports a function @code{gettext} that does
181what we expect, and the module @code{InSane} opens a file for writing
182and associates the @emph{handle} @code{gettext} with this output
183stream, we are clueless again about what will happen at runtime.  It is
184completely unpredictable.  The truth is that Perl has so many ways to
185fill its symbol table at runtime that it is impossible to interpret a
186particular piece of code without executing it.
187
188Of course, @code{xgettext} will not execute your Perl sources while
189scanning for translatable strings, but rather use heuristics in order
190to guess what you meant.
191
192Another problem is the ambiguity of the slash and the question mark.
193Their interpretation depends on the context:
194
195@example
196# A pattern match.
197print "OK\n" if /foobar/;
198
199# A division.
200print 1 / 2;
201
202# Another pattern match.
203print "OK\n" if ?foobar?;
204
205# Conditional.
206print $x ? "foo" : "bar";
207@end example
208
209The slash may either act as the division operator or introduce a
210pattern match, whereas the question mark may act as the ternary
211conditional operator or as a pattern match, too.  Other programming
212languages like @code{awk} present similar problems, but the consequences of a
213misinterpretation are particularly nasty with Perl sources.  In @code{awk}
214for instance, a statement can never exceed one line and the parser
215can recover from a parsing error at the next newline and interpret
216the rest of the input stream correctly.  Perl is different, as a
217pattern match is terminated by the next appearance of the delimiter
218(the slash or the question mark) in the input stream, regardless of
219the semantic context.  If a slash is really a division sign but
220mis-interpreted as a pattern match, the rest of the input file is most
221probably parsed incorrectly.
222
223There are certain cases, where the ambiguity cannot be resolved at all:
224
225@example
226$x = wantarray ? 1 : 0;
227@end example
228
229The Perl built-in function @code{wantarray} does not accept any arguments.
230The Perl parser therefore knows that the question mark does not start
231a regular expression but is the ternary conditional operator.
232
233@example
234sub wantarrays @{@}
235$x = wantarrays ? 1 : 0;
236@end example
237
238Now the situation is different.  The function @code{wantarrays} takes
239a variable number of arguments (like any non-prototyped Perl function).
240The question mark is now the delimiter of a pattern match, and hence
241the piece of code does not compile.
242
243@example
244sub wantarrays() @{@}
245$x = wantarrays ? 1 : 0;
246@end example
247
248Now the function is prototyped, Perl knows that it does not accept any
249arguments, and the question mark is therefore interpreted as the
250ternaray operator again.  But that unfortunately outsmarts @code{xgettext}.
251
252The Perl parser in @code{xgettext} cannot know whether a function has
253a prototype and what that prototype would look like.  It therefore makes
254an educated guess.  If a function is known to be a Perl built-in and
255this function does not accept any arguments, a following question mark
256or slash is treated as an operator, otherwise as the delimiter of a
257following regular expression.  The Perl built-ins that do not accept
258arguments are @code{wantarray}, @code{fork}, @code{time}, @code{times},
259@code{getlogin}, @code{getppid}, @code{getpwent}, @code{getgrent},
260@code{gethostent}, @code{getnetent}, @code{getprotoent}, @code{getservent},
261@code{setpwent}, @code{setgrent}, @code{endpwent}, @code{endgrent},
262@code{endhostent}, @code{endnetent}, @code{endprotoent}, and
263@code{endservent}.
264
265If you find that @code{xgettext} fails to extract strings from
266portions of your sources, you should therefore look out for slashes
267and/or question marks preceding these sections.  You may have come
268across a bug in @code{xgettext}'s Perl parser (and of course you
269should report that bug).  In the meantime you should consider to
270reformulate your code in a manner less challenging to @code{xgettext}.
271
272In particular, if the parser is too dumb to see that a function
273does not accept arguments, use parentheses:
274
275@example
276$x = somefunc() ? 1 : 0;
277$y = (somefunc) ? 1 : 0;
278@end example
279
280In fact the Perl parser itself has similar problems and warns you
281about such constructs.
282
283@node Default Keywords
284@subsubsection Which keywords will xgettext look for?
285@cindex Perl default keywords
286
287Unless you instruct @code{xgettext} otherwise by invoking it with one
288of the options @code{--keyword} or @code{-k}, it will recognize the
289following keywords in your Perl sources:
290
291@itemize @bullet
292
293@item @code{gettext}
294
295@item @code{dgettext:2}
296
297The second argument will be extracted.
298
299@item @code{dcgettext:2}
300
301The second argument will be extracted.
302
303@item @code{ngettext:1,2}
304
305The first (singular) and the second (plural) argument will be
306extracted.
307
308@item @code{dngettext:2,3}
309
310The second (singular) and the third (plural) argument will be
311extracted.
312
313@item @code{dcngettext:2,3}
314
315The second (singular) and the third (plural) argument will be
316extracted.
317
318@item @code{pgettext:1c,2}
319
320The first (message context) and the second argument will be extracted.
321
322@item @code{dpgettext:2c,3}
323
324The second (message context) and the third argument will be extracted.
325
326@item @code{dcpgettext:2c,3}
327
328The second (message context) and the third argument will be extracted.
329
330@item @code{npgettext:1c,2,3}
331
332The first (message context), second (singular), and third (plural)
333argument will be extracted.
334
335@item @code{dnpgettext:2c,3,4}
336
337The second (message context), third (singular), and fourth (plural)
338argument will be extracted.
339
340@item @code{dcnpgettext:2c,3,4}
341
342The second (message context), third (singular), and fourth (plural)
343argument will be extracted.
344
345@item @code{gettext_noop}
346
347@item @code{%gettext}
348
349The keys of lookups into the hash @code{%gettext} will be extracted.
350
351@item @code{$gettext}
352
353The keys of lookups into the hash reference @code{$gettext} will be extracted.
354
355@end itemize
356
357@node Special Keywords
358@subsubsection How to Extract Hash Keys
359@cindex Perl special keywords for hash-lookups
360
361Translating messages at runtime is normally performed by looking up the
362original string in the translation database and returning the
363translated version.  The ``natural'' Perl implementation is a hash
364lookup, and, of course, @code{xgettext} supports such practice.
365
366@example
367print __"Hello world!";
368print $__@{"Hello world!"@};
369print $__->@{"Hello world!"@};
370print $$__@{"Hello world!"@};
371@end example
372
373The above four lines all do the same thing.  The Perl module
374@code{Locale::TextDomain} exports by default a hash @code{%__} that
375is tied to the function @code{__()}.  It also exports a reference
376@code{$__} to @code{%__}.
377
378If an argument to the @code{xgettext} option @code{--keyword},
379resp. @code{-k} starts with a percent sign, the rest of the keyword is
380interpreted as the name of a hash.  If it starts with a dollar
381sign, the rest of the keyword is interpreted as a reference to a
382hash.
383
384Note that you can omit the quotation marks (single or double) around
385the hash key (almost) whenever Perl itself allows it:
386
387@example
388print $gettext@{Error@};
389@end example
390
391The exact rule is: You can omit the surrounding quotes, when the hash
392key is a valid C (!) identifier, i.e.@: when it starts with an
393underscore or an ASCII letter and is followed by an arbitrary number
394of underscores, ASCII letters or digits.  Other Unicode characters
395are @emph{not} allowed, regardless of the @code{use utf8} pragma.
396
397@node Quote-like Expressions
398@subsubsection What are Strings And Quote-like Expressions?
399@cindex Perl quote-like expressions
400
401Perl offers a plethora of different string constructs.  Those that can
402be used either as arguments to functions or inside braces for hash
403lookups are generally supported by @code{xgettext}.
404
405@itemize @bullet
406@item @strong{double-quoted strings}
407@*
408@example
409print gettext "Hello World!";
410@end example
411
412@item @strong{single-quoted strings}
413@*
414@example
415print gettext 'Hello World!';
416@end example
417
418@item @strong{the operator qq}
419@*
420@example
421print gettext qq |Hello World!|;
422print gettext qq <E-mail: <guido\@@imperia.net>>;
423@end example
424
425The operator @code{qq} is fully supported.  You can use arbitrary
426delimiters, including the four bracketing delimiters (round, angle,
427square, curly) that nest.
428
429@item @strong{the operator q}
430@*
431@example
432print gettext q |Hello World!|;
433print gettext q <E-mail: <guido@@imperia.net>>;
434@end example
435
436The operator @code{q} is fully supported.  You can use arbitrary
437delimiters, including the four bracketing delimiters (round, angle,
438square, curly) that nest.
439
440@item @strong{the operator qx}
441@*
442@example
443print gettext qx ;LANGUAGE=C /bin/date;
444print gettext qx [/usr/bin/ls | grep '^[A-Z]*'];
445@end example
446
447The operator @code{qx} is fully supported.  You can use arbitrary
448delimiters, including the four bracketing delimiters (round, angle,
449square, curly) that nest.
450
451The example is actually a useless use of @code{gettext}.  It will
452invoke the @code{gettext} function on the output of the command
453specified with the @code{qx} operator.  The feature was included
454in order to make the interface consistent (the parser will extract
455all strings and quote-like expressions).
456
457@item @strong{here documents}
458@*
459@example
460@group
461print gettext <<'EOF';
462program not found in $PATH
463EOF
464
465print ngettext <<EOF, <<"EOF";
466one file deleted
467EOF
468several files deleted
469EOF
470@end group
471@end example
472
473Here-documents are recognized.  If the delimiter is enclosed in single
474quotes, the string is not interpolated.  If it is enclosed in double
475quotes or has no quotes at all, the string is interpolated.
476
477Delimiters that start with a digit are not supported!
478
479@end itemize
480
481@node Interpolation I
482@subsubsection Invalid Uses Of String Interpolation
483@cindex Perl invalid string interpolation
484
485Perl is capable of interpolating variables into strings.  This offers
486some nice features in localized programs but can also lead to
487problems.
488
489A common error is a construct like the following:
490
491@example
492print gettext "This is the program $0!\n";
493@end example
494
495Perl will interpolate at runtime the value of the variable @code{$0}
496into the argument of the @code{gettext()} function.  Hence, this
497argument is not a string constant but a variable argument (@code{$0}
498is a global variable that holds the name of the Perl script being
499executed).  The interpolation is performed by Perl before the string
500argument is passed to @code{gettext()} and will therefore depend on
501the name of the script which can only be determined at runtime.
502Consequently, it is almost impossible that a translation can be looked
503up at runtime (except if, by accident, the interpolated string is found
504in the message catalog).
505
506The @code{xgettext} program will therefore terminate parsing with a fatal
507error if it encounters a variable inside of an extracted string.  In
508general, this will happen for all kinds of string interpolations that
509cannot be safely performed at compile time.  If you absolutely know
510what you are doing, you can always circumvent this behavior:
511
512@example
513my $know_what_i_am_doing = "This is program $0!\n";
514print gettext $know_what_i_am_doing;
515@end example
516
517Since the parser only recognizes strings and quote-like expressions,
518but not variables or other terms, the above construct will be
519accepted.  You will have to find another way, however, to let your
520original string make it into your message catalog.
521
522If invoked with the option @code{--extract-all}, resp. @code{-a},
523variable interpolation will be accepted.  Rationale: You will
524generally use this option in order to prepare your sources for
525internationalization.
526
527Please see the manual page @samp{man perlop} for details of strings and
528quote-like expressions that are subject to interpolation and those
529that are not.  Safe interpolations (that will not lead to a fatal
530error) are:
531
532@itemize @bullet
533
534@item the escape sequences @code{\t} (tab, HT, TAB), @code{\n}
535(newline, NL), @code{\r} (return, CR), @code{\f} (form feed, FF),
536@code{\b} (backspace, BS), @code{\a} (alarm, bell, BEL), and @code{\e}
537(escape, ESC).
538
539@item octal chars, like @code{\033}
540@*
541Note that octal escapes in the range of 400-777 are translated into a
542UTF-8 representation, regardless of the presence of the @code{use utf8} pragma.
543
544@item hex chars, like @code{\x1b}
545
546@item wide hex chars, like @code{\x@{263a@}}
547@*
548Note that this escape is translated into a UTF-8 representation,
549regardless of the presence of the @code{use utf8} pragma.
550
551@item control chars, like @code{\c[} (CTRL-[)
552
553@item named Unicode chars, like @code{\N@{LATIN CAPITAL LETTER C WITH CEDILLA@}}
554@*
555Note that this escape is translated into a UTF-8 representation,
556regardless of the presence of the @code{use utf8} pragma.
557@end itemize
558
559The following escapes are considered partially safe:
560
561@itemize @bullet
562
563@item @code{\l} lowercase next char
564
565@item @code{\u} uppercase next char
566
567@item @code{\L} lowercase till \E
568
569@item @code{\U} uppercase till \E
570
571@item @code{\E} end case modification
572
573@item @code{\Q} quote non-word characters till \E
574
575@end itemize
576
577These escapes are only considered safe if the string consists of
578ASCII characters only.  Translation of characters outside the range
579defined by ASCII is locale-dependent and can actually only be performed
580at runtime; @code{xgettext} doesn't do these locale-dependent translations
581at extraction time.
582
583Except for the modifier @code{\Q}, these translations, albeit valid,
584are generally useless and only obfuscate your sources.  If a
585translation can be safely performed at compile time you can just as
586well write what you mean.
587
588@node Interpolation II
589@subsubsection Valid Uses Of String Interpolation
590@cindex Perl valid string interpolation
591
592Perl is often used to generate sources for other programming languages
593or arbitrary file formats.  Web applications that output HTML code
594make a prominent example for such usage.
595
596You will often come across situations where you want to intersperse
597code written in the target (programming) language with translatable
598messages, like in the following HTML example:
599
600@example
601print gettext <<EOF;
602<h1>My Homepage</h1>
603<script language="JavaScript"><!--
604for (i = 0; i < 100; ++i) @{
605    alert ("Thank you so much for visiting my homepage!");
606@}
607//--></script>
608EOF
609@end example
610
611The parser will extract the entire here document, and it will appear
612entirely in the resulting PO file, including the JavaScript snippet
613embedded in the HTML code.  If you exaggerate with constructs like
614the above, you will run the risk that the translators of your package
615will look out for a less challenging project.  You should consider an
616alternative expression here:
617
618@example
619print <<EOF;
620<h1>$gettext@{"My Homepage"@}</h1>
621<script language="JavaScript"><!--
622for (i = 0; i < 100; ++i) @{
623    alert ("$gettext@{'Thank you so much for visiting my homepage!'@}");
624@}
625//--></script>
626EOF
627@end example
628
629Only the translatable portions of the code will be extracted here, and
630the resulting PO file will begrudgingly improve in terms of readability.
631
632You can interpolate hash lookups in all strings or quote-like
633expressions that are subject to interpolation (see the manual page
634@samp{man perlop} for details).  Double interpolation is invalid, however:
635
636@example
637# TRANSLATORS: Replace "the earth" with the name of your planet.
638print gettext qq@{Welcome to $gettext->@{"the earth"@}@};
639@end example
640
641The @code{qq}-quoted string is recognized as an argument to @code{xgettext} in
642the first place, and checked for invalid variable interpolation.  The
643dollar sign of hash-dereferencing will therefore terminate the parser
644with an ``invalid interpolation'' error.
645
646It is valid to interpolate hash lookups in regular expressions:
647
648@example
649if ($var =~ /$gettext@{"the earth"@}/) @{
650   print gettext "Match!\n";
651@}
652s/$gettext@{"U. S. A."@}/$gettext@{"U. S. A."@} $gettext@{"(dial +0)"@}/g;
653@end example
654
655@node Parentheses
656@subsubsection When To Use Parentheses
657@cindex Perl parentheses
658
659In Perl, parentheses around function arguments are mostly optional.
660@code{xgettext} will always assume that all
661recognized keywords (except for hashes and hash references) are names
662of properly prototyped functions, and will (hopefully) only require
663parentheses where Perl itself requires them.  All constructs in the
664following example are therefore ok to use:
665
666@example
667@group
668print gettext ("Hello World!\n");
669print gettext "Hello World!\n";
670print dgettext ($package => "Hello World!\n");
671print dgettext $package, "Hello World!\n";
672
673# The "fat comma" => turns the left-hand side argument into a
674# single-quoted string!
675print dgettext smellovision => "Hello World!\n";
676
677# The following assignment only works with prototyped functions.
678# Otherwise, the functions will act as "greedy" list operators and
679# eat up all following arguments.
680my $anonymous_hash = @{
681   planet => gettext "earth",
682   cakes => ngettext "one cake", "several cakes", $n,
683   still => $works,
684@};
685# The same without fat comma:
686my $other_hash = @{
687   'planet', gettext "earth",
688   'cakes', ngettext "one cake", "several cakes", $n,
689   'still', $works,
690@};
691
692# Parentheses are only significant for the first argument.
693print dngettext 'package', ("one cake", "several cakes", $n), $discarded;
694@end group
695@end example
696
697@node Long Lines
698@subsubsection How To Grok with Long Lines
699@cindex Perl long lines
700
701The necessity of long messages can often lead to a cumbersome or
702unreadable coding style.  Perl has several options that may prevent
703you from writing unreadable code, and
704@code{xgettext} does its best to do likewise.  This is where the dot
705operator (the string concatenation operator) may come in handy:
706
707@example
708@group
709print gettext ("This is a very long"
710               . " message that is still"
711               . " readable, because"
712               . " it is split into"
713               . " multiple lines.\n");
714@end group
715@end example
716
717Perl is smart enough to concatenate these constant string fragments
718into one long string at compile time, and so is
719@code{xgettext}.  You will only find one long message in the resulting
720POT file.
721
722Note that the future Perl 6 will probably use the underscore
723(@samp{_}) as the string concatenation operator, and the dot
724(@samp{.}) for dereferencing.  This new syntax is not yet supported by
725@code{xgettext}.
726
727If embedded newline characters are not an issue, or even desired, you
728may also insert newline characters inside quoted strings wherever you
729feel like it:
730
731@example
732@group
733print gettext ("<em>In HTML output
734embedded newlines are generally no
735problem, since adjacent whitespace
736is always rendered into a single
737space character.</em>");
738@end group
739@end example
740
741You may also consider to use here documents:
742
743@example
744@group
745print gettext <<EOF;
746<em>In HTML output
747embedded newlines are generally no
748problem, since adjacent whitespace
749is always rendered into a single
750space character.</em>
751EOF
752@end group
753@end example
754
755Please do not forget that the line breaks are real, i.e.@: they
756translate into newline characters that will consequently show up in
757the resulting POT file.
758
759@node Perl Pitfalls
760@subsubsection Bugs, Pitfalls, And Things That Do Not Work
761@cindex Perl pitfalls
762
763The foregoing sections should have proven that
764@code{xgettext} is quite smart in extracting translatable strings from
765Perl sources.  Yet, some more or less exotic constructs that could be
766expected to work, actually do not work.
767
768One of the more relevant limitations can be found in the
769implementation of variable interpolation inside quoted strings.  Only
770simple hash lookups can be used there:
771
772@example
773print <<EOF;
774$gettext@{"The dot operator"
775          . " does not work"
776          . "here!"@}
777Likewise, you cannot @@@{[ gettext ("interpolate function calls") ]@}
778inside quoted strings or quote-like expressions.
779EOF
780@end example
781
782This is valid Perl code and will actually trigger invocations of the
783@code{gettext} function at runtime.  Yet, the Perl parser in
784@code{xgettext} will fail to recognize the strings.  A less obvious
785example can be found in the interpolation of regular expressions:
786
787@example
788s/<!--START_OF_WEEK-->/gettext ("Sunday")/e;
789@end example
790
791The modifier @code{e} will cause the substitution to be interpreted as
792an evaluable statement.  Consequently, at runtime the function
793@code{gettext()} is called, but again, the parser fails to extract the
794string ``Sunday''.  Use a temporary variable as a simple workaround if
795you really happen to need this feature:
796
797@example
798my $sunday = gettext "Sunday";
799s/<!--START_OF_WEEK-->/$sunday/;
800@end example
801
802Hash slices would also be handy but are not recognized:
803
804@example
805my @@weekdays = @@gettext@{'Sunday', 'Monday', 'Tuesday', 'Wednesday',
806                        'Thursday', 'Friday', 'Saturday'@};
807# Or even:
808@@weekdays = @@gettext@{qw (Sunday Monday Tuesday Wednesday Thursday
809                         Friday Saturday) @};
810@end example
811
812This is perfectly valid usage of the tied hash @code{%gettext} but the
813strings are not recognized and therefore will not be extracted.
814
815Another caveat of the current version is its rudimentary support for
816non-ASCII characters in identifiers.  You may encounter serious
817problems if you use identifiers with characters outside the range of
818'A'-'Z', 'a'-'z', '0'-'9' and the underscore '_'.
819
820Maybe some of these missing features will be implemented in future
821versions, but since you can always make do without them at minimal effort,
822these todos have very low priority.
823
824A nasty problem are brace format strings that already contain braces
825as part of the normal text, for example the usage strings typically
826encountered in programs:
827
828@example
829die "usage: $0 @{OPTIONS@} FILENAME...\n";
830@end example
831
832If you want to internationalize this code with Perl brace format strings,
833you will run into a problem:
834
835@example
836die __x ("usage: @{program@} @{OPTIONS@} FILENAME...\n", program => $0);
837@end example
838
839Whereas @samp{@{program@}} is a placeholder, @samp{@{OPTIONS@}}
840is not and should probably be translated. Yet, there is no way to teach
841the Perl parser in @code{xgettext} to recognize the first one, and leave
842the other one alone.
843
844There are two possible work-arounds for this problem.  If you are
845sure that your program will run under Perl 5.8.0 or newer (these
846Perl versions handle positional parameters in @code{printf()}) or
847if you are sure that the translator will not have to reorder the arguments
848in her translation -- for example if you have only one brace placeholder
849in your string, or if it describes a syntax, like in this one --, you can
850mark the string as @code{no-perl-brace-format} and use @code{printf()}:
851
852@example
853# xgettext: no-perl-brace-format
854die sprintf ("usage: %s @{OPTIONS@} FILENAME...\n", $0);
855@end example
856
857If you want to use the more portable Perl brace format, you will have to do
858put placeholders in place of the literal braces:
859
860@example
861die __x ("usage: @{program@} @{[@}OPTIONS@{]@} FILENAME...\n",
862         program => $0, '[' => '@{', ']' => '@}');
863@end example
864
865Perl brace format strings know no escaping mechanism.  No matter how this
866escaping mechanism looked like, it would either give the programmer a
867hard time, make translating Perl brace format strings heavy-going, or
868result in a performance penalty at runtime, when the format directives
869get executed.  Most of the time you will happily get along with
870@code{printf()} for this special case.
871