• komplex expressions in statement modifiers

    From Rainer Weikusat@21:1/5 to All on Fri Apr 2 19:37:29 2021
    The perl grep operator can be used to determine if some condition holds
    for some elements of a list. In scalar context, it returns the number of
    times the condition was true. This means it's semantically a bad fit for finding a single element in a list because it always processes the
    complete list. But it can be used as test expression for a statement
    modifier ... so far, so bad.

    Upon spending a time thinking through this, it dawned to be that eval {
    } is syntactically an expression, hence

    ------
    @a = qw(a b c d e);

    while ($l = <STDIN>) {
    chomp($l);
    print("found $l\n") if eval { $_ eq $l and return 1 for @a };
    }
    -------

    is valid Perl (which works, obviously).

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From George Bouras@21:1/5 to All on Fri Apr 2 22:22:57 2021
    Στις 2/4/2021 9:37 μ.μ., ο/η Rainer Weikusat έγραψε:
    The perl grep operator can be used to determine if some condition holds
    for some elements of a list. In scalar context, it returns the number of times the condition was true. This means it's semantically a bad fit for finding a single element in a list because it always processes the
    complete list. But it can be used as test expression for a statement
    modifier ... so far, so bad.

    Upon spending a time thinking through this, it dawned to be that eval {
    } is syntactically an expression, hence

    ------
    @a = qw(a b c d e);

    while ($l = <STDIN>) {
    chomp($l);
    print("found $l\n") if eval { $_ eq $l and return 1 for @a };
    }
    -------

    is valid Perl (which works, obviously).



    use the clever equal ~~

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rainer Weikusat@21:1/5 to George Bouras on Fri Apr 2 20:41:29 2021
    George Bouras <foo@example.com> writes:
    Στις 2/4/2021 9:37 μ.μ., ο/η Rainer Weikusat έγραψε:
    The perl grep operator can be used to determine if some condition holds
    for some elements of a list. In scalar context, it returns the number of
    times the condition was true. This means it's semantically a bad fit for
    finding a single element in a list because it always processes the
    complete list. But it can be used as test expression for a statement
    modifier ... so far, so bad.

    Upon spending a time thinking through this, it dawned to be that eval {
    } is syntactically an expression, hence

    ------
    @a = qw(a b c d e);

    while ($l = <STDIN>) {
    chomp($l);
    print("found $l\n") if eval { $_ eq $l and return 1 for @a };
    }
    -------

    is valid Perl (which works, obviously).



    use the clever equal ~~

    I'm not interested in the smart alec operator :->, not the least because
    it (presumably forever) prevents Perl from getting a non-experimental
    multiway conditional construct (something I'm using frequently)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Otto J. Makela@21:1/5 to Rainer Weikusat on Tue Apr 6 07:50:35 2021
    Rainer Weikusat <rweikusat@talktalk.net> wrote:

    [...]
    is valid Perl (which works, obviously).

    Thanks for this bit of strangeness, it caused me to look up the Perl
    "secret operators" page:

    https://metacpan.org/pod/distribution/perlsecret/lib/perlsecret.pod
    --
    /* * * Otto J. Makela <om@iki.fi> * * * * * * * * * */
    /* Phone: +358 40 765 5772, ICBM: N 60 10' E 24 55' */
    /* Mail: Mechelininkatu 26 B 27, FI-00100 Helsinki */
    /* * * Computers Rule 01001111 01001011 * * * * * * */

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rainer Weikusat@21:1/5 to Otto J. Makela on Tue Apr 6 15:37:14 2021
    om@iki.fi (Otto J. Makela) writes:
    Rainer Weikusat <rweikusat@talktalk.net> wrote:

    [...]
    is valid Perl (which works, obviously).

    Thanks for this bit of strangeness, it caused me to look up the Perl
    "secret operators" page:

    https://metacpan.org/pod/distribution/perlsecret/lib/perlsecret.pod

    There's nothing particularly strange in this construct: Any operator
    can (obviously) be used as test expression for a
    conditional. And any operator with a &-prototype would do here:

    ---------
    sub judge (&)
    {
    &{$_[0]}
    }

    @a = qw(a b c d e);

    while ($l = <>) {
    chomp($l);
    print("found $l\n") if judge { $_ eq $l and return 1 for @a };
    }
    ---------

    eval is just one behaving in this way which is already available.

    NB: The page is somewhat interesting. But I don't generally care for
    "brevity" (for its own sake) or "obscurity" (at all).

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)