Perl5 -> Perl6: sprintf
I love
sprintf
. After the wow! string concatenation hype, I found myself a lot more comfortable in adopting sprintf
and alike for string formatting. It looks a lot nicer and more readable to me.
Of course, it has drawbacks, as a lot of variadic methods: you can miss a parameter or fail its type. However, once set up, it is a lot
stable than string concatenation (and can be translated in an easier way).
The above means that a lot of messaging in my Perl programs use sprintf
to, and often I use the following piece of code in Perl 5:
say sprintf "Inizio elaborazione modo %s (%s) su file di output [%s] %s limite",
$current_mode,
%available_modes{ $current_mode },
$output,
( $limit.defined ? sprintf '%d righe', $limit : 'SENZA' ) );
sprintf
is now a method of Str
:
say "Inizio elaborazione modo %s (%s) su file di output [%s] %s limite"\
.sprintf( $current_mode,
%available_modes{ $current_mode },
$output,
( $limit.defined ?? '%d righe'.sprintf( $limit ) !! 'SENZA' ) );
unquote
operator to keep things on different lines. It is not mandatory in this case, but it can be a good habit
for when Perl 6 requires it. Also note the different ternary operator based on ?? !!
.