Here's one that works. I'd be happy to replace it with something cleaner. C'mon PerlMonks, I know you can do it! Here's what I've got:

#!/usr/bin/perl -w use strict; sub is_constant { no strict 'refs'; my $name = shift; my $code = *{$name}{CODE}; # must have an empty prototype to be a constnat my $proto = prototype($name); return 0 unless defined $proto and not length $proto; # attempt to redefine - this will cause a warning for a # real constant that starts with "Constant" my $is_constant; local $SIG{__WARN__} = sub { $is_constant = 1 if $_[0] =~ /^Consta +nt/ }; eval { *{$name} = sub { "TEST" } }; # set it back eval { *{$name} = $code; }; # all done return $is_constant; } use Test::More tests => 6; use constant CONSTANT1 => 1; use constant CONSTANT2 => [ 'foo', 'bar' ]; sub CONSTANT3 () { '...' } sub NONCONSTANT1 () { $_[0]++ } sub NONCONSTANT2 ($) { "fooey" } sub NONCONSTANT3 { 1; } ok(is_constant("main::CONSTANT1")); ok(is_constant("main::CONSTANT2")); ok(is_constant("main::CONSTANT3")); ok(!is_constant("main::NONCONSTANT1")); ok(!is_constant("main::NONCONSTANT2")); ok(!is_constant("main::NONCONSTANT3"));

It passes my tests despite leaveing me with a not-so-clean feeling.

-sam


In reply to Re: Is it possible to distinguish contstants from normal subs? by samtregar
in thread Is it possible to distinguish contstants from normal subs? by samtregar

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.