12 ms·
Whenever I need to write a tiny script that tests the bounds of what is easily done with just awk and Bourne shell, the tool that I next reach for nowadays is P
by nickysielicki 8y ago
Whenever I need to write a tiny script that tests the bounds of what is easily done with just awk and Bourne shell, the tool that I next reach for nowadays is Python. A few years back it was Perl5.
I'm not exactly sure why I started reaching for Python instead. It's so much easier with perl to do things like backticks to call some utility, or to use system() and easily deal with its stdout and stdin, and I never have to do a Google search to figure out how to use a regular expression to process the output (whereas I almost always need to bring up the doc page for Python's re).
I think what it ultimately comes down to is tooling and the availability of good libraries. I call "python3 -m venv venv", do an I'm Feeling Lucky google search for "XXX library inurl:pypi", and the sky is the limit with how easily I can make a script that does something relatively complicated. Comparing this with perl5 isn't even fair. You might find a cpan module that does what you want, and it might still be maintained, and you maybe can be bothered to figure out cpanminus, but anyone who has used perl in the last 5 years will agree, it's nowhere near as easy as it is with python.
Something that makes me optimistic about newer languages is that tooling is improving a great deal. It also makes me somewhat pessimistic about newer languages, though, if tooling is the primary aspect of a language that makes it popular.
I've never seriously tried perl6 but I've seen enough of the language features to know that I was really excited about it, at least at one point. I hope that some derivative of perl can rise from the ashes, because I'm too stupid to write actual lisp programs, but perl allows me to emulate what I imagine lisp programmers must feel.
- crusso 8y agoI'm exactly the same way about Perl vs Python. After years of using Perl 5 and due to what I also think is the simplicity of the language, I know I could do most small tasks more easily in Perl. But Python feels more like "the future". When I need to write a script-like solution, I reach for Python because I need to practice with it and because I figure that the modules will be better maintained. You're right, though. I always need to look up simple things like system calls and often have to remind myself of exactly how the re module works. But I slog through it. Don't get me started on the Python 2 -> 3 glacial migration, though. That almost makes me want to switch back to writing my scripts in Ruby.
- fouc 8y agoIn response to your last sentence. Just wanted to mention that Ruby was highly inspired by Perl, particularly its regular expressions and it has a variety of perlisms. Kind of surprised you went to python and not ruby. Your lisp mention reminds me of this: http://www.randomhacks.net/2005/12/03/why-ruby-is-an-acceptable-lisp/ http://www.randomhacks.net/2005/12/03/why-ruby-is-an-accepta...
- cortesoft 8y agoOh man, you responded the same as me at the same time.
- mst 8y agoI just wish ruby had the same quality of scoping and OO that perl5 has, but it ended up with the same "variables magically pop into existence for the entire function" footgun as python and a horribly restrictive OO that also requires me to repeat myself (accessor/constructor etc.) in a way that after about an hour sends me screaming back to perl Edit: Look, this is a genuine opinion, I've chatted to ruby core devs about this and they couldn't find ways to achieve the same things I do with perl5 as elegantly in ruby (Steve Klabnik and I have an open conversation about how to find a kata where we can compare this stuff properly), I'm 100% happy to discuss this but blindly downvoting because you disagree is depressing - tell me why you think I'm wrong instead and I'll do my best to reply constructively
- fouc 8y agoNot sure who downvoted you, sounds like you have good points. Do you have some links/examples on that scoping & how you can avoid accessor/constructor in perl?
- mst 8y agoScoping - 'my' is lexical/block scoping, same as javascript's 'let' (thank you! steal more things please!) so use feature 'say'; use strict; use warnings; my $code = do { my $count = 0; sub { ++$count } }; say $code->(); # 1 say $code->(); # 2 and note that if I e.g. typoed $count inside the subroutine I'd get a compile time error (and it goes out of scope at the end of the 'do { ... }' block). Note that the 'state' keyword provides an easier way to do this, but it's the easiest demo of closures I can think of, though also there's fun to be had with methods: my $method = sub ($self, @args) { $self->foo(3, @args) }; $object->$method(1, 2); # same as $object->foo(3, 1, 2); Constructor/accessor wise: package MyClass; use Moo; has foo => (is => 'ro', required => 1); has bar => (is => 'rw'); has baz => (is => 'lazy', builder => sub { expensive_default() }); then means that MyClass->new will throw an exception telling you foo is required MyClass->new(foo => 1); will work fine, $obj->foo; # returns the value of foo $obj->foo(3); # throws an exception - foo is ro $obj->bar; # returns the value of bar is any $obj->bar(26); # sets bar and (if you didn't pass a value for it to the constructor) $obj->baz; # runs expensive_default() once and then retains the value Hopefully that's a start, this is all typed off the top of my head on my first post-9am pint of coffee so my apologies for any omissions and/or errors.
- cortesoft 8y agoRuby has all of those perl features you mention, without a lot of the strange quirks from perl.
- cutety 8y ago> without a lot of the strange quirks of perl And instead with a lot of the strange quirks of Ruby! \s, only kind of, I love Ruby but it does have a few quirks that I see confuse some people coming from other languages when they start writing non-trivial Ruby code (literally everything is an object being a big one — “what do you mean a class is an object?! modules too?!”). However, these quirks are much, much easier to learn, understand, and possibly eventually come to like, rather than some (many) of the quirks that exist in Perl.
- chrisseaton 8y ago> what do you mean a class is an object I think that's pretty standard for object orientated languages. I can't find of any where it isn't the case off the top of my head. Is it not the case in Perl's object orientated support? And why wouldn't it be an object? Seems more surprising for classes to be an exception.
- Fellshard 8y agoPeople coming from Javaville or C#land definitely don't have that kind of abstraction (and yes, the usual rebuttal is that they're procedural, not OO).
- chrisseaton 8y ago> what do you mean a class is an object > People coming from Javaville or C#land definitely don't have that kind of abstraction What do you mean? A class is an object in both Java and C#.
- Fellshard 8y ago
- snazz 8y ago> because I'm too stupid to write actual lisp programs Even PG admits that Lisp (in particular CL) is an "atrocious"[0] scripting language for Unix, in part because the text-processing applications that you speak of aren't its forte. It's not that you're "too stupid", it's that it's harder than it needs to be to write scripts that do useful things on filesystems and text files using Lisp in a way that is compatible with multiple CL implementations. You'd still be Googling plenty and Lisp sure doesn't have the wealth of libraries that Python or Perl does. On the other hand, I'd recommend you learn some form of lisp anyway for the aha moment when you implement a domain specific language with macros and scratches some itch of yours. Try Ruby, too, for the lispiest of those three scripting languages. [0]: http://www.paulgraham.com/popular.html http://www.paulgraham.com/popular.html
- kazinator 8y agoThat may be true in CL, but there is a scripting dialect of Lisp called TXR Lisp. It's documented in one big manual page (just like GNU Awk, Bash, and other classic programs). If you can't find what you're looking for there, either it doesn't exist, or you need help from StackOverflow or the TXR Mailing list. TXR has decent capabilities for text processing, plenty of POSIX functions built right in, and such. There is a decent port of TXR to Windows, which uses a fork of the Cygwin DLL called Cygnal. Cygnal provides more Windows-like behaviors in areas where stock Cygwin is too assertive with POSIX conventions in ways that make Cygwin programs confusing to Windows users.
- petre 8y agoHow about Guile? I've only seen it used as an embedded language (in C) or using the repl.
- wvenable 8y agoI have two experiences telling with Perl and Python. Way back in the day, I had job doing web development in Perl. I had no say in the matter, it was the only choice available. And I actually used Perl enough to actually like it! I mean I understand why people who like Perl really like Perl. But just a few years after that and I could hardly even read Perl code let alone try and code in it again. Python was is opposite; I never coded in Python but I ported a Python product SDK from Linux to Windows in a few weeks. I could read it. I could write it. I made more mistakes with indenting than anything else. Even to this day, I don't know Python but I can muddle my way through and be reasonably productive.
- bsder 8y ago> But just a few years after that and I could hardly even read Perl code let alone try and code in it again. Precisely this. The Perl Cookbook (and Perl Book cookbook section) was an absolute godsend in the time before the web. However, the fact that my Perl Book and Perl Cookbook fell apart from so much usage shows how the language never really stuck in my brain in spite of continuous low-level usage.
- naikrovek 8y agoI lost my copies of those books because I rarely needed them. Once I wrote it once, it stuck in my mind. Everyone has a language or two that really "clicks" with them. For me, one of them is Perl. Python, on the other hand, is one of those languages that won't ever stick in my mind. I don't know why. I just can't do Python. I picked up Golang in about a day. I've tried Python about 4 times over about as many years and it has gone from something I wanted to learn to something I wish never existed.
- biomcgary 8y agoI had the same experience. Perl clicked for me, but not Python. A few years ago, I switched to Go for performance and it has clicked as well. Go is a rather sparse language relative to Perl (and TIMTOWTDI), so I'm a bit surprised that it was a natural switch.
- v_lisivka 8y agoI just use perl5. Python is too hard to use for perl tasks. Separate venv for a script is horrible overkill. I also use Fedora Linux, where everything is integrated and just works.
- zbentley 8y ago> Separate venv for a script is horrible overkill ...and god help you when you write code/use a library that depends on language features not available by default, without pragmas, on the system Perl (5).
- petre 8y agoThere is local lib and if you don't use RHEL or some other OS with ancient Perl it works just fine. Or just install what you need from the OS if you have root. Or use plenv and cpanm and whichever Perl version you want to create a local environment.
- zbentley 8y ago> Or just install what you need from the OS if you have root. The availability of libraries is not the problem. The availability of language features is. Manually replacing the OS version of a ubiquitously-depended-on language as root seems like a very poor idea. The last time I tried that with Perl, I broke GNU Parallel (whose Perl code is a fascinating read, by the way!). > Or use plenv and cpanm and whichever Perl version you want to create a local environment. I agree; that's the right approach (or Perlbrew or any other portable-Perl installer). GP was responding to someone who argued against using venv, which is the equivalent of plenv.
- petre 8y agoI was arguing about installing Perl modules from the distribution's repository as root, not replacing the system interpreter. The other solution is system wide module install with cpanm. Perl5 has plugin "language features" available via modules such as Moo, Role::Tiny, MOP, autobox, Clone etc.
- marcosdumay 8y ago> I'm not exactly sure why I started reaching for Python instead. Oh, I am quite sure. Looks like Perl6 changed that, but it's because I was never sure that data structure should be a reference to a dictionary of references to scalars, or a dictionary of scalars, or a dictionary of reference to scalars, or a reference to a dictionary of scalars. Anyway, next time I go for a quick script, I should go for Haskell, not Python, nor Perl (5 or 6, whatever). But I will probably go for Python, because it's painless enough and I don't know Haskell's shell integration by hearth. Anyway I will go for Haskell before I try Perl6, what is a nice measure of the problem they have to get mainstream again, because the language varies, but I'm far from the only person thinking this way.
- ethagnawl 8y agoIf it's not already on your radar, you should have a look at the Shelly library on Hackage.
- marcosdumay 8y agoOh, I know about it. And it looks great. It's basically the reason the next language I'll try on the shell is Haskell. But it takes looking at the documentation, so I'll leave it to try when I'm not in a rush. For shell scripting, that may take a long time.
- AnimalMuppet 8y agoThe kind of quick scripts I use Perl for involve picking interesting bits out of text files. Honest question: Would you use Haskell for that? If so, why? And how?
- 3rdAccount 8y agoI'm with you here. It's not that Haskell isn't a good language, but that scripting languages are really good at scripting :) Haskell does have a good REPL and being able to distribute a binary when finished is really nice.
- tzs 8y ago> I'm not exactly sure why I started reaching for Python instead. It's so much easier with perl to do things like backticks to call some utility, or to use system() and easily deal with its stdout and stdin, and I never have to do a Google search to figure out how to use a regular expression to process the output (whereas I almost always need to bring up the doc page for Python's re). The thing I find easier in Perl than in the other languages I might reach for when doing a lot of quick scripting tasks is declaring nested data structures. It's easy in Perl because often you don't have to declare them--you just use them. And that means that you can often make major changes to how your data structures work with just small source changes. For tasks where you need to process data from logs or text files or similar, but don't actually quite know what it is you need to be doing with the data so need a good bit of exploring/flailing to get your bearings, and then maybe a few iterations trying different approaches to figure out what structures you need, and you don't have a lot of time to think about it up front--in other words, data analysis improvisation, Perl shines.
- collyw 8y agoSame here. I do mainly Python development - Django docs made things way easier then the Perl frameworks that I tried. But for quick and dirty scripting tasks I still pull out Perl as the extra sysntax makes thing so much easier - backticks and built in regular expressions for example. And I have still never seen anything as intuitive to use as a Perl hash tied to a berkley db file for simple persistence to disk.
- sciurus 8y agoI agree that Perl's autovivification is a timesaver, although I can usually hack together what I want with Python's defaultdict. For "data analysis improvisation" though, I don't know. A good REPL is a key part of that, and I'm not sure that Perl has anything as good as the ipython cli or jupyter notebook.
- kazinator 8y agoWhenever I need to write a tiny script that tests the bounds of what is easily done with just awk and Bourne shell, the tool that I next reach for nowadays is TXR or TXR Lisp. I made it myself. One of the conscious goals of TXR is to have a scripting language in the POSIX environment (without forgetting about MS Windows too) that is acceptable for people who would rather be coding in Lisp. (Speaking of awk: TXR has it: it's a Lisp macro.) TXR saves coders from Python, Perl, Ruby, Tcl, Lua and whatever else. TXR has no ecosystem or libraries; everything comes with the program. If it's not described in the manual, it doesn't exist; stop looking and write it yourself. This kind of thing is forbidden in the TXR project: https://xkcd.com/1987/ https://xkcd.com/1987/
- foobarian 8y agoI lean to Python when writing anything more than a few lines that I can see extending, but Perl (perl -pe '<code here>') still rules for command line pipes where I need a bit more power than what the standard Unix utilities provide.
- MichaelMoser123 8y agoPerl is no longer my first choice for scripting. Once upon a time perl was preinstalled on almost every Linux box and was a default package on most distributions, now you can't assume that. Also it is a bit difficult to use cpan - often the c code for a module does not compile for some reason during installation and you have to go and figure; on the other hand I never had any problems with pip. (Though I still don't quite like Python) On the other hand bash has maps so it is possible to use that more for scripting.
- jimpudar 8y agoWhat Linux distros don't include Perl by default? I certainly haven't come across any...
- MichaelMoser123 8y agoWell I did not do a lot of research for this post, but many installations that I have to deal with come from some image where perl is not been installed.
- collyw 8y agoBash has a horrible syntax. Beyond a simple for loop I would reach for Perl.
- CydeWeys 8y agoShell scripts have so many footguns. Python is almost as terse, takes less time to write (for me anyway), and is way more maintainable.
- MichaelMoser123 8y agoBut it has set -x ; really helps to see what the script is doing step by step. I don't know how to do that easily with other languages.
- bmn__ 8y ago
- zebraman 8y ago> I hope that some derivative of perl can rise from the ashes, because I'm too stupid to write actual lisp programs, but perl allows me to emulate what I imagine lisp programmers must feel. Well, there is Clojure. You can script it with `clj` and https://github.com/l3nz/cli-matic https://github.com/l3nz/cli-matic, and/or one of the ClojureScript interpreters. It has sane library management, so there is no PITA installing anything. You have JVM libraries, so you have all you need. We find it pretty handy.
- audunw 8y agoFor me, also with quite a bit of experience with Perl and Python, the main reason to use Python over Perl is readability. Especially when it comes to code written or modified by less experience engineers. Programmers tend to think that writability is the most important when it comes to coding, while readability is actually far more important. I agree that using Python's re module is harder to write for than regexps in Perl or Ruby. But honestly it's not that much harder, and usually using it leads to clearer, more explicit and more readable code.