in reply to Re: Re: subroutine constants
in thread subroutine constants

I don't know that there is much real difference other than one will fail if you try to pass it arguments and one won't (I think)... in general though, I believe use Constants is better since it does some behind the scenes work and is done compile time... if you are going to do it the hard way, probably sub BUFFER () { 2048 } is the better way to go.

                - Ant
                - Some of my best work - Fish Dinner

Replies are listed 'Best First'.
(tye)Re: subroutine constants
by tye (Sage) on Sep 21, 2001 at 01:00 UTC

    Ah, but: sub BUFFER() { 2048 } is also a compile-time construct.

    The () means that uses of BUFFER can be optimized away so that the code gets compiled as if you said 2048 directly. Without the (), each time an instance of BUFFER is encountered at run time, a Perl subroutine call has to be executed.

    I doubt you'd be able to notice a speed difference between the two without trying really hard. But it also makes the compiled code a little smaller and declares your intention to not change this definition at run time.

            - tye (but my friends call me "Tye")
      Ooooh... ahhhh... teach me oh great one ;)

      I suppose if you had a few thousand constants you would notice it... :)

                      - Ant
                      - Some of my best work - Fish Dinner