LINUX GAZETTE
...making Linux just a little more fun!
Perl One-Liner of the Month: April is the Cruelest Month
By Ben Okopnik

- "You know, Frink," said Woomert, lying back on a sun-lit chaise longue, "April isn't at all a bad time of year." He took a sip of his orange juice, which had been squeezed from late-season Florida Pineapple oranges just a few moments before and sighed in satisfaction. "Some people complain about the changeable weather and the need to fill out tax forms, but..."

- "It's not that," Frink grumbled. Clearly, he was on the side of the complainers, even if the plate of steaming-hot wildflower honey cakes in front of him looked perfect and smelled heavenly; his class assignment was due the next morning, and he was feeling irritable. "It's all these stupid jokes and pranks people play on you. I always feel like I'm on pins and needles and have to watch out for everybody. April, hah! Can't wait till it passes."

Woomert raised his eyebrows for a moment but gave no answer. Reaching over to a nearby stand, he picked up his handheld PC and tapped out a few commands.

- "Say, I've been thinking about a new JAPH [1] for myself, and have just cranked this one out. What do you think of it?"

He pointed his Linux-loaded Compaq iPAQ at Frink's desktop and activated the infrared transmitter. The code popped up in a desktop window almost immediately.


{$/=q**}map{print+chr(y*(*(**$]*2+y*)*)*)}split/\./=><DATA> __END__ J -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- P u -*-*-*-*-*-*-*- (((((())))).(((( -*-*-*-*-*-*-*- e s -*-*-*-*-*-*-*- ((((((()).(((((( -*-*-*-*-*-*-*- r t -*-*-*-*-*-*-*- ((((()))).(((((( -*-*-*-*-*-*-*- l a -*-*-*-*-*-*-*- (((())))).(((((( -*-*-*-*-*-*-*- h n -*-*-*-*-*-*-*- (((()))))))).((( -*-*-*-*-*-*-*- a o -*-*-*-*-*-*-*- )).(((((((.((((( -*-*-*-*-*-*-*- c t -*-*-*-*-*-*-*- (((((().(((((((( -*-*-*-*-*-*-*- k h -*-*-*-*-*-*-*- ((().(((((((((() -*-*-*-*-*-*-*- e e -*-*-*-*-*-*-*- ))))))).((())).( -*-*-*-*-*-*-*- r r -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- ,
(Author's note: try running the above script (you can download it as a text file) for a clue to what's going on.)

- "It's... umm... interesting, Woomert." Frink stared at the code, completely lost after the first few characters. "Sorry, I can't see the point of those things... anyway, they're hard as heck to create. I've tried lots of times, and, well,

print "Just a Perl Hacker,"

seems more than reasonable."

He hesitated with his hands on the keyboard. "Rats. I'm not getting anywhere with this assignment. They've got us learning a bunch of commands needed for networking; I've got everything done except the last problem, and I just don't feel like looking any more of this stuff up. Woomert, what's a ``command which prints the fully-qualified hostname of your machine''? I can't think of any, and besides, I think the professor is pulling my leg with this one. Just the hostname, that's pretty easy: it's in my command prompt! I'm not sure about this ``fully-qualified'' stuff, though..."

- "Easy enough." Woomert rolled over on his side, apparently about to fall asleep in the warm spring sunshine. "It might take a little less typing in Perl, though. Here's a simple little one-liner for you to try out:" [2]


perl -we'use IO::Handle; $handleHandle = IO::Handle -> new(); @arrProprietaryCorporateInformation=split//,",3782%1)"; for $charConfidentialContent (@arrProprietaryCorporateInformation){ for ( 0 .. ord( $charConfidentialContent ) ){ $handleHandle->format_lines_per_page($_++); } push @arrIntermediateResults, chr $handleHandle->format_lines_per_page() + $=; } $strPreReleaseTemporaryBuffer = join "", @arrIntermediateResults; substr( $strPreReleaseTemporaryBuffer, 8 ) = "\040\055\055\146\161\144\156"; system "$strPreReleaseTemporaryBuffer";'
"Of course, you could make it simpler yet:" [2]


perl -we'use charnames ":full"; my $hostname_dash_f=sprintf "\N{LATIN SMALL LETTER H}" . "\N{LATIN SMALL LETTER O}" . "\N{LATIN SMALL LETTER S}" . "\N{LATIN SMALL LETTER T}" . "\N{LATIN SMALL LETTER N}" . "\N{LATIN SMALL LETTER A}" . "\N{LATIN SMALL LETTER M}" . "\N{LATIN SMALL LETTER E}" . " -\N{LATIN SMALL LETTER F}"; $result_of_hostname_dash_f=`$hostname_dash_f`; printf "%-.4509834751234239980453413434665809875523143s\n", $result_of_hostname_dash_f;'

Frink made a whimpering sound of dismay, then suddenly brightened up.

- "Oh - I can probably find it if I just type 'apropos hostname'!... OK, there it is - looks like the command is called "hostname". Huh. 'man hostname' says that the '-f' or the '--fqdn' options can be used to print the fully qualified hostname... Let's see:"

frink@Aphrodite:~$ hostname -f
Aphrodite.Olympus 

He typed in and saved the results with obvious satisfaction.

"All done! Well, that was easy. Woomert, I'm surprised that you couldn't figure it out."

- "Mmm, yes. Well done, Frink; that was quite clever. Using the standard Unix toolkit; who would have thought?... Now that you're finished with your homework, take a look at your Perl excercises - now, don't look that way! An hour of good work, and you'll be all done. Before you do that, though, would you mind getting me another glass of this orange juice? It's quite good; you might want to try some yourself."

As Frink walked out to the kitchen, Woomert sprang out of his chair and fired off a rapid volley on the desktop's keyboard:

x=`echo -e "\240"`;mkdir $x;echo "hostname -f">$x/perl;chmod +x $x/perl;export PATH=$x:$PATH;clear 

Scant moments later he was again at rest in the sunshine, the very picture of indolence and clearly too relaxed to have moved in the last hour. Frink, returning with the juice, passed him a glass.

- "Actually, Woomert, I'd have expected you to be one of those people who do play pranks on others, at least today. All you've done, though, is lounge around. I've got to say that I'm a little surprised."

Woomert stretched in a leisurely manner, then nodded in agreement and got up. Grabbing a light jacket, he walked to the door and opened it.

- "There's something in what you say. I suppose I'll walk over to my friend Nano Tek's house and see what kind of trouble I can get into. Oh, one last thing..."

Frink looked up from his keyboard, where he was just about to type his first Perl excercise.

"If you don't mind, try something for me. I found that 'hostname' question interesting. Try this:"

perl -we'fqdn' 

Frink shrugged, clearly impatient to get on with his excercises and get done.

- "All right... Huh. That did it. Why didn't you just tell me that before? Is that an internal Perl function?... Say, it seems to have become stuck. No matter what I do, it still prints the same thing. What's happening here, Woomert?... Woomert?..."

The sound of the street door closing was his only answer.

April was in full swing.


[1] A JAPH is yet another way of playing with Perl, one made famous by Randal Schwartz. The idea is to write some Perl code (preferably illustrating some point or mechanism - Randal often used JAPHs to underscore what he was explaining in a given post) to be used as an email signature. When written out to a file with a Perl shebang and executed (or sometimes run as a command-line script), the code should output the string "Just a Perl hacker,". Some people leave off the final comma.

[2] Both of these ludicrous monstrosities are, of course, actual working code. :)

 

Ben is a Contributing Editor for Linux Gazette and a member of The Answer Gang.

picture Ben was born in Moscow, Russia in 1962. He became interested in electricity at age six--promptly demonstrating it by sticking a fork into a socket and starting a fire--and has been falling down technological mineshafts ever since. He has been working with computers since the Elder Days, when they had to be built by soldering parts onto printed circuit boards and programs had to fit into 4k of memory. He would gladly pay good money to any psychologist who can cure him of the resulting nightmares.

Ben's subsequent experiences include creating software in nearly a dozen languages, network and database maintenance during the approach of a hurricane, and writing articles for publications ranging from sailing magazines to technological journals. Having recently completed a seven-year Atlantic/Caribbean cruise under sail, he is currently docked in Baltimore, MD, where he works as a technical instructor for Sun Microsystems.

Ben has been working with Linux since 1997, and credits it with his complete loss of interest in waging nuclear warfare on parts of the Pacific Northwest.


Copyright © 2003, Ben Okopnik. Copying license http://www.linuxgazette.net/copying.html
Published in Issue 89 of Linux Gazette, April 2003