Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3391724
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:50:09+00:00 2026-05-18T03:50:09+00:00

my $var = Hello; my $Hello = Hi; my question is: how can I

  • 0
my $var = "Hello";
my $Hello = "Hi";

my question is: how can I substitute the value of $Hello in $var?
Here $var contains string “Hello” (there is a variable with same name $Hello). I am looking for a way to substitute it with the value of variable $Hello.
Please let me know how can I do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T03:50:10+00:00Added an answer on May 18, 2026 at 3:50 am

    This is discussed in Perl FAQ (7): How can I use a variable as a variable name?.

    In short, the answer is: Don’t do it. Use hashes instead.

    Longer answer:


    A MUCH better and simpler solution would have been to store the values in a hash:

    my %sayings = ("Hello" => "Hi");
    my $key = "Hello";
    my $var2 = $sayings{$key};
    

    You can also use eval:

    my $var = "Hello";
    my $Hello = "Hi";
    
    print "1.$var\n";
    
    my $var2 = eval "\$$var";
    
    print "2.$var2\n";
    

    As a last resort, you COULD use symbolic references. HOWEVER (as discussed in the FAQ linked above), they ONLY work on global variables and ONLY when `use strict ‘refs“ is not in effect – which it should always be for normal Perl development.

    ##############################################################
    # This works. But only without "use strict" and on a package (global) variable.
    ##############################################################
    no strict; # BOO! Bad coder!
    my $var = "Hello";
    $Hello = "Hi"; #Look, ma, GLOBAL VARIABLE! Bad Coder!
    
    print "1.$var\n";
    
    my $var2 = ${$var}; # You could use $$var shorthand
    
    print "2.$var2\n";
    
    # OUTPUT: 
    # 1. Hello
    # 2. Hi
    
    ##############################################################
    # I meant it about package (global variable)
    ##############################################################
    no strict;
    my $var = "Hello";
    $Hello = "GLOBAL value";
    my $Hello = "Hi";
    print "1.$var\n";
    my $var2 = ${$var}; # You could use $$var shorthand
    print "2.$var2\n";
    
    # OUTPUT - see how we don't get "Hi" on a second line
    # If we did not assign "GLOBAL Value" to $Hellp on line 3
    #    , second line would contain no value at all (e.g. "2.")
    #  1. Hello
    #  2. GLOBAL value
    
    ##############################################################
    # And, if you do what you SHOULDA done and used strict:
    ##############################################################
    use strict; # Much better!
    my $var = "Hello";
    my $Hello = "Hi"; 
    print "1.$var\n";
    my $var2 = ${$var}; # You could use $$var shorthand
    print "2.$var2\n";
    
    # OUTPUT: 
    # Global symbol "$Hello" requires explicit package name at line 4.
    # Execution aborted due to compilation errors.
    

    P.S. If you simply want to use the value of $Hello hardcoded variable, you can do $var2 = $Hello;, but I have a feeling you meant you want to use whichever variable’s name is contained in $var.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code as below: var s : String = hello world var xml
Given a string like the following in JavaScript var a = 'hello world\n\nbye world\n\nfoo\nbar\n\nfoo\nbaz\n\n';
Hello friends here's my code function dropItems(idOfDraggedItem,targetId,x,y) { var html = document.getElementById('dropContent').innerHTML; if(html.length<=0){ html.innerHTML='<img
Hello StackOverflow, I'd like to know if there is way to trigger an event
Hello everyone i have an question about firefox add-on: How i can get the
I'm trying to use a VARIABLE as a ATTRIBUTE value... I have : var
There is a similar question in here . In that question X happens to
I can not get environment at custom target shell. CMakeList.txt set( ENV{TEST_VAR} Hello )
Lets say I have this code: <?php class hello { var $greeting = hello;
var Dog = function() { var _instance = 'hello world'; return function() { console.log(this._instance);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.