in reply to Re^7: Constant subroutine main::C redefined
in thread Constant subroutine main::C redefined

Tried that also :(

C:\test>perl -wE"no warnings 'redefine';sub FOO(){23};sub FOO(){42 if +$];};say$];" Constant subroutine FOO redefined at -e line 1. 5.014002

Dropping the prototype isn't an option either because the constant getting redefined is a very rare occurence (but one that must be catered for), and I don't want to lose the benefits of compile time optimisations for the majority of cases.

If a constant sub is redefined to be the same value, it doesn't cause the warning:

C:\test>perl -wE"no warnings 'redefine';sub FOO(){23};sub FOO(){23};sa +y$];" 5.014002

which will be the majority of cases, but I cannot predict when the edge case might occur so I have to try the redefinition in all cases.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

RIP Neil Armstrong

/div

Replies are listed 'Best First'.
Re^9: Constant subroutine main::C redefined
by tinita (Parson) on Sep 10, 2012 at 13:50 UTC
    so what about delete $main::{C}? did you try?

      It works in main, but the target won't always be main. Still trying to get the syntax right for an arbitrary package.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      RIP Neil Armstrong

      /divp align=right
        no strict "refs"; my $p = "main"; delete ${ $p."::" }{C};
        String eval for sub definition might be helpful here, though I don't know if that messes with the optimization. Minus that,
        sub FOO(){23}; BEGIN{eval sprintf 'delete $%s::{FOO}', __PACKAGE__} sub FOO(){42};
        might hit your need.

        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re^9: Constant subroutine main::C redefined
by tobyink (Canon) on Sep 10, 2012 at 21:47 UTC

    You need the if $] in the first definition of FOO. That should prevent it being constant-folded.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      That should prevent it being constant-folded.

      It would. Which is why I cannot use that solution.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      RIP Neil Armstrong

        Well, if you want constant folding, then you'll have to deal with the warning being generated. It's a mandatory warning.

        Of course, just because the warning will be generated; doesn't mean it has to be displayed. $SIG{__WARN__} can catch and hide it.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'