I want to create a scalar named by a string. So, given string "foo" I end up with scalar $foo. The idea is to concatenate strings "foo" and "bar" within a subroutine so as to create the name of arbitrary scalar $foobar.

Two examples, reduced to absurdity, follow...

EXAMPLE 1:

sub N_scalars { my ($prefix, $qty) = @_; ...unknown magic here... }
...such that if I later call...
N_scalars("foo",5);
...it initializes five scalars as if I had done...
$foo_1 = ''; $foo_2 = ''; $foo_3 = ''; $foo_4 = ''; $foo_5 = '';
EXAMPLE 2:
sub arb_scalars { foreach $foo (@_) { ...unknown magic here... } }
...so that if I later call...
arb_scalars("alfa","bravo","charlie");
...it works as if I had done...
$alpha = ''; $bravo = ''; $charlie = '';
I know that looks ridiculous, but it is a useful way to automate other subs. I can do it in PostScript simply by converting a string to a variable. The real world use I am shooting for is more complicated. But if the above could be done, then I could also do what it is I have in mind. I could get around it with references, but the result would be unreadible a hundred lines further down the file. To use named scalars would be much preferable. But my sub, to be flexible, ought be fed a list of strings from which to create its vars.

I'm guessing it might be impossible. Does anyone know?

Thanks,

Gan

update (broquaint): dropped <pre> tags and added formatting


In reply to create arbitrarily named scalars by aplonis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.