Perlsecret – Perl secret operators and constants
21–30 of 32 posts
Re: Perlsecret – Perl secret operators and constants
#22Earlier 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.
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
#23I 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...
perl -Minteger -E 'say ~-0'
and it should work as expected.Re: Perlsecret – Perl secret operators and constants
#24Perl 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.
Re: Perlsecret – Perl secret operators and constants
#25if ($text =~ /error/) { print "Found error\n"; }
if ($text !~ /error/) { print "No error found\n"; }
It's just fun and fast to slice and dice text this way.
Re: Perlsecret – Perl secret operators and constants
#26Perl 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
#27I 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
#28Perl 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…
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
#29Perl 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.
Re: Perlsecret – Perl secret operators and constants
#30Perl 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 :)
Unfortunately I had a different experience with the Ruby community, so I eventually switched to Python along with apparently everybody else.