develooper Front page | perl.perl5.porters | Postings from May 2009

Re: feature 'switch' as an expression?

Thread Previous | Thread Next
From:
Paul LeoNerd Evans
Date:
May 9, 2009 05:52
Subject:
Re: feature 'switch' as an expression?
Message ID:
20090509134510.0d939f84@nim.leo
On Fri, 8 May 2009 20:56:47 -0700
chromatic <chromatic@wgz.org> wrote:

> On Friday 08 May 2009 20:00:25 David Nicol wrote:
> 
> > >> You're new here.  "Nobody relies on it" is code for "somebody will now
> > >> show me code which relies on it". :)
> 
> > I had hoped this thread would die off and I wouldn't have to write
> > this but here's a reasonable scenario for code relying on a for loop
> > returning undef.  It is hypothetical, of course, but this is the kind
> > of entirely innocent thing that would break.
> >
> >
> >     sub brake_for_safety { ... }
> 
> There's a point at which a reasonable person should be able to say that anyone 
> blindly upgrading the dependencies of a braking control system without running 
> any tests whatsoever is so incompetent that he should give up programming 
> altogether, forever, and should possibly spend the rest of his life stacking 
> rocks.

I originally made my point as a half-serious, half "reductio ad absurdum"
demonstration on why we should think about it more carefully...

I don't think it would be safe to wholesale change the behaviour of half
of all the perl code ever written; that would be ... unwise.

Instead, if we want such an ability, why not protect it by a 'feature'
bit? We now have this lovely "use feature" introduced at 5.10 for just
such a purpose. If people want to enable it, then let them:

  use feature qw( control_as_expression );   # someone find a better name

  # ?:
  my $name = if( $person eq "Fred" ) { "Bob" } else { "Jane" };

  # map
  my @squares = foreach my $number ( @nums ) { $x * $x }

  # chain of ?:, like the OP requested
  my $colour = given( $colname ) {
     0 when "black";
     1 when "red";
     2 when "green";
     3 when "yellow";
     ...
  };

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk
ICQ# 4135350       |  Registered Linux# 179460
http://www.leonerd.org.uk/


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