237 lines
11 KiB
HTML
237 lines
11 KiB
HTML
<!--startcut ==============================================-->
|
|
<!-- *** BEGIN HTML header *** -->
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
|
<HTML><HEAD>
|
|
<title>Perl One-Liner of the Month: April is the Cruelest Month LG #89</title>
|
|
</HEAD>
|
|
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#0000AF"
|
|
ALINK="#FF0000">
|
|
<!-- *** END HTML header *** -->
|
|
|
|
<!-- *** BEGIN navbar *** -->
|
|
<!-- *** END navbar *** -->
|
|
|
|
<!--endcut ============================================================-->
|
|
|
|
<TABLE BORDER><TR><TD WIDTH="200">
|
|
<A HREF="http://www.linuxgazette.com/">
|
|
<IMG ALT="LINUX GAZETTE" SRC="../gx/2002/lglogo_200x41.png"
|
|
WIDTH="200" HEIGHT="41" border="0"></A>
|
|
<BR CLEAR="all">
|
|
<SMALL>...<I>making Linux just a little more fun!</I></SMALL>
|
|
</TD><TD WIDTH="380">
|
|
|
|
|
|
<CENTER>
|
|
<BIG><BIG><STRONG><FONT COLOR="maroon">Perl One-Liner of the Month: April is the Cruelest Month</FONT></STRONG></BIG></BIG>
|
|
<BR>
|
|
<STRONG>By <A HREF="../authors/okopnik.html">Ben Okopnik</A></STRONG>
|
|
</CENTER>
|
|
|
|
</TD></TR>
|
|
</TABLE>
|
|
<P>
|
|
|
|
<!-- END header -->
|
|
|
|
|
|
|
|
|
|
<p>- "You know, Frink," said Woomert, lying back on a sun-lit <i>chaise longue</i>,
|
|
"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..." </p>
|
|
<p>- "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." </p>
|
|
<p>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.
|
|
</p>
|
|
<p>- "Say, I've been thinking about a new JAPH <a href="#1">[1]</a> for myself,
|
|
and have just cranked this one out. What do you think of it?" </p>
|
|
<p>He pointed his Linux-loaded Compaq <a
|
|
href="http://www.ipaqlinux.com/">iPAQ</a> at Frink's desktop and activated
|
|
the infrared transmitter. The code popped up in a desktop window almost
|
|
immediately. </p>
|
|
<p> </p>
|
|
<pre>
|
|
<hr width="100%">{$/=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 -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- ,<hr width="100%">
|
|
</pre>
|
|
<b>(Author's note: try running the above script (you can download it as a
|
|
<a href="misc/okopnik/japh.pl.txt">text file</a>) for a clue to what's going on.)
|
|
</b>
|
|
|
|
<P>
|
|
- "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, </p>
|
|
<p><pre>print "Just a Perl Hacker,"</pre></p>
|
|
<p>seems more than reasonable." </p>
|
|
<p>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..." </p>
|
|
<p>- "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:"
|
|
<a href="#2">[2]</a></p>
|
|
<pre>
|
|
<hr width="100%">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";'<hr width="100%">
|
|
</pre>
|
|
"Of course, you could make it simpler yet:" <a href="#2">[2]</a></p>
|
|
<pre>
|
|
<hr width="100%">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;'<hr width="100%">
|
|
</pre>
|
|
<p>Frink made a whimpering sound of dismay, then suddenly brightened up. </p>
|
|
<p>- "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:" </p>
|
|
<pre>frink@Aphrodite:~$ hostname -f
|
|
Aphrodite.Olympus </pre>
|
|
<p>He typed in and saved the results with obvious satisfaction.<br>
|
|
</p>
|
|
<p>"All done! Well, that was easy. Woomert, I'm surprised that you couldn't
|
|
figure it out." </p>
|
|
<p>- "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." </p>
|
|
<p>As Frink walked out to the kitchen, Woomert sprang out of his chair and
|
|
fired off a rapid volley on the desktop's keyboard: </p>
|
|
<pre>x=`echo -e "\240"`;mkdir $x;echo "hostname -f">$x/perl;chmod +x $x/perl;export PATH=$x:$PATH;clear </pre>
|
|
<p>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. </p>
|
|
<p>- "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." </p>
|
|
<p>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. </p>
|
|
<p>- "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..." </p>
|
|
<p>Frink looked up from his keyboard, where he was just about to type his
|
|
first Perl excercise. </p>
|
|
<p>"If you don't mind, try something for me. I found that 'hostname' question
|
|
interesting. Try this:" </p>
|
|
<pre>perl -we'fqdn' </pre>
|
|
<p>Frink shrugged, clearly impatient to get on with his excercises and get
|
|
done. </p>
|
|
<p>- "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?..." </p>
|
|
<p>The sound of the street door closing was his only answer. </p>
|
|
<p>April was in full swing. <br>
|
|
</p>
|
|
<p> </p>
|
|
<hr width="100%"><a name="1"></a>[1] A JAPH is yet another way of playing
|
|
with Perl, one made famous by <a
|
|
href="http://www.stonehenge.com/merlyn/">Randal Schwartz</a>. 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. <br>
|
|
<p><a name="2"></a>[2] Both of these ludicrous monstrosities are, of
|
|
course, actual working code. :)
|
|
</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- *** BEGIN author bio *** -->
|
|
<P>
|
|
<P>
|
|
<P> Ben is a Contributing Editor for Linux Gazette and a member of
|
|
The Answer Gang.
|
|
|
|
<!-- *** BEGIN bio *** -->
|
|
<P>
|
|
<IMG ALT="picture" SRC="../../gx/2002/tagbio/ben-okopnik.jpg" WIDTH="199"
|
|
HEIGHT="200" ALIGN="left" HSPACE="10" VSPACE="10">
|
|
<em>
|
|
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.
|
|
|
|
<p>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.
|
|
|
|
<p>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.
|
|
</em>
|
|
<br CLEAR="all">
|
|
<!-- *** END bio *** -->
|
|
|
|
<!-- *** END author bio *** -->
|
|
|
|
|
|
<!-- *** BEGIN copyright *** -->
|
|
<hr>
|
|
<CENTER><SMALL><STRONG>
|
|
Copyright © 2003, Ben Okopnik.
|
|
Copying license <A HREF="../copying.html">http://www.linuxgazette.com/copying.html</A><BR>
|
|
Published in Issue 89 of <i>Linux Gazette</i>, April 2003
|
|
</STRONG></SMALL></CENTER>
|
|
<!-- *** END copyright *** -->
|
|
<HR>
|
|
|
|
<!--startcut ==========================================================-->
|
|
<CENTER>
|
|
<!-- *** BEGIN navbar *** -->
|
|
<!-- *** END navbar *** -->
|
|
</CENTER>
|
|
</BODY></HTML>
|
|
<!--endcut ============================================================-->
|