develooper Front page | perl.perl5.porters | Postings from February 2019

Re: New defined-match operator

Thread Previous | Thread Next
From:
Rocco Caputo
Date:
February 22, 2019 18:04
Subject:
Re: New defined-match operator
Message ID:
2403E529-A0D4-4496-96AE-394E38026A29@pobox.com
Since it would set $_ until the end of the expression, using it again would override previous uses.

  $_   = "123";
  $foo = "abc";
  $bar = "def";
  defined2($foo) &&
  say            &&
  defined2($bar) &&
  say;
  say;

would print

  abc
  def
  123

in the same way that

  ($_ = "abc") &&
  say          &&
  ($_ = "def") &&
  say;

prints

  abc
  def

I agree that restoring $_ afterward is tricky, especially if people start mixing in non-localized assignments, like

  # reusing previous $foo and $bar
  $_ = "123";
  say;
  defined2($foo) &&
  say            &&
  ($_ = "456")   &&
  say            &&
  defined2($bar) &&
  say;
  say;

In my mind the last non-defined2() assignment to $_ would persist after the expression.  So the above might print:

  123
  abc
  456
  def
  456

In terms of existing language:

  $foo = "abc";
  $bar = "def";
  $_   = "123";
  say;
  my $saved = $_;
  defined($foo) && ($_ = $foo) &&
  say                          &&
  ($saved = $_ = "456")        &&
  say                          &&
  defined($bar) && ($_ = $bar) &&
  say;
  $_ = $saved;
  say;

"How presumptuous!"

The lack of precedent is something of a non-sequitur.  Progress occasionally requires new ones.  I agree new ones need a lot more consideration than usual.

The idea describes a composite solution to the original problem without introducing new punctuation.  Its space may be worth exploring even if it's infeasible.

-- 
Rocco Caputo <rcaputo@pobox.com>

> On Feb 22, 2019, at 11:27, David Nicol <davidnicol@gmail.com> wrote:
> 
> 
> what happens when it gets used twice in the same expression? Nothing else that aliases the topic uses expression scope (allowing of course that postfix for implies a block to its left) (and a list to the right) (put your ands on your hips)
> 
> /me is abruptly returned to transylvania via transit beam
> 
> On Fri, Feb 22, 2019 at 9:50 AM Rocco Caputo <rcaputo@pobox.com <mailto:rcaputo@pobox.com>> wrote:
> If there was a defined()-alike that set $_ for the rest of the expression:
> 
>   $foo = 'bar';
>   $_ = 'abc';
>   if ( defined2($foo) && /bar/ && say ) {
>     say $_;
>   }
> 
> Would print:
> 
>   bar
>   abc
> 
> -- 
> Rocco Caputo <rcaputo@pobox.com <mailto:rcaputo@pobox.com>>
> 
> 
> -- 
> "I don't know about that, as it is outside of my area of expertise." -- competent specialized practitioners, all the time


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About