Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Empty string but True

by Yary (Pilgrim)
on Jan 22, 2015 at 16:30 UTC ( [id://1114157]=perlmeditation: print w/replies, xml ) Need Help??

I was looking at an old StackOverflow question about generating an explicit null XML namespace and had the thought, "maybe an object that stringifies to the empty string, but is true in every other context, will trick LibXML into doing what the seeker of wisdom required."

It did not, nor did an earlier attempt using than Scalar::Util's dualvar. Still I liked my little Empty but True module enough to post it here. (Seems too useless/dangerous/not worth the bother to be on CPAN.)

package MyEmptyTrueVar; our $singleton; use overload fallback => 'TRUE', '""' => sub { "" }, # Return empty string on stringification bool => sub { 1 }, # Return true in boolean context '0+' => sub { 1 }, # Return true in numeric context cmp => sub { !ref $_[1] }; # unequal to empty string, or any other +string bless $singleton=\$singleton;
usage:
say "Single str='$MyEmptyTrueVar::singleton', Num=", (0+$MyEmptyTrueVa +r::singleton) if $MyEmptyTrueVar::singleton && $MyEmptyTrueVar::singleton ne '' && '' ne $MyEmptyTrueVar::singleton;
shows that Single str='', Num=1.

Replies are listed 'Best First'.
Re: Empty string but True
by RonW (Parson) on Jan 22, 2015 at 18:57 UTC

    I'd be inclined to put it on CPAN as Acme::EmptyTrueVar just for humor value.

    As for usefulness, it could be if it had actual instances and it returned it's actual value when not empty.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-24 22:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found