Groups | Search | Server Info | Login | Register


Groups > comp.lang.perl.misc > #4775

Re: $var = do { ... }?

From Rainer Weikusat <rweikusat@mssgmbh.com>
Newsgroups comp.lang.perl.misc
Subject Re: $var = do { ... }?
Date 2012-03-16 21:37 +0000
Message-ID <871uosw7ls.fsf@sapphire.mobileactivedefense.com> (permalink)
References <jiqv7d$6k4$1@reader1.panix.com> <jjt7p5$e3g$1@reader1.panix.com> <2gda39-ljc2.ln1@anubis.morrow.me.uk> <871uosoc6n.fsf@sapphire.mobileactivedefense.com> <jk09nb$ong$1@reader1.panix.com>

Show all headers | View raw


tmcd@panix.com (Tim McDaniel) writes:
> In article <871uosoc6n.fsf@sapphire.mobileactivedefense.com>,
> Rainer Weikusat  <rweikusat@mssgmbh.com> wrote:
>>Ben Morrow <ben@morrow.me.uk> writes:
>>> Quoth tmcd@panix.com:
>>
>>[...]
>>
>>>>     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?
>>
>>[...]
>>
>>> The other thing that works, and it is in fact documented though I had no
>>> idea until I just looked, is to return from an eval {}:
>>>
>>>     my $result = eval {
>>>         $_ % 2 == 0     and return "even";
>>>         $_ % 3 == 0     and return "divisible by three";
>>>         return "just wrong";
>>>     };
>>>
>>> I'm not sure it's got much to recommend it over
>>>
>>>     my $result = sub {
>>>         $_ % 2 == 0     and return "even";
>>>         return "odd";
>>>     }->();
>>>
>>> though,
>>
>>The first is using a language construct according to its intended
>>purpose.  The second is abusing a language construct in order to
>>emulate the first 'somehow'. That alone should be sufficient to avoid
>>it. In addition to that, it needs more test because the mock
>>subroutine created for this purpose also needs to be invoked and -
>>depending on whether the compiler special-cases this so that people
>>can indulge their passion for the bizarre[*] - it is probably also
>>less efficient.
>
> I infer that "first" means the eval example and "second" means the sub
> example.
>
> The notion of "abusing a language construct" in Perl is already a
> problematic notion. Perl is the polyperverted abuse bottom of
> languages.

The Perl code some people write is 'the polyperverted abuse bottom of
coding', this being enabled by the versatility of the language.

> The doc for eval says "This form is typically used to trap exceptions
> more efficiently than the first (see below), while also providing the
> benefit of checking the code within BLOCK at compile time."  So using
> eval just to be able to return is not its "intended purpose".

this text continues with

	In both forms, the value returned is the value of the last
	expression evaluated inside the mini-program; a return
	statement may be also used, just as with subroutines. 

> In fact, the eval version is the one that needs the testing, not the
> sub.  eval traps fatal terminations, and just about anything can
> die().  In this simple case, I think you'd need "use warnings FATAL =>
> 'numeric';".  More realistic examples can die much easier.  So you
> can't just replace do{...} with eval{...}; you'd have to add proper
> error checking and we've recently had a discussion about the edge
> cases to worry about.

Almost anything can be turned from a molehill into an insurmountable
moutain by sufficiently wild generalizations. None of this is an issue
for the actual example and there is a single 'edge case' code using
eval for exception handling needs to worry about (There are two more
if some module written by some confused guy is being used. But since
these 'edge cases' have been fixed in Perl 5.14.0, there's little
reason to do so).

> sub{...}, on the other hand, *is* a straightforward substitution.
> Also, for people like me who are used to Lisp and similar languages
> that make much more use of small functions created all over, it's a
> natural idiom.

If you want to make it a subroutine then do so. That would be a
sensible choice. Recreating an otherwise invariable anonymous
subroutine every time the code is executed in order to call it once
just to avoid structuring the code as hard as possible isn't.

Back to comp.lang.perl.misc | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

$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