in reply to generating subroutines from array variables

You could do something like:

my @list = qw/foo bar baz/; foreach my $element (@list) { *{$element} = sub { $state{$element} = 1; }; *{"${element}_"} = sub { $state{$element} = 0; }; }

If you are using strict {insert standard admonition here} you'd need to use:

no strict 'refs';
in the loop, I believe.

On another note, without knowing just what your goal is, there is likely another XML module that might do more of what you need for you. A search at search.cpan.org for "XML" returns over 2000 matches. :)

UPDATE: Added the missing semi-colons. Thanks to esh for pointing out they were missing. :)

- Matt Riffle

Replies are listed 'Best First'.
Re: Re: generating subroutines from array variables
by esh (Pilgrim) on Aug 17, 2003 at 17:16 UTC

    Slight syntax correction to mattriff's cool code. You'll need to terminate the assignments with semicolons. (Well, this being Perl, you just need to separate them with semicolons, but let's pretend it's terminate so we don't run into problems the next time we add a statement).

    my @list = qw/foo bar baz/; foreach my $element (@list) { *{$element} = sub { $state{$element} = 1; }; *{"${element}_"} = sub { $state{$element} = 0; }; }

    -- Eric Hammond