What Anony is saying is that you don't need "arbitrarily named scalars" - you need arrays.

Example 1

N_scalars("foo",5); --yields-- $foo_1 = ''; $foo_2 = ''; $foo_3 = ''; $foo_4 = ''; $foo_5 = '';
Solution: use an array!
my @foo;
Whether you need 5 foos, or 50 foos, or 5,000,000 foos, @foo will hold them (as long as you have the necessary memory available). The only price you have to pay to use an array is remember that you start counting them at zero, not one. But most of the time you won't even have to count them, or even care how many you have:
open FH, 'foo.txt' or die "$!: can't open foo.txt\n"; my @file = <FH>; close FH;
Now @file contains as many lines as are in the file foo.txt (as long as that file is the same directory that you ran that code in). Regardless of how many lines there are, these snippets will print them all:
foreach my $line (@file) { print $line; }
foreach (@file) { print $_; }
print foreach @file;
print @file;
for my $i (0..$#file) { print $file[$i]; }
for (0..$#file) { print $file[$_]; }
print for @file;
print @file;

Example 2

arb_scalars("alfa","bravo","charlie"); --yields-- $alpha = ''; $bravo = ''; $charlie = '';
Solution: use an array! (an associative array)
my %word = ( alpha => 'a', bravo => 'b', charlie => 'c', );
Now i can find out what character belongs to 'charlie' like so:
print $word{'charlie'};
You don't need to create an arbitrary variable named $charlie. As a matter of fact, imagine having to pass 26 variables to a subroutine ... wouldn't you rather pass one hash instead? I would.

So, now that you know why you don't need 'arbitrary variables' in Perl, and you know that you can have them anyway ... why should you not use them? Well, my number one reason for not using them is because i think they are more trouble than they are worth. (passing one var vesus 26 ...)

Interestingly enough, PHP allows you to use 'arbitrary variables'. extract allows you to turn a hash's keys into varibles and compact will take a list of variables and create a new hash from them. You can do this in Perl1, but if you use strict in your script, then you have to declare the 'arbitrary variables' before you called explode (if such a sub existed in Perl). Having to declare them ahead of time isn't very 'arbitrary', and it is a major nuisance if you don't even need to know what they will be named.

1 I think that an explode for Perl would be pure abuse, but compact might be useful for creating objects with arbitrary attributes ... surely this is a already CPAN module ... ;)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to (jeffa) Re: create arbitrarily named scalars by jeffa
in thread 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.