in reply to "" but true

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

Replies are listed 'Best First'.
Re^2: "" but true
by LanX (Saint) on May 14, 2021 at 02:42 UTC
    > 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.

    Do you have a reference for this, or is it a guess?

    I'm surprised since

    • I read boolean context be a sub case of numeric context
    • Perl tends to avoid such overhead like checking two slots

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      Do you have a reference for this, or is it a guess?

      I thought I had a reference for this ... but no, I completely misread this statement in the docs, relating to isdual():
      If $var is a scalar that has both numeric and string values, the resul +t is true
      Not only does it not pertain to dualvar(), but it doesn't say what I thought it said, anyway !!
      Additional experimentation suggests that, in fact, the dualvar will be True if the second arg is True. Is that the correct summation ?
      Thanks for catching.

      Not sure if that mistake necessarily impacts upon the little overload demo I provided.
      In any case, of course, that overload_true() subroutine can be tweaked to make it do whatever you want.

      I'll edit that post of mine.

      Cheers,
      Rob
Re^2: "" but true
by Chuma (Scribe) on May 14, 2021 at 13:03 UTC

    I'm learning so many dirty tricks!

    It's a shame the dualvar thing doesn't work in this case, but I guess if I ever have a need for "5 but false", I know what to do.