When the person above you said 'inline', I don't think he was referring to Inline::C or anything like that. Rather, that the Perl compiler knows to optimize "constant" subroutines at compile-time and replace its subroutine/stack-calling stuff with... well, constant values

As an example, I created a program around the "Foo" package example we're using here, and I expanded the "Foo" package as well. Here it is:

#!/usr/bin/perl -w

use strict;

my $foo = Foo->new();

$foo->setBar('10');

print "\$foo's 'bar' value is = " . $foo->getBar() . "\n";

exit 0;

package Foo;

use constant BAR      => 1;
use constant BAZ      => 2;
use constant SO_ON    => 3;
use constant SO_FORTH => 4;

sub new {
    return bless ([], +shift);
}

sub getBar {
    my $self = shift;
    $self->BAR;
}

sub setBar {
    my $self = shift;
    $self->BAR = +shift;
}

Now, look at what this looks like when I run it through B::Deparse

rdice@tanru:~$ perl -MO=Deparse,-uFoo constants.pl
my $foo = 'Foo'->new;
$foo->setBar('10');
print q($foo's 'bar' value is = ) . $foo->getBar . "\n";
exit 0;
sub Foo::BAR () {
    package constant;
    $scalar;
}
sub Foo::BAZ () {
    package constant;
    $scalar;
}
sub Foo::SO_ON () {
    package constant;
    $scalar;
}
sub Foo::SO_FORTH () {
    package constant;
    $scalar;
}
sub Foo::new {
    package Foo;
    return bless([], shift @_);
}
sub Foo::getBar {
    package Foo;
    my $self = shift @_;
    $$self1;
}
sub Foo::setBar {
    package Foo;
    my $self = shift @_;
    $$self1 = shift @_;
}
constants.pl syntax OK

This is a reflection of how Perl sees this program after compilation and optimization. Notice that there is NO subroutine call within sub Foo::getBar or sub Foo::setBar with regards to the BAR 'constant', which is really a subroutine, but has been optimized back to being a constant -- namely, "1".

Cheers,
Richard


In reply to Re: Re^3: %hash (@array && use constant) in Modules by Dice
in thread %hash (@array && use constant) in Modules by abaxaba

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.