Perl Monk, Perl Meditation PerlMonks

### What is truth? (Curiosity corner)

by robin (Chaplain)
 on Dec 20, 2001 at 22:28 UTC Need Help??

```    sub explain {
my \$val = shift;
print "Numeric value = ", 0+\$val, "\n";
print "String value = \"\$val\"\n";
print "Truth value = ", \$val ? "true\n" : "false\n";
}
```
```    # explain("23skidoo");
Numeric value = 23
String value = "23skidoo"
...
Numeric value = 0
String value = ""
Truth value = false
```
```[robin@robin robin]\$ perl -wle 'print 0+"23skidoo"'
Argument "23skidoo" isn't numeric in addition (+) at -e line 1.
23
```
```[robin@robin bits &amp; pieces]\$ perl -wle 'print 23+"0 but true"'
23
```
```    # explain([1,2,3]);
Numeric value = 25616
String value = "ARRAY(0x6410)"
Truth value = true
```
```    my \$foo = *bar;
our \$bar = "hello\n";
our @bar = ("Hello again!\n");
print \$\$foo, @\$foo;
explain(\$foo);
```
```    hello
Hello again!
Numeric value = 0
String value = "*main::bar"
Truth value = true
```
```    # explain(bless {}, "SomePackage");
Numeric value = 25616
String value = "SomePackage=HASH(0x6410)"
Truth value = true
```
```    explain(bless {}, "SomePackage");
package SomePackage;
use overload '""'   => sub {'string value'};
use overload '0+'   => sub {23};
use overload 'bool' => sub {""};
```
```    Numeric value = 23
String value = "string value"
Truth value = false
```
```    Numeric value = 0
String value = "string value"
Truth value = true
```
```    explain(bless {}, "SomePackage");
package SomePackage;
use overload '""'   => sub {''};
use overload '0+'   => sub {23};
```
```    Numeric value = 23
String value = ""
Truth value = true
```
```    explain(bless {}, "SomePackage");
package SomePackage;
use overload '""'   => sub { '' };
use overload '0+'   => sub {'00'};
```

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://133554]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2022-05-22 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Do you prefer to work remotely?

Results (80 votes). Check out past polls.

Notices?