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

Hi I am new to perl so please help me even though my question is idiotic to you

#! /usr/bin/perl use constant VARS => {X => 20, Y => 20}; use constant EQ => [2*Y, -2*X*Y]; $x = VARS -> {X}; $y = VARS -> {Y}; print "Val of \$x is $x \n"; print "Val of \$y is $y \n"; $temp = EQ ->[0]; print "$temp \n"; print EQ->[1]."\n";

I get warnings even without -w saying : operator or Semicolon missing Unambiguous use of *

So can't I use already defined constants in other constants. Here, as you might have run the code, EQ->[0] or EQ->1 is not evaluated.

Appreciate any help..

Replies are listed 'Best First'.
Re: Use of Constants
by choroba (Cardinal) on Jul 24, 2012 at 13:50 UTC
    You have not defined X and Y, you have defined VARS->{X} and VARS->{Y}.
      Thanks for reply..

      You have brought my attention to something stupid i was doing.. but i guess this is what i call learning a new language..

      using separate use constants for each X and Y solves my problem and also use of VARS->X is correct.

      Thank You all.. I do not know if this forum has a rule of marking question as solved or something like that.. bit busy today... so have no time to read the rules..

      In that case someone can please mark this problem as "SOLVED"

        In that case someone can please mark this problem as "SOLVED"

        You mark the problem as 'SOLVED' by going away satisfied with a solution.

Re: Use of Constants
by nemesdani (Friar) on Jul 24, 2012 at 13:47 UTC
    Try to insert a space before the *-s :)


    I'm too lazy to be proud of being impatient.
      Thanks for reply.. inserting space does not make any difference in this case though..!
        Except warnings are gone..

        But the value is still zero..

Re: Use of Constants
by kcott (Archbishop) on Jul 25, 2012 at 01:40 UTC

    You appear to have a solution to your immediate problem.

    As you're new to Perl, I'll draw your attention to constant - CAVEATS. Not knowing that in certain circumstances you need to prepend +, or append (), to your constant names can be a source of problems that can be hard to track down.

    Readonly was suggested above. While this module addresses the use constant issues, it also has its drawbacks: see the CONS section in its documentation.

    Another alternative is Const::Fast. This addresses both the constant and Readonly issues but has its own CAVEATS.

    -- Ken

Re: Use of Constants
by 2teez (Vicar) on Jul 24, 2012 at 20:48 UTC
    Hi,

    Instead of use constant, why don't you try use Readonly like so:

    #! /usr/bin/perl use Readonly; Readonly my $vars => { X => 20, Y => 20 }; Readonly my $eq => [ 2 * Y, -2 * X * Y ]; $x = $vars->{X}; $y = $vars->{Y}; print "Val of \$x is $x \n"; # print 20 print "Val of \$y is $y \n"; # print 20 $temp = $eq->[0]; print "$temp \n"; # print 0 print $eq->[1] . "\n"; print 0
    However, to get desired print out for both
    print "$temp \n"; # and print $eq->[1] . "\n";
    You could could have:
    Readonly my $eq => [ 2 * $vars->{Y}, -2 * $vars->{X} * $vars->{Y} ]; ### instead of Readonly my $eq => [ 2 * Y, -2 * X * Y ];
    Then you have:
    print "$temp \n"; ## prints 40 # and print $eq->[1] . "\n"; ## prints -800