4 ms·
In addition to what others have said, perl also directly exposes errno via $!: $ perl -E 'unlink("/etc/passwd"); say (0+$!)' 13 $ grep 13 /usr/incl
by hoytech 9y ago
In addition to what others have said, perl also directly exposes errno via $!:
$ perl -E 'unlink("/etc/passwd"); say (0+$!)'
13
$ grep 13 /usr/include/asm-generic/errno-base.h
#define EACCES 13 /* Permission denied */
Of course $! is a special dual-val so if you treat it as a string you get the nicer strerror(3) equivalent:
$ perl -E 'unlink("/etc/passwd"); say $!'
Permission denied
That said, some people prefer autodie: https://metacpan.org/pod/autodie https://metacpan.org/pod/autodie
- i336_ 9y agoWow, interesting. The language has type coercion, and makes full use of it. I likey :D Thanks for the example too!
- cestith 9y agoIt actually goes a bit beyond type coercion. Larry Wall is a linguist. The language is actually contextual. There's numeric vs. string context for scalars and scalar vs. list context for assignments and returns. The list of features is impressive, but taken together all at once can be daunting. I think that's why there's so much distaste for it in certain camps. It's actually possible in a function to test whether the caller wants a single value or a list and adjust your return accordingly. It has anonymous functions. It has closures. It has map() and sort(), either of which apply a custom block of code to a list. You can easily write user-supplied functions that do that, too. It has dictionaries / associative arrays (called "hashes" in the community since that's how they are implemented), regular arrays, scalar values, and a number of different object systems from which to choose.