develooper Front page | perl.perl6.users | Postings from June 2018

Re: EVAL?

Thread Previous
From:
ToddAndMargo
Date:
June 14, 2018 20:21
Subject:
Re: EVAL?
Message ID:
325016c3-9aa6-6b08-a791-55a94f2d0cb2@zoho.com
On 06/14/2018 01:13 PM, Trey Harris wrote:
> Just a small stylistic thing to mention:
> 
> On Thu, Jun 14, 2018 at 1:59 AM Todd Chester <ToddAndMargo@zoho.com 
> <mailto:ToddAndMargo@zoho.com>> wrote:
> 
>     p6
>             if not $dir.IO.d.bool {}
>             for  slice  "\n", $WebStr   ->  $Line { }
> 
>        and on and on and so forth.  I know a lot of them by heart now.
> 
> By |.bool|, I assume you meant |.Bool|, but in any case, it isn’t 
> necessary since the context of a |not| forces it to be boolean.

Indeed!

Exists (directory):
    DIRPATH.IO.d.Bool;
        if not $WorkingDir.IO.d.Bool { mkdir $WorkingDir, 0o766; }


> 
> If you want the opposite—a boolean without the sense-negation, you can 
> use |so|, but in the context of an |if|, it should rarely, if ever, be 
> necessary.
> 
> In the same way that |so| is used to explicitly truthify, unary |+| is 
> used to explicitly numify.
> 
> E.g., once you know that an |IntStr| 
> <https://docs.perl6.org/type/IntStr> dual value gives a result 
> reminiscent of Perl 5’s string numbers (except that without going to 
> crazy lengths, you can easily supply exactly the number and string you 
> want):
> 
> |my $x = IntStr.new(42, "The Answer"); my $y = IntStr.new(0, "Nothing"); 
> for ($x, $y) -> $z { printf "\$z: '%s', so \$z: '%s', +\$z: '%s'\n", $z, 
> so $z, +$z; say " Double it: ", $z * 2; say " or 'double' it: ", $z x 2; } |
> 
> gives results:
> 
> |$z: 'The Answer', so $z: 'True', +$z: '42' Double it: 84 or 'double' it: 
> The AnswerThe Answer $z: 'Nothing', so $z: 'False', +$z: '0' Double it: 
> 0 or 'double' it: NothingNothing |
> 
> In case you’re wondering, you’ll usually have no reason to explicitly 
> construct a dual value like that; you generally get a NumStr or its 
> counterparts like IntStr when reading input or expecting command-line 
> arguments.
> 
> (Also note that the stringification equivalent to |so| and unary |+| is 
> |~|, but it wasn’t necessary above since the |%s| template to |sprintf| 
> forces stringification. Also note that using s/printf at all is not 
> encouraged—but it was useful above, just to make things explicit.)
> 
> ​


Thank you!  Wonderful exposition! I am going to write it down
for later use.

I use "if not" a lot because it is human readable for
me and assists in maintaining the code.

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About