Groups | Search | Server Info | Login | Register
Groups > comp.lang.perl.misc > #4593
| From | Rainer Weikusat <rweikusat@mssgmbh.com> |
|---|---|
| Newsgroups | comp.lang.perl.misc |
| Subject | Re: $var = do { ... }? |
| Date | 2012-03-02 19:18 +0000 |
| Message-ID | <877gz2errh.fsf@sapphire.mobileactivedefense.com> (permalink) |
| References | <jiqv7d$6k4$1@reader1.panix.com> |
tmcd@panix.com (Tim McDaniel) writes:
> I just had a case where there's a block of code, I split up the work
> into assignments to intermediate variables, but I only wanted the
> final value. I very much like to restrict the scope of variables,
> because it's then obvious what's a temporary and what has more
> significance
[...]
> my $permanent_variable = do {
> my $this = ...;
> my $that = ... $this ...;
> yadda yadda;
> ... final computation ...;
> };
>
> It does do the scope encapsulation that I like, and it makes it
> vividly obvious that the block has one purpose, to set
> $permanent_variable.
[...]
> $var = do { several statement; }
>
> is just an odd construct?
I decidedly do consider this an odd construct: If you have a
self-contained piece of code whose purpose is to perform some
operation independently of the surrounding code and to return some
value, and which possible has a small number of well-defined inputs,
this should be put into a subroutine with a sufficiently descriptive
name that someone who reads through the outer code knows what the
purpose of the subroutine happens to be but without having to bother
with the details of its implementation and that someone who cares
about the implementation of this particular subroutine doesn't have to
go hunting for it in a large block of otherwise unrelated code.
A real-world example of that:
sub validate_customer_key($)
{
my ($bin_ckey, $ckey, $skey, $skey_version);
eval {
($bin_ckey, $skey_version) = decode_bin_ckey($_[0]);
$skey = get_server_key($skey_version);
die("no version $skey_version server key") unless $skey;
dec($bin_ckey, $skey->key());
$ckey = MECSUPD::CustomerKey->new_from_string($bin_ckey);
die("not a valid customer key") unless $ckey;
check_cookie($ckey, $skey);
check_expiry($ckey);
check_ckey_cid_version($ckey);
};
$@ && do {
syslog('ERR', "$@");
return;
};
syslog('INFO', 'customer %08x authenticated successfully',
$ckey->cid());
return 1;
}
['dec' means 'decrypt' here]
This is (not counting external libraries) 'a meeting point' of 80
lines of Perl code and 20 lines of C code and the algorithm this
subroutine is supposed to perform would be totally obliterated if the
100 lines of code implementing all the details had been used in place
of this 22 lines (sloccount) high-level description.
Back to comp.lang.perl.misc | Previous | Next — Previous in thread | Next in thread | Find similar
$var = do { ... }? tmcd@panix.com (Tim McDaniel) - 2012-03-02 17:15 +0000
Re: $var = do { ... }? merlyn@stonehenge.com (Randal L. Schwartz) - 2012-03-02 09:52 -0800
Re: $var = do { ... }? Shmuel (Seymour J.) Metz <spamtrap@library.lspace.org.invalid> - 2012-03-02 13:42 -0500
Re: $var = do { ... }? Ben Morrow <ben@morrow.me.uk> - 2012-03-02 18:44 +0000
Re: $var = do { ... }? tmcd@panix.com (Tim McDaniel) - 2012-03-02 19:00 +0000
Re: $var = do { ... }? merlyn@stonehenge.com (Randal L. Schwartz) - 2012-03-02 11:45 -0800
Re: $var = do { ... }? Ben Morrow <ben@morrow.me.uk> - 2012-03-02 20:06 +0000
Re: $var = do { ... }? Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-03-02 19:18 +0000
Re: $var = do { ... }? tmcd@panix.com (Tim McDaniel) - 2012-03-02 19:24 +0000
Re: $var = do { ... }? Ben Morrow <ben@morrow.me.uk> - 2012-03-02 20:10 +0000
Re: $var = do { ... }? Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-03-02 20:43 +0000
Re: $var = do { ... }? Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-03-02 22:47 +0000
Re: $var = do { ... }? Martijn Lievaart <m@rtij.nl.invlalid> - 2012-03-03 15:58 +0100
Re: $var = do { ... }? Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-03-03 17:11 +0000
Re: $var = do { ... }? Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-03-03 17:19 +0000
Re: $var = do { ... }? Martijn Lievaart <m@rtij.nl.invlalid> - 2012-03-03 23:58 +0100
Re: $var = do { ... }? Ben Morrow <ben@morrow.me.uk> - 2012-03-04 00:29 +0000
Re: $var = do { ... }? Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-03-04 14:44 +0000
Re: $var = do { ... }? Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-03-05 20:50 +0000
Re: $var = do { ... }? "Dr.Ruud" <rvtol+usenet@xs4all.nl> - 2012-03-05 11:35 +0100
Re: $var = do { ... }? Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-03-05 15:22 +0000
Re: $var = do { ... }? Ben Morrow <ben@morrow.me.uk> - 2012-03-05 16:38 +0000
Re: $var = do { ... }? Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-03-02 20:44 +0000
Re: $var = do { ... }? tmcd@panix.com (Tim McDaniel) - 2012-03-15 17:09 +0000
Re: $var = do { ... }? Ben Morrow <ben@morrow.me.uk> - 2012-03-16 00:18 +0000
Re: $var = do { ... }? Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-03-16 14:25 +0000
Re: $var = do { ... }? tmcd@panix.com (Tim McDaniel) - 2012-03-16 21:01 +0000
Re: $var = do { ... }? Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-03-16 21:37 +0000
Re: $var = do { ... }? Ben Morrow <ben@morrow.me.uk> - 2012-03-16 22:56 +0000
Re: $var = do { ... }? Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-03-16 23:54 +0000
Re: $var = do { ... }? tmcd@panix.com (Tim McDaniel) - 2012-03-17 18:48 +0000
Re: $var = do { ... }? Ben Morrow <ben@morrow.me.uk> - 2012-03-17 22:53 +0000
Re: $var = do { ... }? Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-03-18 15:40 +0000
Re: $var = do { ... }? "Dr.Ruud" <rvtol+usenet@xs4all.nl> - 2012-03-16 14:08 +0100
Re: $var = do { ... }? tmcd@panix.com (Tim McDaniel) - 2012-03-16 15:25 +0000
Re: $var = do { ... }? Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-03-16 17:46 +0000
csiph-web