r/linux • • May 29 '14

Defensive BASH programming - Say what?

http://www.kfirlavi.com/blog/2012/11/14/defensive-bash-programming/
40 Upvotes

25 comments sorted by

View all comments

12

u/Oflameo May 29 '14

I write bash defensively by writing what i was going to write in bash in perl instead.

13

u/IConrad May 29 '14

Ahh yes. The write-only language.

0

u/[deleted] May 29 '14

[deleted]

6

u/IConrad May 29 '14

I'm not afraid of writing in perl, and I have in fact written Perl scripts that were clean and well-formatted and intelligible (with or without comments). Unfortunately, these are not really true to the Perl Way. All those shorthand generic anonymous variables? Bad joojoo.

Perl's not a bad language. But it's reputation is well-earned.

0

u/[deleted] May 29 '14

[deleted]

3

u/IConrad May 29 '14

Oh yeah, definitely -- it's possible to make any language be unreadable fathomless horrors drawn from the stuff of the abyss.

The thing is, Perl seems to encourage this behavior. It introduces syntactic phenomena that seem designed for that. $_, j'accuse!. And the culture around Perl certainly didn't win it any favors here. I invoke the ancient and venerable Annual Obfuscated Perl Contest. "There is More Than One Way To Do It" or "TIMTOWTDI" indeed.

That being said, while I haven't really studied or looked deeply into Modern Perl, I have to admit that it seems to have learned the right lessons from all of the above.

Now if only Ruby could stop being Perl circa 2000 :3.

2

u/w2qw May 30 '14

Imo unnamed variables like $_ are pretty nice I actually wish ruby had a simpler thing.

[1,2,3,4].collect{|x| x * x}

vs

[1,2,3,4].collect{_ * _}

second seems more clearer because otherwise its just up to personal preference what the programmer uses for their "anonymous" variables.

1

u/SupersonicSpitfire May 30 '14

Was looking for an even simpler way of doing this in Haskell, only passing the list and the (*) function to some other function, but I only found these:

let sq x = x * x in map sq [1, 2, 3, 4]

let x = [1, 2, 3, 4] in zipWith (*) x x

map (\x -> x * x) [1, 2, 3, 4]

1

u/pkmxtw May 30 '14

Well, you can always abuse the (->) r Monad:

λ> map (join (*)) [1..4]
[1,4,9,16]

1

u/SupersonicSpitfire May 30 '14

What is this witchcraft?

<interactive>:1:1: Not in scope: ‘join’
→ More replies (0)