Usually I add an explicit exit statement to the end of the 'main' part of the code ..
I don't .. because I use braces for my 'mainline' code. Here's a recent script I wrote based on something I saw on Twitter:
#!/usr/bin/perl
use strict;
use warnings;
# Base 24 question from https://twitter.com/pwnallthethings/status/13
+43590455511023621
my @digits = qw/B C D F G H J K M P Q R T V W X Y 2 3 4 6 7 8 9/;
my %digit_value;
my $string = 'FCKGW-RHQQ2-YXRKT-8TG6W-2B7Q8';
# Convert a base 24 number to decimal. This is a group of five
# numbers, like a Windows key.
{
my $v = 0;
foreach my $d ( @digits ) {
$digit_value{ $d } = $v++;
}
my @units = split ( /-/, $string );
foreach my $u ( @units ) {
my $value = decode ( $u );
print "$u -> $value\n";
}
}
sub decode
{
my ( $str ) = @_;
my $val = 0;
my @chars = split ( //, $str );
foreach my $c ( @chars ) {
$val *= 24;
$val += $digit_value{ $c };
}
return $val;
}
There's no exit required because the closing brace indicates "That's the end!" to Perl. I have seen code where the mainline is right up against the left margin -- I never code like that, because it obscures what's going on -- it's the same level as the use statements, so I don't know where the declarations end and the executable code starts.
And I don't put an exit anywhere, because that would indicate I was bailing out early, instead of allowing the syntax to naturally indicate where the end of the procedure is. To be clear, I'm *not* saying your way is wrong -- it's just not what I prefer. I like my way because it's clear and legible. For me, white space is my friend.
Alex / talexb / Toronto
Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|