Another article of the series “Yet Another Perl 6 Operator”
In Perl 5, we expect values to DWIM (”do what I mean”) in various contexts. For example, if we use a string containg “42″ as a number we expect it automagically act as a number. Perl 6 keeps this tradition of DWIMmery and introduces several new explicit coercion operations.
? to get booleans + to get numbers ~ to get strings
These are all prefix unary operators.
? '' # Bool::False, because '' is a kind of false ? 42 # Bool::True + '1' # 1 ~ 3e4 # '30000'
These operators ('+', '~', '?') allow you to explicitly coerce values to the types you expect. This can lead to code that is more “self-documenting”, rather than relying on implicit type coercion.
Where these operators really shine is when you overload them as part of an object’s API. This can let you provide all sorts of API cleverness, which some people will love and others will hate.
Here is a simple overloading example with a Roman numeral class. This object provides the Roman numeral in a string context, but acts like a regular integer when you do arithmetic.
my $x = Roman.new(42);
print ~$x; # 'XLII'
print +$x; # 42
"in the year {$x} of the Hyborian era" #[
'in the year XLIV of the Hyborian era' ]
$x == 42; # Bool::True
print +($x + 1); # 43
print ~($x + 1); # 'XLIII'
As you might have guessed from the examples so far, in Perl 6 we now have a proper boolean type Bool, which provides two values: Bool::True and Bool::False. Of course, because of DWIMmery, you can use other types of values in a boolean context and get sane results.
So why booleans? Well, without a proper boolean type in Perl 5, we had to fall back on representing boolean values using integers or strings. These types allow many values besides just { 0, 1 } or { '', 'true' }, which can cause confusion, especially if you end up with other true values like "maybe" or 10 in the mix.
Some of the readers may have noticed that I left the Monday deadline slip by. But let’s resume from here. Next article will be due Friday (September 28, 2007).
My thanks for the continued help by Dave Rolsky and the crew of #perl6 freenode channel.
LINKS
- Synopsis S03, the official source
- The introduction of this series
- Official Perl 6 Documentation
- Perl 6 in your browser


How does print ~($x + 1); know to evaluate to 'XLIII' ? Why doesn't it evaluate to '43' or throw an error? What makes it take on the Roman type, because it was first in the expression?
How many digits are in the stringification of a non-integral number? For example, what is the value of ~(1.0 / 3) ?
@Joe
The statement
was corrected to
The issue here is that modelling roman numbers, besides overloading ~ and + (string and numeric coercion), the addition must be overloaded as well, resulting in a new Roman number. So
$x+$x,$x+1,$x+'I'could preserve their nature. So$x+1should result an equivalentRoman.new(43)in this case.A Perl 5 implementation of this is Acme::Roman.
@A different Joe
The answer will be quite similar to what we got today with Perl 5:
where the result comes from some equivalent of applying a format like "sprintf '%.15f'".
Does the auto-decrement operator have magic now? That was soo annoying to have such a cool feature on the one side and not the other...
The specific magic I'm speaking of is...
my $foo = 'D';
$foo++; # 'E'
$foo--; # -1 .... ARGggh!!
In fact, it does. You should be able to do something like:
And the details of how Perl 6 handles that is the subject of one of the articles of the next week.