Segmentation fault protection: Difference between revisions

Added Perl
(→‎{{header|Raku}}: Add a Raku example)
(Added Perl)
Line 55:
...
</lang>
 
=={{header|Perl}}==
<!-- To all the Perl pedants out there: Yes, I have elided much extraneous detail -->
It's fairly hard to get into this kind of trouble with Perl, as it manages memory for you, etc, etc. But it can happen, and the #1 technique for keeping yourself on the straight and narrow is to start your programs off with:
 
<code>use strict;
use warnings;</code>
 
This is so important, I'm going to say it again, louder:
 
<code><b>use strict;
use warnings;</b></code>
<p>
For example, this code will segfault:
<lang perl>unpack p,1x8</lang>
But with the safety features enabled you get compilation errors:
<lang perl>use strict;
use warnings;
unpack p,1x8</lang>
{{out}}
<pre>Bareword "p" not allowed while "strict subs" in use at exp/SegV line 6.
Execution aborted due to compilation errors.</pre>
 
But if you absolutely feel the need to court disaster, at least run your code in an external process.
<lang perl>qx/perl -e 'unpack p,1x8' 2>&1/ or die "Couldn't execute command\n";</lang>
 
=={{header|Phix}}==
2,392

edits