in reply to Re: How does ~~ force string interpolation of code into scalar context?
in thread How does ~~ force string interpolation of code into scalar context?

Hmm, stacking unary operators. I hadn't considered that as even being a possibility except for things like $$ and \\ for multiple levels of referencing/dereferencing.

Thanks for pointing out a new idiom for me (which may not be all that necessary but is definately interesting to understand).

In the spirit of exploration I found that the following will also at least compile and force scalar context. Some of them will mangle return values though. ;)

sub context { print wantarray ? "List\n" : "Scalar\n"; } my $string; $string = "Test ${\~~context()} string"; $string = "Test ${\!!context()} string"; $string = "Test ${\+-+context()} string"; $string = "Test ${\-+-context()} string"; $string = "Test ${\&\&context()} string";

Thanks again...


Dave


"If I had my life to live over again, I'd be a plumber." -- Albert Einstein
  • Comment on Re: Re: How does ~~ force string interpolation of code into scalar context?
  • Download Code

Replies are listed 'Best First'.
Re: Re: Re: How does ~~ force string interpolation of code into scalar context?
by sauoq (Abbot) on Oct 25, 2003 at 22:05 UTC
    I found that the following will also at least compile and force scalar context. Some of them will mangle return values though. ;)

    Actually, of the four additional combinations you tried, only -+- would be at all useful. It avoids mangling integers and floats. The three others mangle everything.¹ Also, with 5.6.1 and 5.8.0 at least, using this will cause warnings about ambiguity unless warnings are explicitly disabled.

    Still, it may have some golf merit in being a short way to impose scalar context on functions that return floating point numbers. For strings or ints, though, I think ~~ is still preferable. For golf or obfuscation that is. Of course, scalar() is far better for "real" code.

    By the way, -+- might also be written as - -, the plus or space only being necessary to differentiate it from a decrement.

    1. In fact, &\& will, I think, always fail under strict checking.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Re: Re: How does ~~ force string interpolation of code into scalar context?
by TomDLux (Vicar) on Oct 27, 2003 at 00:54 UTC

    I think these CAN be useful ... for decorative purposes ...

    $arg1 = "arg1"; $arg2 = "arg2"; $arg3 = "arg3"; sub f1 { return "1"; } sub f2 { return "0"; } sub f3 { return "3"; } print "${\-+- f1( $arg1 )}"; print "${\+-+ f2( $arg2 )}"; print "${\-+- f3( $arg3 )}"; print "\n"

    isn't that far better than the undecorated print statements? ( Same f1(), f2(), f3(), $arg1, $arg2, $arg3. )

    print f1( $arg1 ); print f2( $arg2 ); print f3( $arg3 );

    And FAR superior to the boring and mundane:

    print "103\n";

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

      Far better indeed, as your "undecorated print statements" don't interpolate the function f1, f2 and f3; they just print the literal text f1( ... ).

      The "decorative" version executes f1() with $arg1 as its parameter.

      Of course neither approach is better than print "@{[f1( $arg1 )]}";, and that method is, itself, usually worse than print f1( $arg1 ); (without unnecessary interpolation).


      Dave


      "If I had my life to live over again, I'd be a plumber." -- Albert Einstein