0

I am trying to insert a variable $font1 below. The variable $font1 is the name of the font e.g. arial. I really want it to return $arial (or whatever $font is) as a variable.

When $arial is called, it gets the arial.ttf from a folder on my server via get_fonts.pl.

I have tried everything \$$font '"$font"' and every possible variation of that.

I even tried if ($font=="arial"){$font = a ton of different attempts;}

do "get_fonts.pl";
&GetFonts($im);

foreach $key (keys %ENV) {
  if($key !~ /[A-Z]/){
    if ($key="sometext") {
      $text="$ENV{'typetext'}";
      $color="$ENV{'typecolor'}";
      $font="$ENV{'typefont'}";
      $size="$ENV{'typesize'}";

      $string = qq~ $text ~;
      $gd_text = GD::Text->new() or die GD::Text::error();
      $gd_text->set_font($arialb, $size) or die $gd_text->error;
      $gd_text->set_text($string);
      my ($w, $h) = $gd_text->get('width', 'height');
      $y1 = (300 / 6);
      if ($w <= 380) {
        $x1 = ((400 / 2) - ($w / 2));
        $im->stringFT("$blue", $font1, $size, 0, $x1, $y1, "$string");
      }
...

Pay no attention to excluded ending brackets..

Notice whare I called $font1.. If I call $arialb all is fine.

Here is get fonts

sub GetFonts {
  my($im) = $_[0];
  $arial = "fonts/arial.ttf";
  # I tried Tons of things here to no avail
  if ($font=="arialb") {
    $font1 = '$arialb'; # and so many different other attempts
  }

  $arialb = "fonts/ariblk.ttf";
  $ariali = "fonts/ariali.ttf";
  $arialbi = "fonts/arialbi.ttf";
  $comic = "fonts/comic.ttf";
  $comicb = "fonts/comicbd.ttf";
  $verdana = "fonts/verdana.ttf";
  $verdanab = "fonts/verdanab.ttf";
  $verdanai = "fonts/verdanai.ttf";
  $verdanabi = "fonts/verdanaz.ttf";
}

I also desire to do the same with $color but, once $font is figured out, I should be able to fiqure that out.

Thanks for any help.

Brad Gilbert
  • 33,846
  • 11
  • 78
  • 129
  • 4
    Maybe you won't want to hear this, but what you want to do is a pretty bad idea. See here for discussion: http://perl.plover.com/varvarname.html – Telemachus Aug 21 '09 at 23:00
  • 1
    @Jimbo: I did a little clean-up on your formatting, but frankly it's a disaster. Can you please clean up the blocks and add some indentation? You can't expect many people to wade through that in an effort to help you. – Telemachus Aug 21 '09 at 23:06
  • 2
    One more thing: if you start all your programs with `use warnings;` you get lots of helpful - well - warnings. So you would have seen this: Argument "fonts/arial.ttf" isn't numeric in numeric eq (==) at line . – Telemachus Aug 22 '09 at 00:01
  • Thanks so much. Sorry for the quick paste of the poor code. I will clean it up a bit so all can see what I actually have in mind. Thanks again for all the great advice.. –  Aug 22 '09 at 00:13
  • 1
    See http://stackoverflow.com/questions/1298035/how-do-i-use-symbolic-references-in-perl/1298392#1298392 – daotoad Aug 22 '09 at 00:37
  • Excellent reading at that link.. Thank you. Yeah, I write stuff out pretty quickly. If the concept works I take the time to clean it up. I am far from a developer and not even good enough to classify as a hack but, I am very careful about doing it right in the long run, should there be one. I need to learn much more and I have nothing but time, well <=10 more years anyhow... This is a simple flyer creator for local bands. I was asked to make one. I am giving it a shot. The gui side works well and is simple enough to well, be used by a band.. GD is kicking my butt though.. –  Aug 22 '09 at 00:42
  • Referring to perl.plover.com/varvarname.html in that last one.. I will check out the latter too. Thanks.. –  Aug 22 '09 at 00:46
  • Are you using Perl 5? If you are, why are you writing for Perl 4? – Brad Gilbert Aug 24 '09 at 20:43

3 Answers3

10

If $font equals "arial" and you want to access $arial, you want:

${$font}

But you almost certainly don't actually want to be doing this. I'm not quite sure what you're trying to do in your code, but it seems like using a hash would be easier and better:

$fonts{'arial'} = "/path/to/arial"

Also note that the ${$font} example won't work if you're using

use strict;
michael
  • 1,757
  • 2
  • 12
  • 9
  • Thanks a bunch. The ${$font} really helped. The hash is a better idea. I am still learning. So: $fonts{'arial'} = "/path/to/arial"; $fonts{'arialb'} = "/path/to/arialb"; What would I do with it? I guess I am lost there... –  Aug 22 '09 at 00:19
  • 1
    @Jimbo: if you use a hash, you can still refer to the font _by name_ (which is what you want, I think) since the name of the font is the hash's key. – Telemachus Aug 22 '09 at 00:27
  • @Telemachus I need to dive into using hash in my application. I don't care about calling the font by name and prefer to make it as dynamic and "unusual" as possible. I use timestamps in may cases for things / vars.. Your advice here has been quite valuable.. Thanks.. I basically have six lines of centered +/- wrapped text each line with indivisual font, color and size assigned over a background image. The gui is html/javascript converted to a png with gd.. hopefully.. –  Aug 22 '09 at 01:12
3

Stop wanting that: See perldoc -q "variable name":

Beginners often think they want to have a variable contain the name of a variable. ...

Short answer: Don't. Long answer: Read the FAQ entry (which is also available on your computer). Longer answer: Read MJD's Why it's stupid to "use a variable as a variable name" (Part 2, Part 3).

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
1

To answer your question, try something similar to:

my $f=\$b;  
$b="foo"; 
print $$f;

But I do see some issues:

if ($font=="arialb") {$font1 = '$arialb'}

you shouldn't be doing string comparisons with ==. You should use the eq operator.

Try this code:

$f="sf";
$b="fs";
if($f==$b){
    print "whoops";
}

Also, where is $font1 declared? If it's declared inside the scope of that if statement, you won't be able to see that variable outside of that scope. I recommend that you use the pragma use strict; see http://www.perl.com/doc/manual/html/lib/strict.html

Matt Beldyk
  • 121
  • 4
  • 1
    I don't think that `==` compares the length of strings. It always checks for numerical equality. But all strings are numerically equal to 0, so == will test true for any two strings. `perl -e 'print "True\n" if 'hello' == 'what the heck?'';` – Telemachus Aug 21 '09 at 23:25
  • Learn something new every day, especially in edge cases you avoid like the plague. – Matt Beldyk Aug 21 '09 at 23:35
  • 2
    @Telemachus: Actually, strings nummify as the value of their leading digits, if any: `perl -e "print '123abc' + 1"` prints "124". Most strings don't have leading digits, though, and thus nummify as 0. – Michael Carman Aug 21 '09 at 23:58
  • So much help... I have not written any PERL in over 10 years. I totally forgot about eq. Everyone is great help here... –  Aug 22 '09 at 00:21
  • @Michael: I actually do know that, but automatically began to assume strings like those in these examples (i.e., without any digits). Thanks for clarifying though. – Telemachus Aug 22 '09 at 00:23