- 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
Yeah, yeah. Pedantic. Writing specifications does that to you.
Admin
The RWTF is not knowing the difference between "it's" and "its".
Admin
I also like that the original code doesn't even work for files less than ~ 1 meg. Take "crap.txt", a roughly 5kb text file:
WTF, indeed.
Admin
Yes.
Admin
Hah yes, I'm aware of that, but I was trying to make a simple point with out complicating things :)
Although I didn't think the size of the file on disk was just "rounded up" to some block size as a power of two... I thought that was because files are assigned blocks on disk (what ever that block size happens to be) whether or not the file uses it. That is, if the block size is say 512 B but the file only needs 128 B, it's size on disk is still 512 B? Meh I don't know, I'm tired and rambling. And would love some captcha. I mean waffles.
Admin
Hold on...does PERL not have an easy way to destructure the output of commands into variables? (I mean to parse it very simply into a bunch of variables, then select the contents of the required variables)
Admin
fhiting!
Admin
Anyway the right way to do it is : wc -c filename | awk '{print $1}'
Admin
OK, enough of that. I previewed my post; I love how the forum software double-spaces my stuff.
Admin
Pretend for a moment you inherit an application in a language for which you only have a basic understanding. Which is going to make it easier to read:
filesize = -s filename filesize = filename.size ?
Admin
Admin
Those who know Perl in the pre-5.0 days are familiar with the -X stat operators. We should probably stop using them because they aren't very communicative and are only good for quick hacks and shell scripts that no one will maintain.
This was fixed years ago. The way it should be done:
Admin
Even if the program takes 24 hours to run, 75 minutes is still a huge improvement. If it takes more than 24 hours to run...why are they using Perl?
Of course, they must be making a ton of file size calculations in that time to make the change that significant. If the piped method takes .01 seconds (which is a drastic overestimate), and the perl "-s" method returns instantly, then 75 minutes difference means they looked up a file size 450,000 times while executing this perl script.
Admin
You would not have to guess. You would read "perlfunc", which is the Perl function list, and you would find it under "Functions for filehandles, files, or directories".
Gee that's hard.
Admin
I did that once and thought it was a dead clever way to save 3 lines of code. In fairness, I was about 14 at the time.
Admin
Admin
The guy was using FORTRAN. This means that Google was probably not invented yet when they first wrote the code. The guy would have to dial into a Wildcat! BBS with his 2400 baud modem to get the answer.
Admin
Admin
PipedInputStream and PipedOutputStream may help here. Not used them yet (but am about to in a project coming up). From Java in a Nutshell:
PipedInputStream
This class is an InputStream that implements one half of a pipe and is useful for communication between threads.... ... Data read from a PipedInputStream object is recieved from the PipedOutputStream to which it is connected ...
Admin
There is. The concept is called Channels.
However took me some time to find out, too.
Admin
Admin
Your, uh, second substitution there is a no-op. But you knew that. ;)
Admin
Admin
Many forms of Government have been tried, and will be tried in this world of sin and woe. No one pretends that democracy is perfect or all-wise. Indeed, it has been said that democracy is the worst form of government except all those other forms that have been tried from time to time.
-Winston Churchill
Admin
man perlfunc / size <ENTER>
Oh, there it is.
Admin
Ok sure, it works better for interpreting code, but for actually writing the code (which is what this whole WTF and the original post by smitty is about), using -s or some verbose description is just as effective... you still need to search somehow to know what to use. And if you're just going to guess you should be shot on site.
Admin
Well said. Few things piss me off more than to be talked down to by some dork with stunted social skills. It's people like them that give our entire industry the bad image of being nerdy, antisocial, loosers who couldn't get laid with $10,000 in a whore house.
Don't be quick to call someone a moron or feel that you are better than them in anyway because you've spent more time then the other person learning a particular subject. The real moron is the one who can't understand the difference between ignorance and stupidity. I am ignorant about Pearl because I have not studied it, I am not stupid because I can learn it.
Their ilk is what keeps people from learning more about computers. Most people don't want to be insulted because they don't know how to open up notepad or because they don't know what 'ls' does, so they just don't bother learning.
Admin
Re fitting python into the unix philosophy have a look at an introductory talk I gave recently to some university students: http://www.pixelbeat.org/talks/linux_and_python/
Admin
Re fitting python into the unix philosophy, have a look at an introductory talk I gave recently to some university students: http://www.pixelbeat.org/talks/linux_and_python/
Admin
The coder obviously knew about shell scripting. Otherwise there would not be a shell call with process-pipe operators. The coder clearly never understood anything, but just copied "what works".
Shell also has the '-s' operator. It is one of the "test" predicates:
if [ -s $FILE ]; then echo "good file" fi
When I started my job, they gave many perl scripts (perl4) like this to fix. They had shell calls for everything, even
date
. Damn was that slow!Admin
I like Perl and I've been using it for over a decade, but I'm not about to defend '-s' as being 'intuative' in this day and age, I think Smitty's comments were quite accurate and reasonable and indicative of how how most reasonable developers from a wider range of backgrounds would interpret some of Perl's (now arguably anachronistic) behavior.
I'm surprised at the number of obnoxious comments like your though, I think some people are unable to be objective about it, which is pretty pathetic really (it's just a scripting language, and an increasily creaky one at that). It's not like anyone was insulting Larry's mother (just his baby, boom tsh).
Perl 5 realative suckyness has increased noticeably in the last few years as comparible languages have improved at a much faster than it has over the last few years (roll on Perl 6...).
It's still able to hold it's own as a great Unix shell scripting language, that doesn't mean assuming everyone using it comes from the same background and would therefore immediately be familer with it in the same way is a good idea (and that doesn't excuse being a jackass either).
As it's been brought up:
Everyone knows PHP has lots of inconisistances when it comes to function naming conventions, that includes it's developers - it's old news (and they've been doing something about it).
That doesn't excuse them or mean that having quite as many internal functions as it does is something other languages should seek to replicate, but at least it has them - languages like Java and Perl are all the poorer for not having somewhat similar functionally for common operations IMHO.
When it comes to languages, some people get stuck in ruts endlessly defending the familer I guess (I'm sure there are lots of old of COBOL programmers who feel the same way).
Admin
Of course you can connect an InputStream to an OutputStream.
Just do it like this:
Admin
About the only thing one can say for the "wc ..." solution is that it actually READS the file, so you know a teensy bit more than what -s tells you. You know the runner has permission to read the file, and you know the file's blocks are readable if you get a non-zero answer.
Also depending on the end-of-line convention, you'll get a slightly different answer from wc, won't you?
And does wc count trailing blanks?
Hmmm....
Admin
That's why there's the over-voweled
apropos
.Admin
If you use -s, you get the access operators (-r, etc.) for free. A single stat() call fetches that all at once.
Admin
There is a guy here who I used to respect as a good coder. But he's only known C/C++/Java.
He suffers from the "C as a first language" syndrome. I've seen some of his Java where he tests boolean vaiables as if has_lock == true then
He tried to learn some Perl to better understand how I build prototypes. Most of his code consisted of multiple calls such as system( ... ); system( ... ); system( ... ); without any variables in Perl at all.
Just last month he decided to change one of my demo cgi scripts. Changed it to use a system call to sed exactly in the same way as in the article.
Admin
It's called the possessive case. Back to middle school for the Gramma Police.
Admin
If this code change reduced anything by 75 minutes per run then the rest of the application must be a horrible mess.
As someone above estimated: half a million file sizes on every run !
Admin
Giving sysadmins and other shell-monkeys access to perl or other scripting language can produce some code that looks awful, runs slow, and is fragile as hell. However, it's almost always prettier, faster, and less fragile then what they were using before.
So while a good-natured laugh at their attempts is fine, I refuse to ridicule someone that engaged a tool outside of their domain to improve their productivity for not taking the time to master a language or best practices. In this case we're talking about chemistry grad students, not programmers. It's likely programming is just a necessary evil of their research, and as soon as possible they will hoist it on to their own grad students.
Now when someones been using perl for 4 hours a day for years and still produces stuff like this, it's time to take them behind the shed and tie them to a library of Perl books.Even if programming isn't their passion it's enough of their job that they are robbing themselves by not becoming competent with it.
Admin
It's (i.e., "it is") not rocket science.
Admin
$10 says using -r is a hell of a lot faster than making a call to wc and checking the return value.
Admin
(Oh, snap! ;-)
Admin
Well, you certainly can do:
@q = split (/\s+/,
uptime
);and get the individual fields into the array q (ie, $q[0] will have the current time etc.). You can split at any regex, not just \s (sequence of whitespace).
Admin
Doing a universal replacement with the -s variant could break things, since the special "whatever was last stat'd" filehandle "_" will give different results.
Admin
Truthfully, my biggest complaint against perl is the shear volume of operators. From what I understand, Perl 6 isn't going to fix this...its going to make it worse. A figure I've heard quoted is around 200 new operators. While yes, I can read perl, it takes me 5-10 times longer to read and comprehend a perl script than a comparable c++ program even. Having to discern between hundreds of operators that behave differently in different contexts requires a lot more concentration
The preference given to operators over libraries makes navigating perl a nightmare for an outsider. As a maintainer I'd rather see:
a = list(...) a = [b + 1 for b in a]
or:
int a[] = {...} const int a_size = sizeof(a) / sizeof(*a); for (int i = 0; i < a_size; ++i) a[i] += 1;
than:
$a ^+= 1;
Another problem with so many terse, similar operators is debugging.
Example:
$a = 1; $b = 2; $c = $a // $b;
Now, I when I write this, I know I mean to default $c to $b if $a is undefined. But, when I come back 6 months from now, I might question myself: did I intend to default, or divide? The only way I can clarify this is via comments, which means I've now lost the benefit of a terse language.
Personally, I would rather my code be human readable.
Admin
The issue that comes into play here is twofold, however.
#1: In today's day and age, when you're doing something that stupid, if you're worth your salt, you'll stop, and think, "there must be a smarter way to do this", and ask google.
The first result in google is the correct one.
#2: This damned industry is filled with weenies who can't write good code to save their lives, but think that this industry is 'easy money' so they're here, filling our world with crappy unmaintainable code.
What's most unsettling about these types, is you can try and help and correct them, as I often have, only to be met with resistance as if you've challenged their manhood or something.
When I work with people smarter than me, I leech every last drop of knowledge I can from them, because it's where I've learned the most.
Admin
Admin
I prefer: TATDMWTDI There Are Too Damned Many Ways To Do It
Admin
It absolutely can, and it is one of the many things that makes Perl a curse word in the minds of some folks.
...of course, such thinking fails to address the simple matter that you can write bad code in any language, and thus, TDWTF was born.
Admin
Yup cuz we all know that beginners intuitively know to take the name of the programming language, tack on "func" and answer their questions. Heck, it's something that come sin real handy when doing anything else.
Wait you mean that there is no "cfunc", "pythonfunc", "bashfunc", "lispfunc", etc? Oh, well then maybe it is something only an experienced or trained perler would reactively know. (That or someone who spent time googling and searching so they could post it and appear to be knowledgeable.)
This problem isn't specific to Perl or any language, it's a hindsight or familiarity problem. But seriously I've not yet met anyone who not having used it a lot needed a Perl (Python, PHP, bash, ksh, scheme, lisp...) question related to functions and thought "I know, I'll try to combine the name of the language with the first 4 letters of the word function and look there!"