News for hackers. Stuff that matters.
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.