It can be done, very easily, but it can lead to problems.
sub test { print "nothing\n"; } my $var = 'test'; &$var; # will call the sub, if you are not using strict.
This code will issue a run-time error if you are using strict.
However, if you do the same thing with objects, even with use strict, you can get away with it.
(This is one of the cases where Perl gives you splendid opportunities of shooting yourself in the foot.)
#!/usr/bin/perl -w use strict; package Camel; sub new { my $class = shift; my $self = bless {name =>'Jack Camel'}, $class; } sub who { my $self = shift; return $self->{name} } sub what { my $self = shift; return "a camel, can't you see that?"; } package main; my $camel = new Camel; for ('who', 'what') { print $_, "?\t", $camel->$_, "\n"; }
But you will be in trouble if you add an unmatched item to the list of variables, or you misspell one of them. Say, using ('what','woh'), the error will kick off at run-time only.
I very much agree with Juerd's advice. Don't do it.

In reply to Re: Variable variable? by rnahi
in thread Variable variable? by Ryszard

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.