Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!nx01.iad01.newshosting.com!newshosting.com!novia!news-out.readnews.com!transit4.readnews.com!panix!not-for-mail From: tmcd@panix.com (Tim McDaniel) Newsgroups: comp.lang.perl.misc Subject: Re: $var = do { ... }? Date: Fri, 16 Mar 2012 15:25:10 +0000 (UTC) Organization: Tim McDaniel's at Panix Lines: 40 Message-ID: References: <4f633b3d$0$6845$e4fe514c@news2.news.xs4all.nl> Reply-To: tmcd@panix.com NNTP-Posting-Host: panix2.panix.com X-Trace: reader1.panix.com 1331911510 22311 166.84.1.2 (16 Mar 2012 15:25:10 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Fri, 16 Mar 2012 15:25:10 +0000 (UTC) X-Newsreader: trn 4.0-test76 (Apr 2, 2001) X-Received-Bytes: 2313 Xref: csiph.com comp.lang.perl.misc:4768 In article <4f633b3d$0$6845$e4fe514c@news2.news.xs4all.nl>, Dr.Ruud wrote: >On 2012-03-15 18:09, Tim McDaniel wrote: > >> So I have to make sure the code evaluates the desired return value as >> the last thing in the block, like >> >> my $result = do { >> if ($i % 2 == 0) { 'even' } >> elsif ($i % 3 == 0) { 'divisible by 3' } >> elsif ($i % 5 == 0) { 'divisible by 5' } >> else { 'just wrong' } >> }; >> >> Is there a clever way in Perl 5 to metaphorically return early with a >> value? > >That if/elsif/else of yours already does that. Yes, I know. I was giving an example where the desired final value is the last thing evaluated, and then asking what I could do if that happens not to be the case. The next (badly contrived) example I gave was such a case, with the 'even' case returning early and the first "elsif" changed to an "if". > my $result = !$i ? "0" > : !($i % 2) ? "2-fold" > : !($i % 3) ? "3-fold" > : !($i % 5) ? "5-fold" > : "bah"; My question was prompted bya ?: chain like that, except with much larger and more complicated expressions, and embedded in a much larger and much more complicated expression. I began idly musing on different ways it could be avoided, other than the obvious ones of factoring it into a sub, moving it to an if-elsif-else above the much more complicated block, and so forth. -- Tim McDaniel, tmcd@panix.com