On Mon, May 18, 2020 at 03:51:30PM -0700, ToddAndMargo via perl6-users wrote: > On 2020-05-18 15:44, Tom Browder wrote: > > On Mon, May 18, 2020 at 16:19 ToddAndMargo via perl6-users > > <[email protected] <mailto:[email protected]>> wrote: > > > > On 2020-05-18 13:28, Tom Browder wrote: > > > > ... > > > > > Try: > > > > > > 'say so "test".IO.d' > > > > > > Todd, you didn't try what I suggested. Once again, look a the line above^^ > > > > There is no "if" there. > > > > -Tom > > > > It was the "if" I was interested in. "if" has to change > a True or "useless text message" into a True or False.
No, that's not what "if" does. "If" in Raku works pretty much
the same way as "if" in Perl: it takes an expression as
an argument, checks whether the expression is true or false
(that's the part where it takes any value and does a .Bool on
it - by itself - and then looks at the result), and then, if
it was true, the "if" statement runs another part of the code.
So:
if 'h:/'.IO.d {
say 'It is a directory!';
}
...will take the string 'h:/', the .IO method will convert it to
a path, the .d method will check whether the path corresponds to
a valid directory at this moment, and then the "if" statement will
check the result of this whole thing and decide whether to run
the { say... } block. If the path is a directory, it will tell you
that it is a directory. If the expression is true, it runs the code.
The postfix "if" also works in pretty much the same way as in Perl:
it allows you to shorten this:
if 'h:/'.IO.d {
say 'It is a directory!';
}
...to this:
say 'It is a directory!' if 'h:/'.IO.d;
Now you will note that in my example I did *not* write
"say if 'h:/'.IO.d", because I did not expect the "if" to return
a value; I wrote "say 'foo' if 'h:/'.IO.d" since I expected the "if"
statement to make Raku check whether 'h:/'.IO.d returns something that
looks like truth and, if it does, to execute the "say 'foo'" part.
I'm sorry, I assumed that you were familiar with the postfix "if" form
(something if something-else). Maybe you did not recognize it as such,
sorry.
As an exercise for the reader: once the above sinks in, what exactly
will "say if 'h:/'.IO.d" do? :)
G'luck,
Peter
--
Peter Pentchev [email protected] [email protected] [email protected]
PGP key: http://people.FreeBSD.org/~roam/roam.key.asc
Key fingerprint 2EE7 A7A5 17FC 124C F115 C354 651E EFB0 2527 DF13
signature.asc
Description: PGP signature
