Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^4: why avoid & on function call

by talexb (Chancellor)
on Dec 28, 2020 at 17:02 UTC ( [id://11125879]=note: print w/replies, xml ) Need Help??


in reply to Re^3: why avoid & on function call
in thread why avoid & on function call

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11125879]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-25 13:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found