in reply to Re: Avoiding reference of sub optimization
in thread Avoiding reference of sub optimization

I totally understand your point about immutable subs , but...

> UPDATE to your UPDATE: An array is different since you can change values in it. But you can't edit a subroutine "in flight"

Actually I'm blessing them into a dedicated class to set attributes, so subs with the same body can have different attributes. (like debug informations)

UPDATE: And yes using subs as objects isn't frequently done in Perl ...

Cheers Rolf

  • Comment on Re^2: Avoiding reference of sub optimization

Replies are listed 'Best First'.
Re^3: Avoiding reference of sub optimization
by JavaFan (Canon) on Mar 21, 2011 at 14:51 UTC
    Actually I'm blessing them into a dedicated class to set attributes, so subs with the same body can have different attributes. (like debug informations)
    Any reason why you aren't using a closure then? Which ought to give you exactly that, plus give you different references as preferred....
      Making them all closures is what I'm planning to do now.

      I was using an inside-out mechanism to set the attributes (a hash as class-variable with the subref as key) but know I'm thinking about closing over a hash %attribute at generation.

      So no practical problem, just asking for insight.

      Cheers Rolf

Re^3: Avoiding reference of sub optimization (notabug)
by tye (Sage) on Mar 21, 2011 at 14:47 UTC

    Doctor, it hurts when I do this.

    I vote against Perl hiding the fact that two code references are identical and increasing memory usage to do so in order to enable a dubious use of "code reference as object".

    - tye        

      I think everything outside your normal paths is per personal definition dubious.

      Cheers Rolf

      UPDATE: There is along list of "dubious" languages allowing the use of functions as objects. :)

        Perhaps you define "dubious" as "anything outside one's normal paths" (or my paths, if I take you literally), but I don't. You'll note that your proposed usage doesn't actually work. It failing to work is not caused because I haven't personally tried that failing experiment.

        In order for your usage to work, you'd have to break less dubious usages that are already working (as well as making Perl less efficient).

        Note that you can also make $object->() work via overloading of a less-dubious object.

        - tye