Live data from Hacker News

Perlsecret – Perl secret operators and constants

metacpan.org

21–30 of 32 posts

Re: Perlsecret – Perl secret operators and constants

#22
post #19

Earlier quoted context omitted.

Keep in mind that applying the logical NOT operator twice (using `!!`) converts any integer expression into a strict boolean. Any non-zero value becomes `1`, and zero remains `0`. This is commonly used for boolean normalization when the original expression yields a bitmask or arbitrary integer. While the same result can be written as `(x != 0)`, the `!!x` idiom is concise, widely used in low-level C code, guarantees…

Fair, I forgot that C bools are just 0 and 1. That's where I first learned the !! trick, but it's been many a year.

Err, C bools have two interpreted values, TRUE, and FALSE.

Confusingly (to some) they are integers and while 0 represents FALSE, any non 0 value represents TRUE.

It's pedantic, apologies, but that is why the GP refers to "convert to strict boolean"

Re: Perlsecret – Perl secret operators and constants

#23

I didn't know ~- and -~ had a name, I have been using them in many (non-production) places where I wanted to save 2 bytes. But looks like Perl's implementation is more limited compared to other languages: % perl -e 'print ~-(0), "\n"' 18446744073709551615 % ruby -e 'print ~-(0), "\n"' -1 https://metacpan.org/dist/perlsecret/view/lib/perlsecret.pod...

You're not using `integer` in the perl example like you're supposed to. Try

    perl -Minteger -E 'say ~-0'
and it should work as expected.

Re: Perlsecret – Perl secret operators and constants

#24
post #4

Perl was the first language I learned on my own after graduating university many years ago. I fell in love with it because of quirks like these and because code written in it can have a poetic quality you don't see often. Now I am old and joyless and I want the code I write for work to be boring and unsurprising. But sometimes one can still want to write poetry.

Perl was also my first productive language, and I do miss it a little. Write something like []string{"foo", "bar", "baz"} in go and you really appreciate qw(foo bar baz). Perl was always designed to be easy to type in, and maybe not so easy to maintain later. Good memories, but not for me anymore.

Re: Perlsecret – Perl secret operators and constants

#26
post #4

Perl was the first language I learned on my own after graduating university many years ago. I fell in love with it because of quirks like these and because code written in it can have a poetic quality you don't see often. Now I am old and joyless and I want the code I write for work to be boring and unsurprising. But sometimes one can still want to write poetry.

Perl was also my first productive language, and I do miss it a little. Write something like []string{"foo", "bar", "baz"} in go and you really appreciate qw(foo bar baz). Perl was always designed to be easy to type in, and maybe not so easy to maintain later. Good memories, but not for me anymore.

All of the Quote and Quote-like Operators, and __DATA__, and so many other niceties..

Re: Perlsecret – Perl secret operators and constants

#27
post #23

I didn't know ~- and -~ had a name, I have been using them in many (non-production) places where I wanted to save 2 bytes. But looks like Perl's implementation is more limited compared to other languages: % perl -e 'print ~-(0), "\n"' 18446744073709551615 % ruby -e 'print ~-(0), "\n"' -1 https://metacpan.org/dist/perlsecret/view/lib/perlsecret.pod...

You're not using `integer` in the perl example like you're supposed to. Try perl -Minteger -E 'say ~-0' and it should work as expected.

That is true, but for the kind of code where I might use this trick, there usually aren't enough `~-$x` for `use integer;` to be worthwhile, I would just do `($x-1)` instead.

Re: Perlsecret – Perl secret operators and constants

#28
post #12
post #4

Perl was the first language I learned on my own after graduating university many years ago. I fell in love with it because of quirks like these and because code written in it can have a poetic quality you don't see often. Now I am old and joyless and I want the code I write for work to be boring and unsurprising. But sometimes one can still want to write poetry.

Agreed! I learned Perl after trying C; and after struggling with `scanf` (not even getting to tokenization), the ease and speed of `while ( ) { @A = split;` for text-handling made it easy to fall in love. This (in the mid 90s, before Java, JavaScript, and C++ TR1) was also my first contact with associative arrays. I was also drawn to the style of the Camel Book. More than most other languages, Perl encouraged one-lin…

> This (in the mid 90s, before Java, JavaScript, and C++ TR1) was also my first contact with associative arrays.

Associative array is just a fancy term for map / dictionary. C++ has always had one of those, even before TR1: std::map (which is a tree under the hood). It does have the extra requirement that your key be ordered, which isn't part of the core definition of associate array[1]. But usually it's not a problem even if you don't actually need the ordering.

As I think you're implying, TR1 / C++11 added std::unordered_map, which is a hash table and doesn't need keys to be ordered (just hashable).

[1] It isn't part of the core definition of "map" either, which despite C++'s usage just means the same thing as dictionary / associative array. A lot of those early STL containers are confusingly named: e.g., in general, "list" just means some ordered sequence of elements, so it covers static arrays, dynamic arrays, and linked lists, but C++ uses this term for linked lists, probably the least likely understood meaning. It use of the term "vector" for contiguous dynamic arrays is very odd. But I'm now way off topic...

Re: Perlsecret – Perl secret operators and constants

#29
post #4

Perl was the first language I learned on my own after graduating university many years ago. I fell in love with it because of quirks like these and because code written in it can have a poetic quality you don't see often. Now I am old and joyless and I want the code I write for work to be boring and unsurprising. But sometimes one can still want to write poetry.

Ruby took the best parts of Perl and got rid of the worst. Give it a shot :)

Re: Perlsecret – Perl secret operators and constants

#30
post #29
post #4

Perl was the first language I learned on my own after graduating university many years ago. I fell in love with it because of quirks like these and because code written in it can have a poetic quality you don't see often. Now I am old and joyless and I want the code I write for work to be boring and unsurprising. But sometimes one can still want to write poetry.

Ruby took the best parts of Perl and got rid of the worst. Give it a shot :)

I actually switched to Ruby for this very reason and used it for a few years. I still have a soft spot for Ruby as well.

Unfortunately I had a different experience with the Ruby community, so I eventually switched to Python along with apparently everybody else.

Post reply on HN