Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: dynamically generated array variable

by ikegami (Patriarch)
on Jan 02, 2007 at 20:36 UTC ( [id://592638]=note: print w/replies, xml ) Need Help??


in reply to dynamically generated array variable

Your config file parser simply creates those variables instead of allowing to query them? To work with this bad design, you'll need to use symbolic references.

my $hostname = 'hosta'; my @host = do { no strict 'refs'; @$hostname };

Or if a reference is fine:

my $hostname = 'hosta'; my $host = do { no strict 'refs'; \@$hostname };

Update: I forgot to mention the above only works if @hosta is a package variable, not a my variable.

Replies are listed 'Best First'.
Re^2: dynamically generated array variable
by perlknight (Pilgrim) on Jan 02, 2007 at 21:03 UTC
    I did this and it works:
    #use strict; $host = qw(@hosta); foreach $cnf (eval($host) { }
    . is there a way to specify it so that I can use strict? Thanks.

      Yes. I already provided that. use strict does not give an error with the code in my previous post.

      use strict; use warnings; # ... Load config file ... my $hostname = 'hosta'; my @host = do { no strict 'refs'; @$hostname }; foreach my $cnf (@host) { ... }

      By the way, why are you using qw(...) to initialize a scalar. Don't you want
      $host = q(...);
      or the equivalent but more readable
      $host = '...';

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-25 20:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found