Live data from Hacker News

Perlsecret – Perl secret operators and constants

metacpan.org

11–20 of 32 posts

Re: Perlsecret – Perl secret operators and constants

#11
post #9

After first experiences with linux shell scripting, sed, awk, and C in 1990s, I found perl a welcome refuge. Way more featureful than DOS .bat files or BASIC! Its capabilities (perl + cpan) have always well exceeded my need for CS goodness. People do complain about the syntax, oddly, without mentioning the numerous ways perl was designed to make common tasks easy to do. The "use strict" pragma, and early adoption of…

[deleted]

Re: Perlsecret – Perl secret operators and constants

#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-liners. When I later read PG's "Succinctness is power" essay, I thought of Perl.

https://paulgraham.com/power.html

Re: Perlsecret – Perl secret operators and constants

#13
Perk is... quite a thing. I think if you like programming because you like believing you have secret knowledge... go for it. Perk will scratch that itch. But I do not believe it beings you closer to the pantheon of God's. Ai n't gonna stop anyone from dancing with the Satyrs though, if that's your jam.

Re: Perlsecret – Perl secret operators and constants

#14
> !! Bang bang boolean conversion

This isn't a special operator. This is just how "not" (!) works. In basically every language: C, C++, Javascript, Perl, etc., ! is the "not" operator so !12 gives you false (12 is truthy), and !!12 (not false) gives you true.

It's the same in languages that use different operators for "not". In python, the "not" operator is just the word not, and can write "not not 12" to get True. They didn't implement a special "not not" operator, anymore than Perl implemented a "!!" operator. They just implemented the basic ! / "not" operator.

Re: Perlsecret – Perl secret operators and constants

#15
post #14

> !! Bang bang boolean conversion This isn't a special operator. This is just how "not" (!) works. In basically every language: C, C++, Javascript, Perl, etc., ! is the "not" operator so !12 gives you false (12 is truthy), and !!12 (not false) gives you true. It's the same in languages that use different operators for "not". In python, the "not" operator is just the word not, and can write "not not 12" to get True. T…

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 a result of exactly `0` or `1`, and works well in macros and constant expressions.

Re: Perlsecret – Perl secret operators and constants

#16
post #14

> !! Bang bang boolean conversion This isn't a special operator. This is just how "not" (!) works. In basically every language: C, C++, Javascript, Perl, etc., ! is the "not" operator so !12 gives you false (12 is truthy), and !!12 (not false) gives you true. It's the same in languages that use different operators for "not". In python, the "not" operator is just the word not, and can write "not not 12" to get True. T…

Right, that's the point of TFA. It doesn't list "special" operators, it lists "secret" operators -- that is, operators combined from existing sigils that do clever things.

The "Venus" operator is a good example: it's the '+' addition operator! You just add zero to a value that's coercible into a number.

The Eskimo operators are also interesting: similar to a SQL injection attack, you use a close brace and an open brace to stop and start a new code block from within a string that's sent to the interpreter. Perl didn't invent open and close braces: hence the verb "discover" rather than "implement".

The whole page is a bit of a lark, and a good example of why some of us don't enjoy Perl!

Re: Perlsecret – Perl secret operators and constants

#17
post #16
post #14

> !! Bang bang boolean conversion This isn't a special operator. This is just how "not" (!) works. In basically every language: C, C++, Javascript, Perl, etc., ! is the "not" operator so !12 gives you false (12 is truthy), and !!12 (not false) gives you true. It's the same in languages that use different operators for "not". In python, the "not" operator is just the word not, and can write "not not 12" to get True. T…

Right, that's the point of TFA. It doesn't list "special" operators, it lists "secret" operators -- that is, operators combined from existing sigils that do clever things. The "Venus" operator is a good example: it's the '+' addition operator! You just add zero to a value that's coercible into a number. The Eskimo operators are also interesting: similar to a SQL injection attack, you use a close brace and an open bra…

Fair point. I should've read more before jumping in here with my first reaction.

Re: Perlsecret – Perl secret operators and constants

#18
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.

Please show me an example of Perl that looks beautiful.

I don't believe you.

Re: Perlsecret – Perl secret operators and constants

#19
post #14

> !! Bang bang boolean conversion This isn't a special operator. This is just how "not" (!) works. In basically every language: C, C++, Javascript, Perl, etc., ! is the "not" operator so !12 gives you false (12 is truthy), and !!12 (not false) gives you true. It's the same in languages that use different operators for "not". In python, the "not" operator is just the word not, and can write "not not 12" to get True. T…

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.

Re: Perlsecret – Perl secret operators and constants

#20
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...
Post reply on HN