News for hackers. Stuff that matters.
Re:Why does OpenBSD still include Perl in its base installat... by qalmakka on Saturday May 30, 2020 @07:51AM (#23360478)
Perl 5 is in my humble opinion a really convenient to have installed. I use it all the time as a replacement for those shell scripts that become too big or too cumbersome, or to remove out of the equation the quirkiness of different implementations of standard tools (i.e. GNU sed has certain flags that BSD does not have, gawk is different from mawk, etc). A Perl onliner is often faster and works the same way pretty much everywhere.
Re:Why does OpenBSD still include Perl in its base installat... by inglor on Saturday May 30, 2020 @08:16AM (#23360614)
That's how I use Python for the last few years.

My scripts end up longer and more verbose typically (I might just not be great at Python) but also a lot more maintainable (I am not great at writing maintainable Perl and I use Python more often).

I also use Node.js for this a lot because of the ecosystem containing so many accessible scripts and dependency management while not great is heaps better than most other ecosystems.

Of course OpenBSD can't do that with Python rather then Perl (as explained in that email). They have a different set of constraints. I am only saying in practice I have found Python to be the better alternative for my personal stuff.

Re:Why does OpenBSD still include Perl in its base installat... by rwmj on Saturday May 30, 2020 @10:11AM (#23361299)
Python's subprocess is a bit more clumsy than Perl's built in features for running commands, with the proviso that running commands in Perl is best described as "idiomatic". Maybe if you didn't learn it 20 years ago like I did you'd have a hard time with its irregularity and colloquialisms.

Is there any non-shell scripting language that makes it as easy as shell to run external commands, redirect I/O etc.? I don't know of any. (Perhaps that is the very definition of a shell.)

Re:Why does OpenBSD still include Perl in its base installat... by lmm on Saturday May 30, 2020 @10:54AM (#23361602)
TCL ships with tclsh which is regular TCL (so a first-class programming language) but allows you to invoke external commands as though they were functions (so arguably is a shell in your sense, though I wouldn't advise using it as your login shell).
Re:Why does OpenBSD still include Perl in its base installat... by jasonhansel on Saturday May 30, 2020 @10:31AM (#23361438)
Agreed. I really want a language that makes it easy to run subprocesses and do other Unix/shell magic, while being more straightforward and modern than Perl.

I think a good way of doing this would be to make a shell that has more expressive power and a clearer syntax.

Re:Why does OpenBSD still include Perl in its base installat... by jhgb on Saturday May 30, 2020 @01:34PM (#23362937)
> Is there any non-shell scripting language that makes it as easy as shell to run external commands, redirect I/O etc.?

Would Scsh count?

Re:Why does OpenBSD still include Perl in its base installat... by Tyr42 on Saturday May 30, 2020 @10:49AM (#23361576)
Ruby uses the same sorta commands as perl does I think.
Re:Why does OpenBSD still include Perl in its base installat... by ma2rten on Saturday May 30, 2020 @10:52AM (#23361591)
This is one of the few things really missing from python. I am wondering how easy it would be to write a PEP for an outsider.
Re:Why does OpenBSD still include Perl in its base installat... by lmns on Saturday May 30, 2020 @11:44AM (#23361914)
subprocess.run() isn't too bad.
Re:Why does OpenBSD still include Perl in its base installat... by andrewl-hn on Saturday May 30, 2020 @10:03AM (#23361250)
I've tried to use Python for this kind of scripting, too, but went back to Perl. Python-the-interpreter is annoying. Is it /usr/bin/python or /usr/bin/python2 or maybe even 3? Does this server image / distro even come with Python? Sure, Python-the-language is nice and all, but when we talk about 1-10 lines of code it's not a big deal, and Perl... It's just there. Even on Windows Git Bash and mingw come with Perl.
Re:Why does OpenBSD still include Perl in its base installat... by dazzawazza on Saturday May 30, 2020 @09:39AM (#23361108)
Perl is a write only language for me.

I've got 20 year old python scripts the have been upgraded over the years that are still maintainable, easy to read and do the job just fine.

Re:Why does OpenBSD still include Perl in its base installat... by tzs on Saturday May 30, 2020 @11:30AM (#23361820)
I can write code that is easy to maintain even after not seeing it for decades, and I can write code that I have trouble maintaining after a long weekend away from it, and I can do this in nearly any language I know.

Take good old fizzbuzz. Want to write it clear and simple in Perl? No problem:

  for (my $n = 1; $n <= 100; ++$n) {
    if ($n % 15 == 0) { print "fizzbuzz\n"; }
    elsif ($n % 3 == 0) { print "fizz\n"; }
    elsif ($n % 5 == 0) { print "buzz\n"; }
    else { print "$n\n" };
  }
Want to get clever and complex in Python? Here you go:

  def fizzbuzz(n, *args):
    cur = ['' for x in range(1,n+1)]
    for m, postfix in args:
      cur = [y+postfix if x%m==0 else y for x, y in zip(range(1,n+1), cur)]
    cur = [str(x) if y == '' else y for x, y in zip(range(1,n+1), cur)]
    return cur

  print("\n".join(fizzbuzz(100, (3, 'fizz'), (5, 'buzz'))))
Of course you could do an unclear and overly clever Perl version, too, or a clear and simple Python version.
Re:Why does OpenBSD still include Perl in its base installat... by d2alphame on Saturday May 30, 2020 @07:35PM (#23365815)
I would have written that Perl code as

    for (1..100) {
        if    (not $_ % 15) { say 'FizzBuzz' }
        elsif (not $_ % 3)  { say 'Fizz'     }
        elsif (not $_ % 5)  { say 'Buzz'     }
        else                { say            }
    }
Just saying
Re:Why does OpenBSD still include Perl in its base installat... by ulisesrmzroche on Saturday May 30, 2020 @11:49AM (#23361956)
The point is that python leads you to clear and simple. No one would write the monstrosity you did, not even by accident, while in Perl, even that little fizzbuzz looks noisy cus of the explicit new lines
Re:Why does OpenBSD still include Perl in its base installat... by DFHippie on Saturday May 30, 2020 @01:26PM (#23362848)
I haven't used Perl recently, but the last time I did there was a say function, so

    say "foo";
was equivalent to

    print "foo", "\n";
Re:Why does OpenBSD still include Perl in its base installat... by ulisesrmzroche on Sunday May 31, 2020 @02:00PM (#23371291)
My point exactly. Python begginners would have trouble recognizing the second option, which is good, because that’s the bad option: “Omit needless words”

Perl obviously even lets experts shoot themselves in the foot on the simplest code, see the fizzbuzz above me, While a beginner would never write the complex python version that was shown, not even by accident.

Re:Why does OpenBSD still include Perl in its base installat... by kbenson on Monday June 01, 2020 @01:40PM (#23381288)
Optimizing for beginners is fine, but let's not act like it's "correct". It's a Python design choice, some people like it, but there are consequences to it. Like almost all language design choices, it's a trade off. Usually the trade-off for simple to learn is that it's restrictive to professionals and experts. For new users, teaching classes, and bringing amateurs onto an existing team, it works well. For a team where everyone has 5+ years of experience in Python, I imagine there will be some annoyances where they fell boxes in by the language. That's the trade off in action.

An example of a langauge at the extreme other end of the spectrum would probably be APL. It's indecipherable gibberish to me every time I look at it, but I'm not even an APL amateur, much less professional, and that language seems built to be used by people that are very familiar with it. Is that wrong? No, they just aren't optimizing for learnability and community growth, as Python does, instead opting to make it as simple and useful as possible for experts to get what they need accomplished.

You can counter by saying that most code is read more than it's written, so readability is more important, and to that I would say that it depends on the type of code (scripting languages lend themselves to a lot of one-off scripts), and that readability is also a function of experience. If you want your code easily readable my amateurs, then keeping the capabilities you can use to those an amateur can understand is beneficial. If you want your code readable and understandable by experienced programmers, use constructs and control flows that they are experienced with, which is a much wider set of capabilities usually (if the language supports them).

Re:Why does OpenBSD still include Perl in its base installat... by d2alphame on Monday June 01, 2020 @07:53PM (#23385529)
How is that version of FizzBuzz shooting oneself in the foot?
Re:Why does OpenBSD still include Perl in its base installat... by eythian on Saturday May 30, 2020 @04:03PM (#23364166)
Even that Perl isn't as tidy as it could be. I'd be inclined to have `for my $n (1..100)`, and use `say` instead of print. I might even skip the `== 0` but just saying `(! $n % 15)`, but I'm on the fence about that.

I also wouldn't keep all the blocks on the same line, but it can be forgiven in a comment box.

Re:Why does OpenBSD still include Perl in its base installat... by tzs on Saturday May 30, 2020 @09:01PM (#23366284)
> I'd be inclined to have `for my $n (1..100)`

I actually had that at first...

> I might even skip the `== 0` but just saying `(! $n % 15)`

...and that too. I changed them to be more likely to be obvious to even someone who has never seen Perl before. A lot of languages besides Perl have a "for" loop variant that is like a C "for" loop.

The formatting was indeed because it was in a comment box. I didn't want to take up too much vertical space on the page.

Re:Why does OpenBSD still include Perl in its base installat... by dcassett on Saturday May 30, 2020 @09:51AM (#23361182)
When I read the perldocs much of it looks write-only. But in keeping with Perl's spirit of "there's more than one way to do it", one can actually write straightforward Perl code that is as understandable as Python.
Re:Why does OpenBSD still include Perl in its base installat... by tyingq on Saturday May 30, 2020 @10:01AM (#23361245)
I'm a Perl fan, but there are some places where it's hard to avoid ugly code. Like collections of complex data structures, especially combined with references. No matter how hard you try, you get messy looking code.
Re:Why does OpenBSD still include Perl in its base installat... by dorfsmay on Saturday May 30, 2020 @10:20AM (#23361363)
This is exactly when I switched to Python, when I realized that I always had to look up how to dereference pointers to non-trivial objects.
Re:Why does OpenBSD still include Perl in its base installat... by eythian on Saturday May 30, 2020 @04:05PM (#23364186)
For what it's worth, there's now postfix dereferencing which avoids the whole having to go back and wrap @{ ... } around things.
Re:Why does OpenBSD still include Perl in its base installat... by b2gills on Wednesday June 03, 2020 @02:32PM (#23406965)
Perl is a write once language for me.

In that I write it once and 20 years later it still works even though I upgraded the compiler many times.

Re:Why does OpenBSD still include Perl in its base installat... by flyinghamster on Saturday May 30, 2020 @08:08AM (#23360563)
That's exactly how I treat Perl as well. Things like lists and hashes make it easy to do things that are difficult or impossible in bash.

If my bash script gets much beyond simple looping, I switch to Perl.

Re:Why does OpenBSD still include Perl in its base installat... by benbristow on Saturday May 30, 2020 @08:38AM (#23360737)
Why Perl over Python though? Most Unix distros come with Python bundled and Python has a bigger ecosystem of libraries etc available. And it's generally easier to read/write.
Re:Why does OpenBSD still include Perl in its base installat... by mfontani on Saturday May 30, 2020 @08:51AM (#23360807)
/usr/bin/perl scripts written more than twenty years ago continue to work.

So do /bin/sh and /bin/bash scripts.

/usr/bin/python scripts IME depend on what /usr/bin/python points to; some break/broke due to the python 2 to python 3 switching, which hasn't been as backwards-compatible as previous examples have.

Re:Why does OpenBSD still include Perl in its base installat... by dorfsmay on Saturday May 30, 2020 @10:21AM (#23361371)
But! If you write modern bash, it won't run everywhere, definitely won't run on mac!
Re:Why does OpenBSD still include Perl in its base installat... by xyproto on Saturday May 30, 2020 @10:35AM (#23361468)
Bash may be installed in /usr/bin (Arch Linux + symlink in /bin), /usr/local/bin (OpenBSD) or not at all, though.

"#!/usr/bin/env bash" or "#!/usr/bin/env python" are the recommended ways to run both, AFAIK.

Re:Why does OpenBSD still include Perl in its base installat... by asveikau on Saturday May 30, 2020 @11:41AM (#23361891)
I have some FreeBSD and OpenBSD machines. /usr/bin/env python doesn't work on either of them because the ports call it python3.7, python2.7, python2, etc. and there is no symlink simply called 'python'.

Sometimes I used to create such a symlink myself but I don't use or run a lot of python day to day so my current systems do not have this.

Re:Why does OpenBSD still include Perl in its base installat... by mekster on Saturday May 30, 2020 @11:23AM (#23361773)
Where is bash not installed?
Re:Why does OpenBSD still include Perl in its base installat... by asveikau on Saturday May 30, 2020 @11:43AM (#23361908)
Bash is not installed by default on any *BSD I have used (FreeBSD, OpenBSD, NetBSD, etc.). You need to install it from the ports tree or package repo.

Most (all?) commercial Unix I have used also don't have bash, but those systems are pretty dead these days.

OpenBSD's fork of ksh is actually really good so I typically feel no reason to ever install bash on there. On others, the default shell kinda sucks and I tend to install bash. In particular the old /bin/sh on older Unix tends to be worse at interactive use and not support some constructs like {a,b}

Re:Why does OpenBSD still include Perl in its base installat... by freemint on Saturday May 30, 2020 @02:43PM (#23363520)
Except Ubuntu where they ship dash by default which is not really bash compatible.
Re:Why does OpenBSD still include Perl in its base installat... by asveikau on Saturday May 30, 2020 @03:55PM (#23364085)
I think you might misunderstand me with what "bash is not installed by default" means. On the systems I'm mentioning bash is not on disk anywhere in the base install and you have to install it extra. I don't think that is the case in Ubuntu.

I'm typing this on FreeBSD. It's in /usr/local because it came from ports.

    $ which bash
    /usr/local/bin/bash
Now I try my OpenBSD machine. It's not installed at all.

    $ which bash
    which: bash: Command not found.
Re:Why does OpenBSD still include Perl in its base installat... by throw0101a on Sunday May 31, 2020 @06:08PM (#23373239)
> Except Ubuntu where they ship dash by default which is not really bash compatible.

Why would you expect it to be?

dash is a POSIX shell:

* https://en.wikipedia.org/wiki/Almquist_shell#dash

Even when called as /bin/sh, which implies Bourne/POSIX behaviour, bash leaks all kinds of functionality into the environment such that if you tru to run your "/bin/sh" script on another shell it may not work.

We had all sorts of issues when Debian made the switch: we had to tell people either (a) fix the code, or (b) change your shebang to bash.

If you want Bash behaviour call bash. It's like when I type in "vi" in the command line and I get an editor with all sorts of colours: WTF? If I wanted Vim I would have typed in "vim", but for editing a small file all I need is POSIX vi without the dog and pony display show. /rant

Re:Why does OpenBSD still include Perl in its base installat... by thesuperbigfrog on Saturday May 30, 2020 @08:49AM (#23360797)
>> Python has a bigger ecosystem of libraries etc available.

CPAN (http://www.cpan.org/index.html) is quite large and has libraries to do almost everything you can imagine.

I am not sure if CPAN or pip has more libraries though.

>> And it's generally easier to read/write.

Python looks nicer, but not everyone likes significant whitespace.

The Python 2 to 3 change has also caused some adoption issues. The Perl 4 to 5 happened quite a while ago. Perl 5 has been integral to Unix-like systems since the web went mainstream.

Re:Why does OpenBSD still include Perl in its base installat... by aryonoco on Saturday May 30, 2020 @08:58AM (#23360840)
Yeah and the Perl 5 to Perl 6 move... oh wait...
Re:Why does OpenBSD still include Perl in its base installat... by thesuperbigfrog on Saturday May 30, 2020 @09:36AM (#23361090)
Exactly. Widely-used programming languages continue to be used even when the authors move to a new version.

Perl 5 lives on and is still developed and maintained: https://www.perl.org/get.html

Perl 6 became Raku: https://www.raku.org/

Python 2 is still in use, but officially deprecated: https://www.python.org/doc/sunset-python-2/

You could probably start a business maintaining a Python 2 distribution and selling support to businesses and organizations that rely on Python 2 and are not in a position to upgrade: https://www.activestate.com/blog/python-2-eol-report-card-is...

Re:Why does OpenBSD still include Perl in its base installat... by jhoechtl on Saturday May 30, 2020 @09:55AM (#23361209)
Uh, raku looks interesting, the web site is also quite accessible. Sad that it took so long, maybe Perl6 would have regained some territory otherwise.
Re:Why does OpenBSD still include Perl in its base installat... by lizmat on Sunday May 31, 2020 @08:37AM (#23369173)
Writing this while looking at CrewDragon docking at the ISS. Which took SpaceX 18 years, and many people thinking they would never make it. If anything, an inspiration to the people currently working on and using Raku!
Re:Why does OpenBSD still include Perl in its base installat... by tankenmate on Saturday May 30, 2020 @09:09AM (#23360910)
And Larry & Co realised that it was sufficient different that now it is no longer called Perl 6.
Re:Why does OpenBSD still include Perl in its base installat... by pram on Saturday May 30, 2020 @10:08AM (#23361273)
and yet kept the logo that literally has P6 embedded in it
Re:Why does OpenBSD still include Perl in its base installat... by mirchibajji on Saturday May 30, 2020 @10:49AM (#23361573)
Perl5 is not EOLed, so no one is forced to "move". I haven't heard of any real business actually moving from Perl5 -> Perl6 (Raku, to be precise) for support/legacy reasons.
Re:Why does OpenBSD still include Perl in its base installat... by toast0 on Saturday May 30, 2020 @11:14AM (#23361725)
Perl 4 to 5 was before my time, but I read it wasn't really a major change, just to sell books. Perl 5.6 to 5.8 was the big moment where strings became (optionally) arrays of characters instead of arrays of bytes. This caused some amount of breakage, but was a long time ago now; anything else broken since then has been pretty minor.
Re:Why does OpenBSD still include Perl in its base installat... by qalmakka on Saturday May 30, 2020 @09:05AM (#23360883)
Because Python is sometimes too "formal" for dirty glue scripts that consist in invoking other commands and parsing their output. Perl blows pretty much every other language out of the water when you need to process text fast and without too much cruft.
Re:Why does OpenBSD still include Perl in its base installat... by tzs on Saturday May 30, 2020 @01:32PM (#23362914)
Perl's being more relaxed about creation and manipulation of data structures helps a lot, too those kinds of scripts.

Consider this:

  my %h = ();  # an empty hash

  $h{'foo'}[3]{'bar'} = 'bletch';
Now my hash contains one entry with key 'foo'. That entry is a reference to an array of 4 elements. The first 3 elements of that array are undef, and the 4th is a reference to another hash. That hash contains one entry with key 'bar' and value 'bletch'.

In a lot of other languages you would need to explicitly allocate an array of at least 4 elements for $h{'foo'}, and then explicitly allocate a hash for $h{'foo'}[3].

With Perl they just come into existence when you try to store something in them.

Is this always good? No. There are many problems where this automatic reification would just hide bugs. If you are, for example, processing data that is supposed to be clean and well specified then parsing it into explicitly allocated data structures that match what you expect and will throw errors if you try to put something in out of range is great.

But when you are dealing with data that is not clean and not well specified, being able to get it all in your data structures easily even when there is something wrong or inconsistent in the data can be great. Get it all in, and then you can run cleanup code on it looking for unexpected things, and fixing them or alerting you that you need to add more cases to the fixing code.

There's a similar thing on the reading side of Perl data structures. Try to access a hash via a key that does not exist, or an array by an index past the end? You simply get back undef.

Is this always good. Like automatic reification, the answer is no. There are many problems where it would be a grave error if the code tried to access an element of a hash or array that it had not previously but in there.

For problems where your logic would include a lot of places where you check if an element exists and if it does you add to a total or if it is a non-empty string you include it in something else, the code can be cleaner in a language with Perl's approach. Adding undef to a number is like adding 0, and undef compares as equal to an empty string.

Re:Why does OpenBSD still include Perl in its base installat... by bmn__ on Sunday May 31, 2020 @07:14AM (#23368770)
> Is this [autovivification] always good? No.

Good thing then that the programmer has the choice to use it or not.

    repl>>> no autovivification 'store'
    repl>>> my %h
    %h = ()
    repl>>> $h{'foo'}[3]{'bar'} = 'bletch'
    Runtime error: Can't vivify reference at (eval …)
> Is this [returning undef for non-existing keys/indexes] always good. […] the answer is no.

Good thing then that the programmer has the choice to use it or not.

    repl>>> use Hash::Util 'lock_keys'
    repl>>> my %h
    %h = ()
    repl>>> lock_keys %h
    ()
    repl>>> $h{no_such_key}
    Runtime error: Attempt to access disallowed key 'no_such_key' in a restricted hash at (eval …)

    repl>>> package Array::Bounded {
                use Tie::Array;
                use base 'Tie::StdArray';
                sub FETCH {
                    my ($self, $index) = @_;
                    die "Attempt to access out of bounds index $index in a restricted array" if $index >= $self->@*;
                    return $self->[$index];
                }
            }
    repl>>> tie my @a, 'Array::Bounded'
    repl>>> @a = ('a'..'f')
    @a = ('a'..'f')
    repl>>> $a[5]
    f
    repl>>> $a[6]
    Runtime error: Attempt to access out of bounds index 6 in a restricted array at (eval …)
Re:Why does OpenBSD still include Perl in its base installat... by soraminazuki on Saturday May 30, 2020 @09:21AM (#23360971)
I feel that's an apples vs oranges comparison. I'd prefer to write complex scripts in python, but for simple scripts, I find a mix of bash and perl to be useful.

If you want to make a comparison though, I think perl vs sed is a more meaningful one. You can do things that's possible in sed with perl one-liners in a more portable way. It's installed by default on major Linux distributions, and even works on macOS without the GNU vs BSD incompatibilities.

Re:Why does OpenBSD still include Perl in its base installat... by microtherion on Saturday May 30, 2020 @09:30AM (#23361034)
> I'd prefer to write complex scripts in python, but for simple scripts, I find a mix of bash and perl to be useful.

Unfortunately, simple scripts that are found to be useful often grow into complex scripts.

A few weeks ago, I wanted to write some functionality that seemed "simple" enough to be a good fit for make+bash. A week later, I ended up tossing the whole thing and rewriting it in python.

Re:Why does OpenBSD still include Perl in its base installat... by soraminazuki on Saturday May 30, 2020 @10:13AM (#23361309)
Not all scripts needs to be complex, though. There would always be demand for quick and easy scripting languages fit for handling simple cases. Yes, some scripts are bound to grow complex, but that doesn't really justify writing every single script in Python. It's just better to make the switch once the benefits exceed the costs.
Re:Why does OpenBSD still include Perl in its base installat... by microtherion on Saturday May 30, 2020 @03:16PM (#23363815)
In principle, I agree, but in practice, I often find that it's common to make the switch too late, so I'm leaning towards making the switch early.
Re:Why does OpenBSD still include Perl in its base installat... by asveikau on Saturday May 30, 2020 @11:37AM (#23361868)
I find Perl easier to write, mostly because my personal experience has a lot of C and shell scripting, and perl borrows a lot from both.

It is useful and not difficult to be productive in, but does manage to feel more unnatural than both of those though.