Rob Dixon wrote:
> Tassilo Von Parseval wrote:
> > On Mon, Jun 02, 2003 at 07:53:06AM -0400 zentara wrote:
> >
> > > This one is puzzling me.
> > > I know it's in the faq, to not use variables for variable
> > > naming, but I find it odd that I can't get a "stringified" form
> > > of a variable name, maybe from the symbol table? Or from the B
> > > line of modules?
> > >
> > > Say I have an array like:
> > >
> > > @somename = (1,2,3,4,5);
> > >
> > > and I want to write that array to a file, but I
> > > want the file named automatically by just
> > > dropping the @ off of the @somename.
> > > How would you go about doing it? Plus,
> > > I would like to be using strict.
> >
> > This can be done, even with strictures enabled. @somename has an
> > entry in the symbol-table if it is a package variable. The entry
> > looks like
> >
> >     'somename' => *::somename,
> >
> > So under the key 'somename' you have a glob as corresponding
> > value. A glob has several slots, one of them the ARRAY slot which
> > you will automatically get when you use @{ } for dereferencing:
> >
> >     use strict;
> >     @main::somename = qw(a b c);
> >     ...
> >     # and now get the content of @somename
> >     my @values = @{ $::{somename} };
> >
> > I think from that it should be obvious how you store it in a file
> > [untested]:
> >
> >     for my $var ( qw/array1 array2/ ) {
> >         my @values = @{ $::{ $var } };
> >         my $valstring = join "," @values;
> >         print FILE "[EMAIL PROTECTED]::$var = ($valstring)\n";
> >     }
> >
>
> But very few people will understand how this works and, worse,
> what it is doing. Far better, I think is
>
>   my @values = do { no strict 'vars'; @$var };

Apologies, that'll be

  my @values = do { no strict 'refs'; @$var };

Rob






-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to