in reply to Re: Make $^V and "my" implicit
in thread Make $^V and "my" implicit

Bad benchmarks do not a good case make:

#! perl -slw use strict; use Benchmark qw[ cmpthese ]; our $fred = 'fred'; our $bill = undef; cmpthese -1, { definedor => q[ for( 1 .. 1e3 ) { $fred //= 'fred'; $bill //= 'bill';; } ], unlessdefined => q[ for( 1 .. 1e3 ) { $fred = 'fred' unless defined $fred; $bill = 'bill' unless defined $bill; } ], ternary => q[ for( 1 .. 1e3 ) { $fred = defined $fred ? $fred : 'fred'; $bill = defined $bill ? $bill : 'bill'; } ], }; __END__ C:\test>junk71 Rate ternary unlessdefined definedor ternary 3989/s -- -45% -53% unlessdefined 7283/s 83% -- -14% definedor 8455/s 112% 16% --

But as tux says, it's not about speed, it's about convenience, conciseness and readability.

Performance gain (when measured correctly), is simply a nice side effect.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^3: Make $^V and "my" implicit
by boftx (Deacon) on Feb 04, 2014 at 01:22 UTC

    I have to ask. Isn't $bill = defined($bill) || 'bill'; the same as the example given above? IIRC, defined returns the scalar value of the expression being tested, which of course is undef when it is not defined. (For those who have not had reason to use it, this is similar to how delete returns the value of the element being deleted, including undef if that was the value or if the element did not exist.)

    Update: DOH!!!! I most certainly did NOT recall correctly! After looking at that again it is obvious that it has to return a Boolean "true" in the case of '0' or ''. The comment regarding delete is correct, but no longer applies in this context. :(

    It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.