docdurdee has asked for the wisdom of the Perl Monks concerning the following question:

Dearest Monks, Is it difficult to enforce void context on a subroutine call in Reply? I found the option I needed here https://metacpan.org/pod/Syntax::Feature::Void, but the docs states that "Not that anyone needs that." So maybe I don't need it. Is there cleaner way to enforce a void context call using the reply REPL? Thanks! D

Replies are listed 'Best First'.
Re: How to enforce void context in Reply?
by kcott (Archbishop) on May 19, 2015 at 02:08 UTC

    G'day docdurdee,

    Using return without arguments would be how I would do this. The Syntax::Feature::Void documentation states this to be the functional equivalent of what it's doing. Is there a reason you can't do the same?

    Please supply code examples of what you're doing, an explanation of how it's not achieving what you want at present, and a description of the actual result you're aimimg for. This clarification should help in providing a better answer.

    -- Ken

      Further to kcott's reply above, here's an experiment that may make the operation of function call return context more clear:

      c:\@Work\Perl\monks>perl -wMstrict -le "sub S { print ! defined wantarray ? 'void' : wantarray ? 'list' : 'sc +alar'; } ;; sub T { print 'non-return: '; S; print 'return: '; S; } ;; sub U { print 'SCALAR ----'; my $scalar = T; print ''; print 'LIST ----'; my @ra = T; print ''; print 'VOID ----'; T; print ''; } ;; U; " SCALAR ---- non-return: void return: scalar LIST ---- non-return: void return: list VOID ---- non-return: void return: void
      See also wantarray and Context in Perl.


      Give a man a fish:  <%-(-(-(-<

Re: How to enforce void context in Reply?
by Anonymous Monk on May 19, 2015 at 07:39 UTC

    I find putting a dummy statement after the call usually works fine. E.g. function_call(); 1;

    sub function_call { print defined wantarray ? (wantarray?"list\n":"scalar\n") : "void\n" } sub calls_it_1 { print "calls_it_1: "; function_call(); # is this void or not? } sub calls_it_2 { print "calls_it_2: "; function_call(); 1; # this is void } calls_it_1(); my $x = calls_it_1(); my @x = calls_it_1(); calls_it_2(); my $y = calls_it_2(); my @y = calls_it_2(); __END__ calls_it_1: void calls_it_1: scalar calls_it_1: list calls_it_2: void calls_it_2: void calls_it_2: void
      This works in the Reply REPL. Thanks!!!!
Re: How to enforce void context in Reply?
by marinersk (Priest) on May 19, 2015 at 10:37 UTC

    I'm fascinated by this question.

    What would be a reason to desire to enforce a void context for a Perl subroutine return value?

      As noted in wantarray:

      return unless defined wantarray; # don't bother doing more my @a = complex_calculation(); return wantarray ? @a : "@a";

      Perhaps the OP is using a function that uses that strategy.

        That is detection not enforcement ... language barrier?