On a number of occasions, I have read that using variable names for variable definitions is a bad thing.

So why would one want to use them? Why are they available? (rhetorical question)

While playing with Perl (in an effort to learn it), I devised a subroutine that I found to be very useful. It's more complex than that shown here, but it's the same basic idea.

So, maybe there's another way to do the same thing, but, you know... tmtowtdi.

# Example of using variable names for variables, # that I find useful. #------------- # user defined code # ------------ my $some_var_list = var_from_env_var("list"); my $some_var_source = var_from_env_var("source"); my $some_var_other = var_from_env_var("other"); print "list=> $some_var_list\n". "source=>$some_var_source\n". "other=> $some_var_other\n"; # ------------ # my module # ------------ sub var_from_env_var { # could use this, or some other complex method # of determining the default value. my $list_def = $ENV{"LIST_VAR"} || $ENV{"LIST_VAR_DEPRECIATED"} || "other_list"; my $source_def = $ENV{"SOURCE_VAR"} || $ENV{"SECONDARY_SOURCE"} || "other_source"; # is there another way to use a variable to point # to a variable other than eval? my $input = shift; my $result = eval "\$$input"."_def"; return $result if (defined ($result)); return $input; }

In reply to variables names used to define variables by Sandy

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.