Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • I assume it's to avoid calling the method on undef values.

    So try to find (or establish) the boundary where $var is always known to not be undef. Test it once there, and not in an inner loop. If possible, it may very well not be.

    If that's not feasible, how about introducing null objects instead of undef values?

    Re: squeezing out performance. Do you need the overhead of calling blessed? If $var contains a string instead of an object, $var->isa is going to work as a class method anyway, right? (and return fa
    • I assume it's to avoid calling the method on undef values.
      Not quite. It's to avoid calling a method on an unblessed reference. People used to use

      UNIVERSAL::isa($value, $class)
      for this reason, but lately this has fallen into disgrace, mainly for proper supporting mock objects [cpan.org], if I understood it correctly.
      • > mainly for proper supporting mock objects [cpan.org], if I understood it correctly.

        Also anything else that needs custom isa values, asa.pm, Class::Adapter, Class::Decorator, and others.

        The only legitimate use for the UNIVERSAL functions directly that I'm aware of is the UNIVERSAL::can($foo, 'can') which is a highly back-compatible method for testing "is $foo an object".

        • The only legitimate use for the UNIVERSAL functions directly that I'm aware of is the UNIVERSAL::can($foo, 'can') which is a highly back-compatible method for testing "is $foo an object".
          So, maybe we could use this then:

          UNIVERSAL::can($thing, 'isa') && $thing->isa($class)
          or, as you seem to prefer:

          UNIVERSAL::can($thing, 'can') && $thing->isa($class)
          :)

          No joke, I extended the original benchmarks with

          isa => sub {
              foreach my $var ($scalar, $cgi, $cgi2,$other) {
                  if (UNIVERSAL::can($var, 'isa') && $var->isa('CGI')) {}
              }
          },
          can => sub {
              foreach my $var ($scalar, $cgi, $cgi2,$other) {
                  if (UNIVERSAL::can($var, 'can') && $var->isa('CGI')) {}
              }
          },
          and got these results:

          Benchmark: timing 100000 iterations of blessed, can, eval, isa...
             blessed:  1 wallclock secs ( 0.67 usr +  0.00 sys =  0.67 CPU) @ 148809.52/s (n=100000)
                 can:  1 wallclock secs ( 0.58 usr +  0.00 sys =  0.58 CPU) @ 173010.38/s (n=100000)
                eval:  1 wallclock secs ( 1.63 usr +  0.00 sys =  1.63 CPU) @ 61538.46/s (n=100000)
                 isa: -1 wallclock secs ( 0.64 usr +  0.00 sys =  0.64 CPU) @ 156250.00/s (n=100000)

          Well... My code definitely is the fastest. I am surprised at the speed difference between 'can' and 'isa'. 'can' is the winner!