I created this code a few days ago. It's pretends to be the constant module and overrides constants to what you like. It's mostly a proof-of-concept but you can try it.

# file: constant.pm package constant; use warnings; use strict; use File::Spec; my %Overrides; sub make_inc_hook { # Make sure we don't load this file... or we'll recurse infinitely + later. my ($path) = ( grep { -f $_ } map { File::Spec->catfile( $_, 'constant.pm' ) } grep { $_ ne q{.} } # avoids our directory @INC ) or die q{couldn't load constant.pm}; open my $fh, '<', $path or die "open: $!"; # Filter the real constant.pm, rename package to old_constant. my $source_filter = sub { return 0 unless defined $_; # edit: added 'defined' s/package constant;/package old_constant;/; return 1; }; # Returns an @INC hook. See perldoc -f require. return sub { my ($self, $file) = @_; return unless $file eq 'old_constant.pm'; return ($fh, $source_filter); }; } BEGIN { push @INC, make_inc_hook(); } # Preload/compile the original constant.pm use old_constant; sub import { my ($pkg) = caller; if ( ref $_[1] eq 'HASH' ) { my $param = $_[1]; $Overrides{ $_ } = $param->{ $_ } foreach keys %{ $param }; return; } my (undef, $name, $value) = @_; $value = $Overrides{ $name } if $Overrides{ $name }; use Data::Dumper; local $Data::Dumper::Terse = 1; eval qq{package $pkg; use old_constant '$name' => } . Dumper( $val +ue ); die if $@; return; } 1;
# file: A.pm package A; use strict; use constant VALUE => 1; sub func { print "Constant is ", VALUE, "\n"; } 1;
#!/usr/bin/perl # file: example.pl use warnings; use strict; use lib q{.}; # because constant.pm above is in the same dir... use constant { VALUE => 1_000_000 }; # this overrides VALUE use A; A->func();

edit: Run 'perl example.pl' and the output is: Constant is 1000000


In reply to Re: Overwriting a Constant by juster
in thread Overwriting a Constant by saintmike

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.