News for hackers. Stuff that matters.
Re:Oh My Glob by deckiedan on Tuesday November 19, 2013 @03:40AM (#6759933)
This is why I refuse to use perl (or SH or derivatives, or haskell, sadly) for system admin scripts:

A new admin comes along in 3 years time, and doesn't know the difference between , \, \, $?, $@, $&, $$, or gets confused between [] type ifs and [[ ]] ifs and 'test -' type ifs in BASH. Or doesn't know which variety of escape quotes are needed for which variety of strings.

Python has so much less "non-alphanumeric semi-random-combo" syntax.

Even though often a 3 line perl script would do the job, I prefer a 20 line python script that is easy to grok.

Re:Oh My Glob by DHowett on Tuesday November 19, 2013 @04:03AM (#6760008)
I understand what you're getting at, but...

    A new admin comes along in 3 years time, and doesn't know the difference(...)
...just makes it feel like you're optimizing for the lowest common intelligence.

It's not a mark on the language but rather one on the understanding of your potential successor. Would the same sysadmin who is unfamiliar with [[ vs [, or $ vs @, be handily equipped to do common system administration tasks? Rebuild a degraded software RAID array? Troubleshoot a faulty network interface? Chances are comparatively slim.

Re:Oh My Glob by noptic on Tuesday November 19, 2013 @05:24AM (#6760269)
Sorry but sounds awfully arrogant.

Lowest common intelligence? Sounds like anyone unfamillar with perl is stupid.

Perl is very different from other languages an has a very special syntax. The only way to REALLY understand perl is reading and writting lots of perl code from different authors.

In perl nearly every problem can be solved in many different ways.

If you are used to one style of perl you might still have trouble understanding the code someone else wrote.

Re:Oh My Glob by Mithaldu on Tuesday November 19, 2013 @05:35AM (#6760314)
> Lowest common intelligence? Sounds like anyone unfamillar with perl is stupid.

No, someone who uses things they don't understand without looking at the documentation is stupid. One could similarly make such comments about the user interfaces of vi or emacs, and get the justified response to RTFM. Only in programming languages is RTFM apparently not expected.

> The only way to REALLY understand perl is reading and writting lots of perl code from different authors.

No, the way to understand Perl is to read the documentation. There is nothing special to Perl about this and the same applies to any language. Just look for example at how many blog posts are written to explain with() and decorators in Python.

> In perl nearly every problem can be solved in many different ways.

The same is true for any language.

> If you are used to one style of perl you might still have trouble understanding the code someone else wrote.

Only if you don't read the documentation.

Re:Oh My Glob by GotAnyMegadeth on Tuesday November 19, 2013 @06:29AM (#6760469)
> someone who uses things they don't understand without looking at the documentation is stupid

I disagree, that's sometimes the best way to learn something. I learnt C, Java and Perl just by ploughing in without reading anything. Change something, see what happens, if it doesn't make sense ask Google.

Also programming languages aside, I never read computer game manuals before playing

Re:Oh My Glob by Mithaldu on Tuesday November 19, 2013 @06:36AM (#6760494)
If you'll allow me a bit of pedanty, that's learning and playing, not using. :)

I learned Perl the same way and play my games the same way, but when i write code that'll be used for anything that isn't frivolity, out comes the documentation to ensure the code doesn't destroy cash (or worse, lives) by the fistful.

Re:Oh My Glob by GotAnyMegadeth on Tuesday November 19, 2013 @09:10AM (#6761212)
Good point. If someone's life depended on it, I probably would read the Street Fighter manual, just in case
Re:Oh My Glob by lmm on Tuesday November 19, 2013 @05:02AM (#6760201)
I've met plenty of competent sysadmins who could definitely rebuild a degraded software RAID array or troubleshoot a faulty network interface, but didn't know perl.

I've watched people who didn't know python edit existing python scripts and make the change they wanted. To be fair, I've seen the same thing happen with well-written perl scripts - but only the kind that don't use [[ vs [ or the hundreds of random $* variables.

Re:Oh My Glob by Mithaldu on Tuesday November 19, 2013 @05:16AM (#6760240)
> [[ vs [

What does that even mean?

Thanks for the last bit though. Perl has been in heavy use since the late 80s and a LOT of shitty legacy Perl has been written, while the Modern Perl renaissance only started in 2005. Many people still think all Perl looks like legacy Perl 4 style code and it's an image that's been hard to get rid of.

Re:Oh My Glob by lmm on Tuesday November 19, 2013 @08:17AM (#6760926)
I think many perl advocates try to have their cake and eat it - they talk about the power and conciseness that perl's short operators allow, and then also claim that you can write maintainable code in perl by using english function names, strict mode and so on. Both claims are mostly true, but you can't do both in the same piece of code.
Re:Oh My Glob by anon1385 on Tuesday November 19, 2013 @07:44AM (#6760741)
>it's an image that's been hard to get rid of.

Maybe because Perl 6 adds even more obscure and magical operators?

http://www.ozonehouse.com/mark/periodic/

Re:Oh My Glob by Mithaldu on Tuesday November 19, 2013 @07:47AM (#6760756)
Perl 6 has nothing whatsoever to do with Perl 5 v18. Perl 6 cannot even execute code written for Perl 5 v18 (or any previous versions of Perl 5). It's a different language.

However, people don't care to look and judge books by their cover, so you're probably right that it's a part of the reason.

Re:Oh My Glob by draegtun on Tuesday November 19, 2013 @03:47PM (#6764340)
I don't see anything magical in there? Looks like a good set of useful/powerful operators to me.
Re:Oh My Glob by poolpool on Tuesday November 19, 2013 @06:17AM (#6760434)
I'm glad you're so impressed with your knowledge of network and computer janitoring you can look down on others so easily.

You are the reason why people hate dealing with IT.

Re:Oh My Glob by kamaal on Tuesday November 19, 2013 @03:48AM (#6759954)
>>Even though often a 3 line perl script would do the job, I prefer a 20 line python script that is easy to grok.

Talking about a 3 line Perl script doesn't make a nice comparison. Multiply those numbers by a minimum of 1000.

Now you have a 3000 line Perl script Vs 60000 line Python program.

Suddenly you go from one person writing a Perl script,to 10 people writing the same in Python.

I've written > 3000 line Perl scripts quite a few times.

Re:Oh My Glob by deckiedan on Tuesday November 19, 2013 @03:54AM (#6759970)
Good thought - the thing is, once it's over a certain size, I don't think that it continues with the same ratio.

Once it's beyond the difference between a one-line regexp/fork-to-shell/reverese combo type thing that perl is so great at, most of the code becomes your actual business logic, and logic code I feel ends up being similar lengths in perl, ruby, python, and other similar languages.

Python has more boilerplate to get at regexps, or calling external programs and doing stuff with the output, say - but it's explicit. Perl has a lot more built in, and hidden behind non-alpha syntax.

Both are great languages, and both have their place. I much prefer python, but also find perl a very versatile and useful tool.

Re:Oh My Glob by kamaal on Tuesday November 19, 2013 @04:01AM (#6760006)
>>Once it's beyond the difference between a one-line regexp/fork-to-shell/reverese combo type thing that perl is so great at

That's just the very start, the very beginning of what Perl is good at. Read this book called "Higher Order Perl" by Mark Jason Dominus. You will see why 60000 line Python program actually becomes a 3000 line Perl program.

>>most of the code becomes your actual business logic, and logic code I feel ends up being similar lengths

No,

You must take a serious look at Perl. Please read the book I mentioned.

Some languages do make it very easy to write short, powerful, clear succinct code.

More importantly you need to work in situations where Perl can show its magic. I've written scripts in a week alone which were easily 6 month Java projects.

Re:Oh My Glob by gjm11 on Tuesday November 19, 2013 @04:39AM (#6760138)
"Higher Order Perl" is a very good book. I do not believe that any substantial fraction of the things it shows would take anywhere near 20x as many lines of code in Python as in Perl; certainly none of the examples I just looked at would.

Could you please (1) give a couple of concrete examples that don't involve regular expressions (yes, Perl is very good with hairy REs; no, very few 3000 line programs consist mostly of REs) and (2) indicate your level of expertise in Python?

Thanks!

Re:Oh My Glob by Mithaldu on Tuesday November 19, 2013 @05:14AM (#6760231)
Not a 20x example, but file handling is one that stems from the implementation of Python's garbage collection.

Python does not guarantee garbage collection at scope end. Thus you need code like this:

    def controlled_execution(callback):
        set things up
        try:
            do something
        finally:
            tear things down
Or alternatively, slightly obtuse incantations like this:

    class controlled_execution:
        def __enter__(self):
            set things up
            return thing
        def __exit__(self, type, value, traceback):
            tear things down

    with controlled_execution() as thing:
         some code
Perl makes this much easier by guaranteeing garbage collection of objects that have left scope, even if the scope is left via die() and caught with eval(). If an object leaves scope, its DESTROY method is called immediately, and after that the object is dismantled. Thus objects can carry their teardown around in their class definition (which can be easily modified by overriding or wrapping the method in a new class), and user's need to do nothing whatsoever to ensure the teardown being executed.

Thus, file opening and using code for example can look as simple as this and contain zero gotchas and no "you need to understand this to actually know what it does":

    sub do_a_thing {
        my $file = io->file(".bashrc");
        print $file->slurp;
    }
Re:Oh My Glob by lloeki on Tuesday November 19, 2013 @05:39AM (#6760319)
Honest questions since my perl-fu is weak.

Since python's file class already implements the with protocol, I don't see how

    sub do_a_thing {
        my $file = io->file(".bashrc");
        print $file->slurp;
    }
is different from

    def do_a_thing:
        with open(".bashrc") as f:
            print(f.read())
Conversely I don't see how

> Thus objects can carry their teardown around in their class definition (which can be easily modified by overriding or wrapping the method in a new class)

is any different from implementing the with protocol in the class, which is strictly speaking "carry their teardown around in their class definition"

Re:Oh My Glob by Mithaldu on Tuesday November 19, 2013 @05:54AM (#6760360)
You answered yourself. What about classes that don't implement the with protocol? In Perl an object is guaranteed to be garbage collected even if it doesn't have a DESTROY method and without requiring any special syntax.
Re:Oh My Glob by lucian1900 on Tuesday November 19, 2013 @07:44AM (#6760744)
All Python objects do get destructed at the end of the scope. The only exception are reference cycles, which get broken by a simple GC. Perl doesn't even have a cycle detector, so cyclic references will never get destructed.

Python has a __del__ method that will be called on destruction, but is not guaranteed to be called in the case of cycles. That's what the with statement solves, guaranteeing destruction at the end of the block. It's common to have both __del__ and __exit__ call the same thing.

Re:Oh My Glob by Mithaldu on Tuesday November 19, 2013 @11:21AM (#6762185)
They get destructed AFTER end of scope, not AT end of scope, regardless of the existence of cycles. See:

http://eli.thegreenplace.net/2009/06/12/safely-using-destruc...

Re:Oh My Glob by deckiedan on Tuesday November 19, 2013 @02:50PM (#6763907)
Thanks for the heads up about HOP, I'll certainly try to read it. It's been a few years since I did any serious perl. :-)
Re:Oh My Glob by Mithaldu on Tuesday November 19, 2013 @04:01AM (#6760004)
Due to Perl lending itself to easily creating DSLs, i often find it to be much more concise even with bigger programs, when porting from python to Perl. (And then i end up making the difference in line count by actually writing documentation.)
Re:Oh My Glob by Mithaldu on Tuesday November 19, 2013 @03:59AM (#6759992)
The article doesn't even use magic variables, so what the hell are you on about?

Also, even with largely text-based programming you can run into obscure issues very easily. See for example:

    if a in b.keys()
    vs.
    if a in b
Re:Oh My Glob by camus2 on Tuesday November 19, 2013 @05:53AM (#6760355)
what about ruby ? i find it much more closer to a shell script or perl than python , i think it's an good use case for admin scripts.