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

Why use dd to convert a string to an integer?

Thread Next
From:
ToddAndMargo
Date:
August 7, 2018 07:16
Subject:
Why use dd to convert a string to an integer?
Message ID:
29d145e9-df1e-cbc2-3bd6-cfe77ab9baf1@zoho.com
Hi All,

I am confused.

This line will convert a string into an integer, but
will print out and extra line with "(Int)" on it:

    $ p6 'my Str $x = "5"; my Int $y = dd +$x; say $y'
    5
    (Int)


This will convert as well, but no extra line:

    $ p6 'my Int $x; my Str $y = "5"; $x = "$y" + 0; say $x'
    5


Also, I can quote $x, and it still works:

    $ p6 'my Int $x; my Str $y = "5"; $x = "$y" + 0; say "$x"'
    5


But this tells me I have an uninitialized value, when all
I did was add quotes around $y, as in the above line.

    $ p6 'my Str $x = "5"; my Int $y = dd +$x; say "$y";'
    5
    Use of uninitialized value $y of type Int in string context.
    Methods .^name, .perl, .gist, or .say can be used to stringify
    it to something meaningful.


Why would I want to use "dd"?

Many thanks,
-T

Thread Next


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