Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Weird (?) behavior with constants under special conditions

by ceo (Acolyte)
on Jan 31, 2013 at 14:04 UTC ( #1016322=perlquestion: print w/ replies, xml ) Need Help??
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?

Comment on Weird (?) behavior with constants under special conditions
Select or Download Code
Re: Weird (?) behavior with constants under special conditions
by choroba (Abbot) 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 (Monsignor) on Jan 31, 2013 at 14:30 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2014-09-17 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (100 votes), past polls