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";
    }

You will have to take additional care if your variables don't contain
plain numbers. If they contain strings, you can't just join with
commatas. You then also have to enclose them in quotes. Also, I am
storing a string like

    @main::array1 = (1,2,3);

because it can later just be evaled (or do()ne) under strictures.

> Also the reverse: take a filename like "somename"
> and load it to an array @somename just by some
> concantation like @{'somename'}.

If you store it similarily to the above, a plain

    do 'somename';

should do.

> The @{'somename'} seems to work, but not with strict.

No, because it's a symbolic reference. The @{ *glob } trick is slightly
different in that you don't dereference a string.

> It seems like it should be easy, but it's not.

It's not so hard with a little bit of understanding for the
symbol-table. However, and that's the limitation, you can't thusly store
lexical (my()-) variables because they don't have an entry in the
symbol-table. In this case you need to walk the pads of the Perl
interpreter which one of the B:: modules could indeed help you with.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


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

Reply via email to