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

  • Home
  • SEARCH
  • 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 612645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:54:58+00:00 2026-05-13T17:54:58+00:00

In Perl, is it possible to create a global variable based on a string?

  • 0

In Perl, is it possible to create a global variable based on a string?

E.g., if I had a function like:

sub create_glob_var {
    my ($glob_var_str) = @_;
    # something like this ( but not a hash access).
    our ${$glob_var_str};
};

and I called it like:

create_glob_var( "bar" );

How could I modify create_glob_var to actually create a global variable called $bar?

My project is using perl 5.8.5.

EDIT

The following doesn’t work:

use strict;
BEGIN {
  sub create_glob_var {
    my ($glob_var_str) = @_;
    no strict 'refs';
    $$glob_var_str = undef;  # or whatever you want to set it to
  }

  create_glob_var("bah");
};

$bah = "blah";

Produces:

Variable "$bah" is not imported at /nfs/pdx/home/rbroger1/tmp2.pl line 12.
Global symbol "$bah" requires explicit package name at /nfs/pdx/home/rbroger1/tmp2.pl line 12.
Execution of /nfs/pdx/home/rbroger1/tmp2.pl aborted due to compilation errors.

NOTE I realize that using global variables causes ozone depletion and male pattern baldness. I’m trying to clean up some legacy code that is already completely infected with the use of global variables. One refactor at a time…

  • 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-13T17:54:58+00:00Added an answer on May 13, 2026 at 5:54 pm

    If you are trying to clean up old code, you can write a module which exports the required variable(s). Every time you feel the need to invoke create_glob_var, instead add a variable to this package and put that in the import list.

    This will help you keep track of what is going on and how variables are being used.

    package MyVars;
    
    use strict; use warnings;
    
    use Exporter 'import';
    
    our($x, %y, @z);
    
    our @EXPORT_OK = qw( $x %y @z );
    

    The script:

    #!/usr/bin/perl
    
    use strict;use warnings;
    
    use MyVars qw( $x %y @z );
    
    $x = 'test';
    %y = (a => 1, b => 2);
    @z = qw( a b c);
    
    use Data::Dumper;
    print Dumper \($x, %y, @z);
    

    Output:

    $VAR1 = \'test';
    $VAR2 = {
              'a' => 1,
              'b' => 2
            };
    $VAR3 = [
              'a',
              'b',
              'c'
            ];
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to do something like this: var s:String = format(%20d %-10s %s,
I know it is possible to create classes/objects in Perl using the bless function
I know this is possible in Perl, but I was wondering if this can
Possible Duplicate: How do I perform a Perl substitution on a string while keeping
Possible Duplicate: How do I create a directory and parent directories in one Perl
I'd like to connect to a Hadoop-based Hive datastore using Perl. Hive allows connection
Possible Duplicate: How do you create a Perl module? I have the script that
Possible Duplicate: How do I create a hash of hashes in Perl? I need
Possible Duplicate: How to replace a string in an existing file in Perl? I
Is it possible to have a Perl script run shell aliases? I am running

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.