tags:

views:

182

answers:

3

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.

+1  A: 

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
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
Learn something new every day, especially in edge cases you avoid like the plague.
Matt Beldyk
@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
So much help... I have not written any PERL in over 10 years. I totally forgot about eq. Everyone is great help here...
@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
+10  A: 

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
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...
@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
@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..
+3  A: 

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