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 8323849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:51:02+00:00 2026-06-08T23:51:02+00:00

#!/usr/bin/perl my $var_a; $sub_a = a; $var_a = a; print ${var_.$sub_a},\n; $sub_b = b;

  • 0
#!/usr/bin/perl
my $var_a;
$sub_a = "a";
$var_a = "a";
print ${var_."$sub_a"},"\n";


$sub_b = "b";
$var_b = "b";
print ${var_."$sub_b"},"\n";

__DATA__

b

Why is b printed, but not a? This seems like very unexpected behaviour to me.

I’m trying to use a variable with a substituted name. In practice, I cannot just not declare the variable, since the assignment is being done in a forloop and thus has different lexical scope.

  • 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-06-08T23:51:03+00:00Added an answer on June 8, 2026 at 11:51 pm

    Please note that this has NOTHING to do with the fact that you are using variables to contain the name of other variable.

    The reason this doesn’t work is because ${"var_a"} construct in reality refers to a package level variable $main::var_a.

    Since $var_a is declared as a lexical variable, it’s a DIFFERENT identifyer, and therefore ${"var_a"} is undef.

    You can see that if you change my $var_a to our $var_a

    our $var_a="a";
    my $var_b="b";
    $var_c="c";
    
    print ${"var_a"},"\n";
    print ${"var_b"},"\n";
    print ${"var_c"},"\n";
    
    ######## RESULTS:
    a
    
    c
    

    As others noted, while there is a good explanation for why what you are trying to do doesn’t work, WHAT you are doing is likely the wrong approach. You should almost NEVER use this method unless there’s no better way; without your problem it’s not clear what the better way is but most likely would be a hash like TLP’s answer says.

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

Sidebar

Related Questions

I have run this Perl code: #!/usr/bin/perl print content-type: text/html \n\n; print Hello World.\n;
I googled about #!/usr/bin/perl , but I could not find any satisfactory answer. I
I have this script: #!/usr/bin/perl $LOGFILE = Soccer.txt; open(LOGFILE) or die(Could not open log
#!/usr/bin/perl print Content-type: text/html \n\n; print qq(<html> <head> <script type=text/javascript src=/jquery.js></script> <script> jQuery(document).ready(function(){ jQuery(#form_lang).submit(function(e){
I've got this simple Perl script: #! /usr/bin/perl -w use strict; use Data::Dumper; my
When I do this #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $s =
I have some simple Perl code: #!/usr/bin/perl use strict; # not in the OP,
When I write #!/usr/bin/perl -w use strict; while( <DATA> ) { print \n-------------------------\n\n; print;
This is my code #!/usr/bin/perl -T use CGI; use CGI::Carp qw(fatalsToBrowser); use CGI qw(:standard);
I have this code #!/usr/bin/perl -w use strict; use URI; use LWP::UserAgent; use Data::Dumper;

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.