Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: constants wont optimize

by patcat88 (Deacon)
on Jul 10, 2011 at 18:09 UTC ( #913615=note: print w/replies, xml ) Need Help??


in reply to Re: constants wont optimize
in thread constants wont optimize

I am trying to NOT use constant. use if eval { require mod; 1 }, qw' constant haveMod 1 '; can't get very complicated, cosmetically, For example, during running/compiling of the require of a use for a PM, probing the capabilities of the current OS/perl installation, including, calling XSUBs, setting the constant subs, then defining the subs that rely on those constant subs and having the perl compiler delete the subroutine branches for never to be used code path.

I looked through the source of constant.pm, its doing something black magic, "mro::method_changed_in($classname)" and Internals::SvREADONLY to make its constant subs. Not the public Constant Functions way.

Replies are listed 'Best First'.
Re^3: constants wont optimize
by ikegami (Pope) on Jul 10, 2011 at 18:12 UTC

    Actually, as you've found out, your way is the complicated one.

    Simple:

    use constant haveMod => eval { require mod; };

    And for the non-trivial:

    my $haveMod; BEGIN { ...[ something complicated ]... $haveMod = ...; } use constant haveMod => $haveMod;
    or even
    BEGIN { ...[ something complicated ]... require constant; import constant haveMod => ...; }

    PS - require always returns true, so the "1" is redundant.

      I can't call "require". I am sniffing to see what PMs were loaded before the "use" that loads my PM. Its so my PM will integrate with other PMs loaded BEFORE my PM. Sort of like threads and threads::shared.

        I can't call "require".

        You seem to have lost track of what you said because you're the one who used require. You said

        use if eval { require mod; 1 }, qw' constant haveMod 1 ';

        is ugly because constant is being used. I pointed out that it's simply not true. Not only does that code not work, but it can be written as

        use constant haveMod => eval { require mod; };

        I am sniffing to see what PMs were loaded before the "use" that loads my PM.

        Asked and answered. Or if you want to check for multiple modules,

        use constant { map { no strict 'refs'; ( my $constant = $_ ) =~ s/:://g; "have$constant" => ${"${_}::VERSION"} } qw( Foo::Mod Bar::Mod ); };

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://913615]
help
Chatterbox?
[shmem]: but the results of both operations are different.
[shmem]: result := the discarded result (what's leftmost of the statement)
[shmem]: push returns the number of elements in @foo, the assignment returns all the elements in the (renewed) @foo
[Lady_Aleena]: Sanity check please? push @line, ref($list_addition ) eq 'ARRAY' ? @$list_addition : $list_addiction; # so I don't have the change existing uses of the sub.
[shmem]: $list_addiction should be $list_addition
Discipulus loose the tozzetto party!
[shmem]: .oO( addition of cookies for addiction to cookies )
[Lady_Aleena]: Other than my typos shmem. 8)
[shmem]: otherwise fine
[Discipulus]: tozzetti & vinsanto

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2017-04-27 11:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (503 votes). Check out past polls.