Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Overwriting a Constant

by saintmike (Vicar)
on May 05, 2010 at 02:10 UTC ( #838429=perlquestion: print w/ replies, xml ) Need Help??
saintmike has asked for the wisdom of the Perl Monks concerning the following question:

From the main package, I would like to overwrite a module's
use constant VALUE => 1;
by a different value for nefarious purposes. I tried
package A; use strict; use constant VALUE => 1; sub func { print "Constant is ", VALUE, "\n"; } package main; use strict; no strict 'refs'; *{"A::VALUE"} = sub () { 2 }; A->func();
but this still prints "1" and not "2". I must be missing something obvious, or is the prototype playing games with me?

Comment on Overwriting a Constant
Select or Download Code
Re: Overwriting a Constant
by Anonymous Monk on May 05, 2010 at 02:36 UTC
    Its compile time versus runtime. By the time your A::VALUE is encountered, sub func already substituted 1 for VALUE.
      B::Deparse shows expansion
      $ perl junk Constant subroutine A::VALUE redefined at junk line 12. Constant is 1 $ perl -MO=Deparse junk package A; sub BEGIN { require strict; do { 'strict'->import }; } use constant ('VALUE', 1); sub func { use strict 'refs'; print 'Constant is ', 1, "\n"; } package main; sub BEGIN { use strict 'refs'; require strict; do { 'strict'->import }; } *{'A::VALUE';} = sub () { 2 } ; 'A'->func; junk syntax OK
      See, it shows     print 'Constant is ', 1, "\n"; not     print 'Constant is ', VALUE, "\n";
Re: Overwriting a Constant
by juster (Friar) on May 05, 2010 at 05:12 UTC

    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

Re: Overwriting a Constant
by sflitman (Hermit) on May 05, 2010 at 05:37 UTC
    Why exactly do you want to do this? I tried to make it happen using Scalar::Readonly, but just turning off the readonly flag isn't enough. I then tried to do it by exporting VALUE from package A (now in another file, so it could be used by main), and it cannot be subverted locally either, even if using readonly_off.

    SSF

Re: Overwriting a Constant
by biohisham (Priest) on May 05, 2010 at 18:56 UTC
    In addition to juster's Re: Overwriting a Constant, probably the closest you can get is via simulative (re)setting of constants from the command line by using Getopt::constant, you provide default values - for constants you use (these values can be scalars, lists or even coderefs) - upon 'use'ing Getopt::constant, then from the command line these constants can be made to look like having been assigned new values or used in their original definitions.

    Interesting to note is that Getopt::constant does prefix such constants too and this serves to distinguish them by providing a label before the constant name, you can pass empty-string to the ':prefix' label too.

    #SYNOPSIS from Getopt::constant document... # Assuming @ARGV is: ('-foo=9,8,7', '-bar', 'wakawaka.txt') use Getopt::constant ( ':prefix' => 'C_', #Can be null too 'foo' => [3,5], 'bar' => 0, ':usage' => "Usage: thingamabob -foo=one,two,three : fooey on these items -bar : enable barriness ", ); # @ARGV is now 'wakawaka.txt', and you've now got # a constant C_foo with value (9,8,7) # and a constant C_bar with value 1

    N.B: Nefarious purposes can backfire in a hairy way so 'plot' with caution...


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://838429]
Approved by AnomalousMonk
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2014-11-29 07:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (204 votes), past polls