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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:13:13+00:00 2026-05-16T02:13:13+00:00

Given a typeglob, how can I find which types are actually defined? In my

  • 0

Given a typeglob, how can I find which types are actually defined?

In my application, we user PERL as a simple configuration format.
I’d like to require() the user config file, then be able to see which variables are defined, as well as what types they are.

Code: (questionable quality advisory)

#!/usr/bin/env perl

use strict;
use warnings;

my %before = %main::;
require "/path/to/my.config";
my %after = %main::;

foreach my $key (sort keys %after) {
    next if exists $before{$symbol}; 

    local *myglob = $after{$symbol};
    #the SCALAR glob is always defined, so we check the value instead
    if ( defined ${ *myglob{SCALAR} } ) {
        my $val = ${ *myglob{SCALAR} };
        print "\$$symbol = '".$val."'\n" ;
    }
    if ( defined *myglob{ARRAY} ) {
        my @val = @{ *myglob{ARRAY} };
        print "\@$symbol = ( '". join("', '", @val) . "' )\n" ;
    }
    if ( defined *myglob{HASH} ) {
        my %val = %{ *myglob{HASH} };
        print "\%$symbol = ( ";
        while(  my ($key, $val) = each %val )  {
            print "$key=>'$val', ";
        }
        print ")\n" ;
    }
}

my.config:

@A = ( a, b, c );
%B = ( b=>'bee' );
$C = 'see';

output:

@A = ( 'a', 'b', 'c' )
%B = ( b=>'bee', )
$C = 'see'
$_<my.config = 'my.config'
  • 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-16T02:13:14+00:00Added an answer on May 16, 2026 at 2:13 am

    In the fully general case, you can’t do what you want thanks to the following excerpt from perlref:

    *foo{THING} returns undef if that particular THING hasn’t been used yet, except in the case of scalars. *foo{SCALAR} returns a reference to an anonymous scalar if $foo hasn’t been used yet. This might change in a future release.

    But if you’re willing to accept the restriction that any scalar must have a defined value to be detected, then you might use code such as

    #! /usr/bin/perl
    
    use strict;
    use warnings;
    
    open my $fh, "<", \$_;  # get DynaLoader out of the way
    
    my %before = %main::;
    require "my.config";
    my %after = %main::;
    
    foreach my $name (sort keys %after) {
      unless (exists $before{$name}) {
        no strict 'refs';
        my $glob = $after{$name};
        print "\$$name\n"             if defined ${ *{$glob}{SCALAR} };
        print "\@$name\n"             if defined    *{$glob}{ARRAY};
        print "%$name\n"              if defined    *{$glob}{HASH};
        print "&$name\n"              if defined    *{$glob}{CODE};
        print "$name (format)\n"      if defined    *{$glob}{FORMAT};
        print "$name (filehandle)\n"  if defined    *{$glob}{IO};
      }
    }
    

    will get you there.

    With my.config of

    $JACKPOT = 3_756_788;
    $YOU_CANT_SEE_ME = undef;
    
    @OPTIONS = qw/ apple cherries bar orange lemon /;
    
    %CREDITS = (1 => 1, 5 => 6, 10 => 15);
    
    sub is_jackpot {
      local $" = ""; # " fix Stack Overflow highlighting
      "@_[0,1,2]" eq "barbarbar";
    }
    
    open FH, "<", \$JACKPOT;
    
    format WinMessage =
    You win!
    .
    

    the output is

    %CREDITS
    FH (filehandle)
    $JACKPOT
    @OPTIONS
    WinMessage (format)
    &is_jackpot

    Printing the names takes a little work, but we can use the Data::Dumper module to take part of the burden. The front matter is similar:

    #! /usr/bin/perl
    
    use warnings;
    use strict;
    
    use Data::Dumper;
    sub _dump {
      my($ref) = @_;
      local $Data::Dumper::Indent = 0;
      local $Data::Dumper::Terse  = 1;
      scalar Dumper $ref;
    }
    
    open my $fh, "<", \$_;  # get DynaLoader out of the way
    
    my %before = %main::;
    require "my.config";
    my %after = %main::;
    

    We need to dump the various slots slightly differently and in each case remove the trappings of references:

    my %dump = (
      SCALAR => sub {
        my($ref,$name) = @_;
        return unless defined $$ref;
        "\$$name = " . substr _dump($ref), 1;
      },
    
      ARRAY => sub {
        my($ref,$name) = @_;
        return unless defined $ref;
        for ("\@$name = " . _dump $ref) {
          s/= \[/= (/;
          s/\]$/)/;
          return $_;
        }
      },
    
      HASH => sub {
        my($ref,$name) = @_;
        return unless defined $ref;
        for ("%$name = " . _dump $ref) {
          s/= \{/= (/;
          s/\}$/)/;
          return $_;
        }
      },
    );
    

    Finally, we loop over the set-difference between %before and %after:

    foreach my $name (sort keys %after) {
      unless (exists $before{$name}) {
        no strict 'refs';
        my $glob = $after{$name};
        foreach my $slot (keys %dump) {
          my $var = $dump{$slot}(*{$glob}{$slot},$name);
          print $var, "\n" if defined $var;
        }
      }
    }
    

    Using the my.config from your question, the output is

    $ ./prog.pl 
    @A = ('a','b','c')
    %B = ('b' => 'bee')
    $C = 'see'
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.