One of the stated reasons some people hate perl is that there are things like perl golf, in which the object is to get it done with minimal keystrokes - often leading to unreadable code.
However...it can be fun too, for trivial examples. Here's some trick code - doesn't do quite the same as the above (no linefeeds) but close, and a lot less strokes.
Code: Select all
#!/usr/bin/perl -w
use strict; # flag questionable constructs, -w above turns on warnings
for (0 .. 9) {print}
I used -w to turn on warnings globally, as well as the sanity-saving use strict. Strictly speaking, we don't need either one here - I can reduce the strokes, but I like to encourage their use. Often via the error messages and warnings these create, one can avoid all need for a debugger (perl has one, but ugh).
I make use of the perl pronoun, which is named $_, but used implicitly in many functions if you don't specify something else. The 0 .. 9 construct creates a list of numbers in that range (and is faster than incrementing a variable, and takes up less memory). print prints $_ in the absence of an argument. I don't need a semicolon on the last line of any block... In perl, for is synonymous with foreach, which takes a scalar (in this case the pronoun "it", or $_, but you can have your own variable if you like there) and a list, which here is generated by the .. range operator.
I'd bet this could be taken further...
Posting as just me, not as the forum owner. Everything I say is "in my opinion" and YMMV -- which should go for everyone without saying.