So if $x has this value, if($x) is true, but $x.$y is just $y

I wondered if this could be done using a dualvar value:
use strict; use warnings; use Scalar::Util qw(dualvar); my $x = dualvar(1, ""); my $y = 'hello'; print $x . $y; # outputs hello
The only problem is that $x evaluates as False.
For $x to be True, its first (number) argument needs to be true in numeric context && its second (string) argument needs to be true in string context.
Unfortunately, "one out of two" is not good enough :-(
Update: Thanks LanX for alerting me to this mistake, and to the following mistake.

As LanX has already suggested As ikegami has already demonstrated, this can however be achieved using overloading.
Here's a another little demo:
# dualvar.pl package DualVar; use strict; use warnings; use overload 'bool' => \&overload_true, '.' => \&overload_concat; sub overload_true { # Return 1 if either $obj->[0] or $obj->[1] # are true. Else return 0. my $obj = shift; if($obj->[0] || $obj->[1]) { return 1 } return 0; } sub overload_concat { my ($obj, $other, $reversed) = (shift, shift, shift); if($reversed) { return $other . $obj->[1] } return $obj->[1] . $other; } # The above procedures would normally be put into # a module, which would be loaded as needed. # The code below relies on the above procedures. # Here we see that $x is True, and also that $y # remains unchanged when $x is concatenated onto it. my $x = [1, ""]; my $y = "hello"; my ($r1, $r2); bless $x, 'DualVar'; if($x) { $r1 = $x . $y; $r2 = $y . $x; } print "# ok 1\n" if $r1 eq $y; print "# ok 2\n" if $r2 eq $y; # Outputs: # ok 1 # ok 2
I think that satisfies the X part of your XY problem - at least for the "X" as quoted at the very beginning of this post of mine.
I'm not sure if it helps with the "Y" part ;-)

Cheers,
Rob

In reply to Re: "" but true by syphilis
in thread "" but true by Chuma

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.