Hacker Newsnew | past | comments | ask | show | jobs | submit | flatwhatson's commentslogin

In all my years of using and loving (and abusing) the shell, I've never encountered apropos. I'm really amazed that something so fundamental has somehow been left out of the many unix/shell articles (such as this one) that I've read. Anyhow, you've made my day. Thanks!


Blocks are also available in Perl, in the form of anonymous subroutines. They follow Perlish conventions for argument handling (rather than having a neat inline syntax like Smalltalk or Ruby), but they're most definitely the same thing!

  my $block   = sub {             say "w00t, blocks!"   };
  my $closure = sub { $block->(); say "w00t, closures!" };
You'll have to look further for something Ruby does that Perl doesn't :)


And in Perl6 you can drop the sub bit:

    my $block = { say "w00t, blocks!" };
And there are also pointy blocks for neater stuff:

    my $pointy_block = -> $text { say "$text, blocks" };
    $pointy_block("ne@t");


Yeah, smalltalk blocks are almost identical to ruby blocks.

Article just acted like that was a ruby invention (which they corrected)

My comment had nothing to do with perl.


I probably should have replied to rtomayko's comment rather than your own. I'd edit the tree, but it's immutable!


Here's one more efficient way... use perl!

  #!/usr/bin/perl
  
  use Modern::Perl;
  use List::Util 'reduce';
  
  sub cook {
    my ($i1, $i2, $f) = @_;
    say "get the $i1";
    $f->($i1);
    $f->($i2);
  }
  
  cook "lobster", "water",   sub { say "pot "  . shift };
  cook "chicken", "coconut", sub { say "boom " . shift };
  
  my @a = (1, 2, 3);
  
  map { say $_ * 2 } @a;
  map { say $_     } @a;
  
  sub my_sum {
    reduce { $a + $b } 0, @_;
  }
  
  sub my_join {
    reduce { $a . $b } "", @_;
  }
  
  say "sum "  . my_sum(@a);
  say "join " . my_join(@a);


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: