- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Any programming language that is distinguishable from line noise merely makes it possible for people who should never have been allowed to touch a computer in the first place to think they know what they are doing.
Oh, and same thing for the user interface.
APL FTW!
(Anyone who doesn't know what line noise is can get their diapers off my lawn now, thank you.)
Admin
That would be Haskell, working inside IO monad without using "do" syntax sugar, using type inference to avoid specifying types explicitly (this gives away too much), and some pointless combinatorial acrobatic to further increase shock level.
I expect to be thanked for not whipping out my Kleisli IO arrow and doing this without any variables at all.
Admin
Admin
Admin
The above example looks like Klingon.
Admin
Admin
Admittedly, Perl does have some defaults that you just have to know, but that code becomes crystal clear looking at just the first couple of chapters of any Perl book. As long as you know that $_ is the default variable and that functions without arguments default to $_ (that is, chomp; really means chomp $_), it's actually quite easy to read, and pretty elegant. You don't have to like it, but don't say it makes for a bad language...to me, Perl is like poetry to Python's dry, academic prose.
Of course, TRWTF is that this introduces a disk I/O slowdown into the mix (which seems oddly common...the default setup for the EeePC 701's wireless featured a Perl script that wrote the output of 'ps aux' to disc and read it back much the same way). The right way is to use datetime, of course, but using only builtins (and assuming you're on a UNIX-like system), you could use
@datevals = split(/ /,qx"date");
for this entire code chunk. I'm not sure why people ignore that so much.
Admin
Admin
[quote user="JH"][quote user="Ross"][quote user="davee123"] I thought one of Perl's biggest credos was "there's more than one way to do it"? [/quote]
Yes, but some ways are better than others, and some ways are just plain bad.[/quote]
Some old guy once said: "A Perl program is considered correct if it does the job before your boss fires you." Now, I'm not certain if it was said by Einstein or Mark Twain.
Admin
That's a mis-quote. The original quote is "there's more than one way to be done by it"...
Admin
The elderly tend to repeat themselves, Pops. Knock. Knock. Who's there? Flaming Poop!
Admin
Admin
Can't get behind you on that. Unless there's some reason to avoid the system call (e.g. performance, portability, etc.) the former example is much clearer to the next person to come along than the latter. Perl already has enough of a (somewhat deserved) reputation as a write-only language, so sacrificing "purity" for clarity should be a no-brainer. Of course, this is a bad example, because using the library function would probably be the best solution. Nevertheless, Perl is decidedly a language that could benefit from the Taco Bell Programming paradigm.
Admin
Right. Thank goodness.
I was worried for a minute there. </deadpan>
Admin
That fact that he opted to put $_ in his split command but not his chop command indicates he's not all that familiar with Perl and its shortcuts.
Admin
First off, "perl -n" says to run the script that you've written on all of the files that are specified on the command line. Handy if you have a small regex or something that you'd like to run on a set of files.
But part of the way it does this (I learned stupid trick from Damien Conway, who got it from some other famous Perl guru whose name I forget) is that it wraps your code in a while loop. So, knowing that, within your code, you can break out of the while loop using a closing brace. And to make sure that the next brace lines up without any errors, you can start a new brace. Effectively, it means you can do:
perl -ne '}{' *
And what you'd think would be a syntax error is avoided.
As for my example, the other bit I just picked something out of a hat that similarly did nothing:
$^X -v
;The backticks specify that it's a system command (executed using the sh shell)-- you can do things like:
echo hi
;Or whatever. The next part is the command that it should run-- and I've got it running $^X, which is a special Perl variable for the Perl binary itself. In other words, if you ran:
/usr/bin/perl -e 'print $^X . "\n";'
You'd get an output of:
/usr/bin/perl
So, effectively, it's running "perl -v" on the command line, and doing nothing with the output.
Anyway, that's all it does-- a fat lot of nothing. But it looks scary! If my memory or creative side was better, I could maybe dig up an example where that mismatched-brace thing was actually USEFUL, but I figured that since this was an example of how to horrify people with Perl, it was unnecessary.
Damn straight. And there are almost always more BAD ways of doing things than GOOD ways :)
DaveE
Admin
:P
/me is new to Perl, but it's an awesome language!
Admin
O.K. Mr.I-know-quite-a-number-of-programming-languages-and-can-see-a-new-one-and-figure-it-out (before any one goes crying about my tone this is tung in cheek...) Try this one on for size :
What's it do? Huh? Tell me that, or what language it even is. (For the record I did not write this code, I copy pasted off a website, so copy/pasting into Google is cheating)
But in all seriousness, have you ever looked at PHP, or bash for that matter? That makes my brain bleed (the PHP, I just use perl instead of bash)...
I do agree with you: if you don't know anything about the language, reading the language can be confusing (did you even try Google, or did you look at it see characters you don't recognize and say "OMG this makes me want to throw up"). This can be said of an English reader looking at at a German textbook on the American Civil war.
I recognize that if you don't know what the sigils tell you perl becomes even more muckier.
This function (or method) returns a list. (That's something else perl does, return more than on value...) the split function takes two parameters--the value to split on, and the scalar to split--and returns a list, which we assign to the @datevals array.
Allow me to say this: perl is an idiomatic language; if you don't know the idioms, then understanding the language becomes more difficult. I have a library of perl books. Conversely I have a library of Java books. Java is TRWTF.
+1; the bold part is especially true of perl. which idiomatically is a true value, thus creating an infinite loop. (false values are: 0, '0', '', 'undef', everything else is true) In this case 'False' is used in string context, thus it is not equal to (ne) '' and is evaluated as true. Exactly my point. TIMTOWTDI is more of a motto than a creedo. IMO this is the funniest post...Admin
Admin
That would be INTERCAL. Like Perl, it is a joke language, but this one is never taken seriously.
What it does? Oh, boy, it doesn't even compile because the programmer was not polite enough.
Admin
Something else Perl does is automatically flattening your lists: ((1,2),(3,4),(5,6)) is the same as (1,2,3,4,5,6), because one night Larry Wall need to flatten lists, so he decided to put that into the language. Then people started hacking around with typeglobs (whatever monstrosity that is), until pointers^H^H^H^H^H^H^H^Hreferences were grafted into the language to fix the auto-flatten mistake.
Admin
THEDATE is a file-handle. By putting pointy brackets around it you can read from the file.
You can read the whole file into a variable as an array of strings by saying: @lines = <THEDATE>
One clever|stupid thing in Perl is that you can often omit a variable name as Perl is more than happy to give you an implicit one.
When you "while" over an array, you get one array item per iteration. In this case, by omitting the variable name, the while loop puts a fresh line from the file into the implicit variable (which is only once explicitly named in this code, we'll come to that later) per iteration. No biggy.
The line you just read in still has a line-feed/carriage-retun, and you use 'chop' to, well, chop it off. You now have a string that is essentially the data you want. It is fields separated by spaces.
Now, here comes 'split'. It splits a string into smaller strings, based on a separator character you supply.
Here, it splits the string in variable "$_" (hey, that is the implicit variable that was mentioned earlier) into smaller strings at every space character. This gives an array of small strings, which gets stored in the array @datevals.
And that, as they say, is it. Easy, idiomatic, readable, fast (to program, that is).
I've programmed in a lot of languages. But Perl is the only one where I have the feeling that it works with me, instead of against me.
Admin
Admin
The real WTF is calling 'date' without a full pathname.
Now every yokel with write access to the current directory can run with potentially elevated privileges.
Step 1: cp evil_executable ./date
Step 2: Wait for Perl to call your package-o'-fun
Admin
It has perfectly fine basic date/time access. If you throw in the DateTime library, I'll put $1,000 to any other language's date routines that DateTime is faster and more complete. It is simply the best date library available. It makes the date routines in other languages look very pitiful.
The fact of the matter is, the programmer that wrote today's snippet would use the same process in any language. The people who are mocking perl in this post really shouldn't be programming and are more dangerous and create more WTF code than the OP ever could hope to do.
Admin
& refers to a subroutine. It isn't necessarily a subroutine call. Take the following:
Of course, if just used alone, it will call the subroutine (as it would without the sigil).
It's still misleading though to say that the sigil refers to a call. ;)
(As always, I reserve the right to be corrected)
Admin
The syntax for a three element list of two element arrays is:
([1,2],[3,4],[5,6])
The reason for this is because perl has explicit referencing/dereferencing opposed to automatic. You know what automatic referencing/dereferencing is?
SLOW. SLOW AS MOLASSES.
Admin
You're right! They should have
to get the file name
Admin
Props. You're currently about as useful as the chariot.
Admin
[quote user="Ross"][quote user="davee123"] I thought one of Perl's biggest credos was "there's more than one way to do it"? [/quote] This is a poor excuse for punching co-workers in the face with inconsistent coding practices
Admin
Honestly, I'm not sure we've ever had a PROBLEM with them, but I fear them because of potential issues with performance and portability. Security's another one, but I'm not too worried about that:
Performance: I believe the backtick version spawns off two child processes ('sh' and 'date'). Probably not very noticeable, but might be if abused. It's probably less efficient than the pure-Perl approach.
Portability: We've moved from different versions of Solaris and Linux, and in theory some code could go to other platforms (there's never been talk of Windows, thankfully). Anyway, I'm leery that "date" might not always have the same command options and outputs. We had that happen with "ps", I know, and a few other commands, but I can't recall which ones. Solaris to Linux caused us some headaches in that department.
Security: This isn't too big of a deal-- but since the path to "date" isn't specified, it could theoretically be picking up a different binary than you expected. Not likely, but possible.
Agreed. My vote would be for using standard libraries. Otherwise, if you don't know if the library's installed, use pure Perl. It's helped me out a few times on the rare occasions when I HAVE had to run Perl on Windows. I try to avoid backticks and system calls unless it's either really necessary, or I'm writing a throw-away script.
DaveE
Admin
Admin
And that, my friends, are why Billing Systems are neither Shiny nor New.
Admin
Admin
When I was at school they taught us how to use the index. You know, that boring list at the back of the book. It tells you on which page to find what you're looking for. Can be useful. If not, throw the book away and get a better one.
Admin
Admin
Maybe there should be an obfuscated PERL contest. No, wait. It comes that way.
Admin
If it wasn't for Perl, we wouldn't have camels.
Admin
I like the split implementation, very nice, but here is how you might normally write it if you're not trying to be intentionally obtuse:
Admin
I think that may be exactly what this code is for.
If a read-only the.date file is placed in the directory it'll revert to that date, allowing the user to override the default current date taken from the system. It's also useful for testing Y2K/2038 type bugs in the processing.
Admin
So while steps through an array? Really? And you're a Perl programmer?
So, which of these count to three and which is an infinite loop?
The <FOO> operator simply assigns to $_ as a side effect, and returns the line or undef at EOF. The while statement just checks the boolean value of the returned line. Since undef is false, an EOF ends the loop.
And if you did this:
That will do (roughly) what you're talking about. Obviously, the way to step through an array is with foreach.
And the first complaint that while(<FOO>) is dumb is correct. In python:
That's the way to do it: make a file handle an iterator. No side effects, no magic variables, and the context manager handles exceptions properly.
Admin
So... like this then?
DaveE
Admin
Hey, you beat me to it ...
A week or two ago, the latest service pack of my company's product was pulled from the download site because of this -- IN JAVA :-O
Apparently someone decided the only way to get the date IN JAVA was to issue the date command (actually 'time' because it might have been Windoze), capture it to a file, then read it in. When a customer tried it on a localized, non-English, system, the install blew up because it couldn't parse it. Did I say "IN JAVA"?
Admin
You set $PATH to contain . then? (Also you forgot chmod)
Admin
I meant hash MAP... hash map... What I was make clear is it is referring to the whole hash, not just a part.
You're right, strictly speaking saying & is a subroutine call is incorrect, & represents a subroutine, weather it's a call or a reference.
Admin
Apart from the fact the code in question is full of WTFs of it's own, I have to agree with the first poster - TRWTF is Perl, and PHP, and pretty much all the other languages with non-C-like syntax. Visual Basic? Garbage. (albeit not as bad as Perl) Haskell? Ruby? Scala? Bad, bad, bad.
If you want to get stuff done, and make it maintainable, and be able to work in a team environment, you got to use either C++ for performance-oriented development, or Java/C# if you're OK with managed.
Seriously, the only reason all these other languages are around is because a bunch of nerds have too much time on their hands which they spend inventing bullshit, instead of getting drunk or something.
Admin
Since it's Perl, I'm not sure you can truly say that solution is wrong or wtf. Of course, since that sort of thing is considered perfectly valid in Perl you could argue that Perl is TRWTF.
In spite of that, I happen to like Perl. So... maybe I'm a wtf too, I dunno. shrug Is the "wtf" property transitive?
-- Furry cows moo and decompress.
Admin
I've got 4 jobs. Sometimes 5 or 6. I can do all of these jobs because perl makes it easy.
Addendum (2010-12-31 00:48):
I'll repeat since it appears you are incapable of comprehension: The person who wrote this code would do the same thing in any language: They would think it is a good idea to write the date to a file, read it back in to the file, and then do some strange obscure procedure to read the data back in from the file.
People who are somehow making the fact that perl was used somehow relevant in any way are providing written evidence of gross incompetence.
Admin
Shouldn't that be 16:39:49 23 ﬨטב 5771?
Admin
Point-free notation can be very enlightening, although I'll be the first to admit it might look incomprehensible to people unaccustomed to it. Also, point-free formulae are completely concatenative, in that you may replace any symbol with its definition, and any balanced expression with a symbol, fully preserving the semantics. This can be extremely useful.
Re split: until I tried to write it, I didn't realize myself that it actually was a beauuutiful anamorphism. Haskell never ceases to enlighten me.
chop is wrong actually, I forgot much of my Perl, but perldoc -f says it's actually init :: String -> String. Not needed here anyway: I looked in System.IO, and hGetLine (hGetLineBufferedLoop, rather) discards the newline on its own.