Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Fun with implicit type conversions

by haj (Chaplain)
on Aug 29, 2019 at 15:34 UTC ( #11105250=perlmeditation: print w/replies, xml ) Need Help??

Recently, jdporter closed one of his comments with the following snippet: $orry - not $orry

This is a valid Perl expression. So, I wondered, what is its value? My first thought was "easy: that's just $orry converted to a number", but as ever so often, my first thought is wrong. If $orry = 0, then the value is -1. If $orry is a string, then the value is 0. Unless the string is empty, in which case the value is -1. And unless the string starts with a number, where the result is that number, even if the number is 0. And then, there are references, typeglobs, tied scalars, and objects, which may even overload stuff.

Here's a collection of expressions for $orry, and results of $orry - not $orry.

$orry : $orry - not $orry 42 : 42 -1 : -1 0 : -1 undef : -1 "" : -1 0e0 : -1 "0e0" : 0 "a123" : 0 "-.2e2e" : -20 "nancy boy" : NaN "infidelity" : Inf *STDOUT : 0 [] : 94269931603432 File::Temp->new : 94269935380840 URI->new(q(https://perl.org/)) : 0

The implicit conversions of scalars into numeric / boolean / string context are amazing, and sometimes scary. Fortunately, use warnings; detects most of the surprising ones.

Update:: syphilis contributed the strings "nancy boy" and "infidelity" which are indeed amusing additions. I inserted them after the strings starting with numbers. In the case of 'nancy boy' my text "string starts with a number" looks silly because NaN claims to be not a number.

Replies are listed 'Best First'.
Re: Fun with implicit type conversions
by syphilis (Bishop) on Aug 30, 2019 at 04:28 UTC
    Here's a couple of mildly amusing examples that might require a little bit of thought before one realizes that they are in fact obvious:
    "nancy boy" : NaN "infidelity" : Inf
    I think that, on some systems, the right hand side will be lowercase throughout (ie "nan" and "inf", respectively) and other case variations might also exist.

    Cheers,
    Rob
Re: Fun with implicit type conversions
by syphilis (Bishop) on Aug 30, 2019 at 13:07 UTC
    In the case of 'nancy boy' my text "string starts with a number" looks silly because NaN claims to be not a number

    I don't think you need worry about that.
    My take on the reason that NaN ** 0 is defined to be 1 is that "every number raised to the power of 0 is 1" ... from which I deduce that the IEEE standards committee has decided that NaN *is* a number.

    Cheers,
    Rob
Re: Fun with implicit type conversions
by kikuchiyo (Pilgrim) on Aug 30, 2019 at 14:17 UTC

    $orry, I couldn't resist.

    #!/usr/bin/perl package Sorry; use overload '-' => sub { "@_\n" }, '!' => sub { __PACKAGE__->new("Perl Hacker"); }, '""' => sub { $_[0]->[0] }; sub new { bless [$_[1] // "Just Another"] } package main; my $orry = Sorry->new; print $orry - not $orry;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://11105250]
Approved by Fletch
Front-paged by Discipulus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2019-12-13 21:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?