Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

constant variable using scaler variable

by luckypower (Beadle)
on Oct 01, 2008 at 08:55 UTC ( [id://714743]=perlquestion: print w/replies, xml ) Need Help??

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

hellow,

use constant A1 => "abc"; my $var = "A1";

now i want to print "abc" usig $var...

Thanks

Replies are listed 'Best First'.
Re: constant variable using scaler variable
by lamp (Chaplain) on Oct 01, 2008 at 09:00 UTC
    The following code will print 'abc'.
    use constant A1 => "abc"; my $var = A1; # remove double quotes print $var;
    Please go through constant for more information.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: constant variable using scaler variable
by Corion (Patriarch) on Oct 01, 2008 at 09:46 UTC

    Alternatively, set up a hash with your error messages:

    use strict; use constant ERR => 'Some error happened.'; use constant OK => 'All is fine.'; my %messages = ( 'ERR' => ERR(), 'OK' => OK(), ); my $res = some_func(); print $messages{ $res }, "\n";

    Alternatively, you can invite the demons of symbolic references into your house, but they will eat your children:

    use strict; use constant ERR => 'Some error happened.'; use constant OK => 'All is fine.'; my $res = some_func(); { no strict 'refs'; print &$res(), "\n"; };
Re: constant variable using scaler variable
by rovf (Priest) on Oct 01, 2008 at 10:11 UTC

    I guess you have a good reason to say $var="A1" instead of $var=A1, so

    print(eval($var));
    would be one way to solve the problem.

    -- 
    Ronald Fischer <ynnor@mm.st>
      That's one way to solve the problem, in the same way that using a flamethrower to light a cigarette is "one way to solve the problem". Not good. Dangerous. Expensive. The other solutions in this thread are far better.

        I dunno, I think using a flamethrower to light a cigarette is a great way to solve the problem of being addicted to cigarettes...

        /me ducks, runs, and hides

        But a pastry torch is a fine way to light a cigar. ;)


        Revolution. Today, 3 O'Clock. Meet behind the monkey bars.

        I would love to change the world, but they won't give me the source code

    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://714743]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-25 08:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found