http://www.perlmonks.org?node_id=1016322

ceo has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone,
I had no chance of getting PPI installed in our environment, so I wrote a small perl-parser which does kind of okay in most cases we got here. Of course, its not as mighty as PPI, but that isnt neccessary anyway. But that isnt the topic, just somekind of bad introduction.
So, I just stumbled upon this:
You can say, let's say...
use constant g => 30;
and "print g" will print "30". Allright...
So, what if you are trying to print g divided by 5?
print (g/5)
prints 6, so, it works fine.
But what if you are going to try define another constant, named s, and try to print it, divided by 5, divided by 20, divided by g?
print (s/5/20/g)
That doesnt work. It always tries to do a substition regex on $_, and I, of course, understand why.
But... is there no way to get the result of s divided by 5 divided by 20 divided by g, when s and g are constants? And is that really wanted?

Replies are listed 'Best First'.
Re: Weird (?) behavior with constants under special conditions
by choroba (Cardinal) on Jan 31, 2013 at 14:07 UTC
    Of course there is a way to get the result you want:
    print &s/5/20/g;
    As you can see, it is not wise to use operator names for constants.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      ... it is not wise to use operator names for constants.

      ceo: ... or for functions, which is what constants are:

      >perl -wMstrict -le "sub tr (&$) { $_[0]->($_[1]) } sub trx (&$) { $_[0]->($_[1]) } ;; $_ = 'foo'; print 'A: ', tr{ print qq{tr match: $_[0]} }/foo/; print 'B: ', trx{ print qq{trx match: $_[0]} }/foo/; " A: 0 trx match: 1 B: 1
Re: Weird (?) behavior with constants under special conditions
by Athanasius (Archbishop) on Jan 31, 2013 at 14:30 UTC