in reply to Re: Re: Good way of getting subroutine name from ref?
in thread Good way of getting subroutine name from ref?

Oh that's a good point. I like it. One clarification though - eval returns undef when it fails so your `|| undef` is pure obfuscation. You disguise that eval already has the || undef behaviour automatically.
  • Comment on Re: Re: Re: Good way of getting subroutine name from ref?

Replies are listed 'Best First'.
Re: Re: Re: Re: Good way of getting subroutine name from ref?
by ysth (Canon) on Dec 07, 2003 at 09:24 UTC
    I know eval is documented that way, but it actually returns an empty list in list context, which messes things up if you are passing cv2name as a parameter to another function with other parameters following.

      Ok, so if the problem is context, solve that problem: scalar eval { ... } neatly describes the issue without the || undef which I'm nitpicking because I don't like the style.

        Fair enough. The scalar() version is certainly more efficient, but I think the || undef is more descriptive.

        Just to keep this post from having zero value, here's another case where I used B without eval. The problem was to show what a format (set via a complex eval STRING) was getting set to. Original version only allowed deparse_format(*WHATEVER{FORMAT}) but *FOO{FORMAT} isn't supported before perl-5.7.0.

        sub deparse_format { use B::Deparse; # translate arg (or reference to it) into a B::* object my $Bobj = B::svref_2object(ref $_[0] ? $_[0] : \$_[0]); # if passed a glob or globref, get the format $Bobj = B::GV::FORM($Bobj) if ref $Bobj eq 'B::GV'; if (ref $Bobj ne 'B::FM') { require Carp; Carp::croak "deparse_format: expected a glob, globref, or form +at ref"; } my $deparser = B::Deparse::->new(); return $deparser->indent($deparser->deparse_format($Bobj)); }