Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Perl has these tiny neat features that I still miss in other languages. Like unless (opposite of if), and postfix syntax.

    print "It worked!\n" unless $error;
    print "Item: $_\n" foreach @items;


Ruby has some of these, too, thanks to its Perl heritage.

    puts "It worked!" unless error
    i = 0; puts "Item: #{i+=1}" while i < 5
Unfortunately foreach didn't survive, but thankfully arrays have methods to achieve similar concision:

    items.each {|i| puts "Item: #{i}"}


Until some joker writes an unless/else block. Gah. I hope someone implements an elsunless keyword /s


I find this:

    error "The eggs are not in their basket"
        unless $eggs_in_basket;
clearer than this:

    error "The eggs are not in their basket"
        if !$eggs_in_basket;
because "unless" is more visible than "!", and because the condition indicates the "normal" case.

I agree that unless/else blocks are quite unhelpful. The double negative doesn't help.

That said I am guilty of writing some unless/else blocks, but the reason is always performance. "unless ($x)" is faster than "if (!$x)". Only one opcode faster, but in something optimised for speed that's a few percent, and therefore a goal, especially if there are many of them. For something like "++$y unless $flag" it's more than a few percent.

As with many other things in Perl, and other languages in the "slow" class (Python, Ruby etc), there are no dataflow optimisations. Things like "my $x; $x = 1" are slower than "my $x = 1;". Yet still there are times when something is worth doing faster, without the big jump of rewriting in a different language.

That's completely different from the "fast" languages, like C, C++, C#, and nowadays JavaScript. I've come to appreciate that I wish all languages did trivial dataflow optimisations and inlining, even if they are in every other respect interpreted languages, because absence of dataflow motivates people to write uglier and more difficult to understand code in tight hotspots, when code clarity would be really valuable.


Yeah, I hear you. I actually use `doit() unless $something;` quite often, but then change it to `doit() if ! ($something || $something_else )` immediately the need arises. That is I always think about it when I"m writing an unless statement.


I've never been a fan of unless blocks (as opposed to the unless postfix, which is a bit easier to swallow). The lower precedence spelled out boolean operators (not/and/or) mean you can just use if not instead, which is just as readable in most cases and isn't confusing in the else case.

e.g.

    unless ( $foo ) { }
    if ( not $foo ) { }


yeah I tend to avoid unless - definitely in blocks, but it's ok for some postfixes


I spent much of my career working primarily in Perl. Now I use it when messing around with older codebases, but not for new work. However, I still use it pretty often for one-liners because of these neat features. For instance:

    perl -E 'say for grep {/o/} @ARGV' one two three


I hate unless! It always makes me stop and think a few times, causing double or even triple negatives in my brain. What's wrong with something like "print this if not $error"?

Maybe that's because I'm not a native English speaker?


I am not native English speaker but I like it in ruby, helps with double not:

    if not not_implemented
    unless not_implemented


Well, print this if not $error doesn't work in most modern languages either. It's

    if not error:
        print this


I miss those too and I'm constantly annoyed Python doesn't have more one liner support.




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

Search: