in reply to Constant subroutines thoughts

Maybe for discussion rather than revelation, but why use constants as sub-routines?

Constant functions can be used to optimize away parts of your program, are just as fast as regular scalars, and they can't be modified "accidentally". You can create these automatically with use constant FOO => 'bar';

Regular variables can be created faster (no module import required), used inside strings without special handling, and are often more "user friendly" in this regard.

There was a module called Tie::Const which created "read-only" variables using, of course, tie. The end result is slower scalars, just so you can put them in strings directly and have the piece of mind that they aren't being modified. This seems like a sub-optimal solution, and indeed, the module has been decommissioned.

I'd rather see something like this in the future:
my const $FOO = 'bar'; # This is preferable, excuse my C++ my $FOO : const = 'bar'; # This is "more Perly"
const is really useful when applied correctly, but Perl has no such mechanism. This is a huge drawback when it comes to optimization.

Replies are listed 'Best First'.
Re: Re: Constant subroutines thoughts
by Juerd (Abbot) on Jun 09, 2002 at 09:41 UTC

    my $FOO : const = 'bar'; # This is "more Perly"

    my $foo is const = 'bar';
    According to Exegesis 3.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

Re: Re: Constant subroutines thoughts
by Courage (Parson) on Jun 09, 2002 at 06:38 UTC
    I see your point, and agree with you completely.

    Why I used constant subroutines instead of doing use constant FOO => 'bar';? Because, as I saw everywhere else sub SOME_CONST{0}, and only now I discovered details about this.

    Additionally I want to note that use constant FOO => 'bar'; internally does things similar to sub FOO(){'bar'} - I checked this right now.

    Most important point of my initial message - that my places in programs, such as do{'a lot of debug stuff'} if DEBUG were NOT optimized away, whereas adding a prototype '()' to 'sub DEBUG' makes them to be optimized away!

    Courage, the Cowardly Dog.
    PS. Something fishy is going on there, or my name is Vadim Konovalov. And it's not.

      It would be nice if you could declare inline functions explicitly rather than implicitly using this empty bracket "trick". I had no idea that Perl did this sort of magic for you under special circumstances. Maybe I haven't read enough of the latest Camel Book, because the older one didn't seem to make much of a big deal about it.

      I think the reason I was so befuddled by it in the first place is because it didn't make any sense that Perl would do that sort of thing, I mean, other languages you've got to tell them to do this for you.
        Seems like I fallen into the same sort of confusion as you did. Anyway, as I mentioned, this is documented in "perlsub.pod", section "Constant Functions", and, if you're not pleased by bracket trick, then use "constant" module which does the same thing behind the scenes, and will be more portable just in case such mechanics will change...

        Courage, the Cowardly Dog.
        PS. Something fishy is going on there, or my name is Vadim Konovalov. And it's not.