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.

8
  • 4
    Maybe you won't want to hear this, but what you want to do is a pretty bad idea. See here for discussion: perl.plover.com/varvarname.html Commented Aug 21, 2009 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. Commented Aug 21, 2009 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 <program-name> line <number>. Commented Aug 22, 2009 at 0: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.. Commented Aug 22, 2009 at 0:13
  • 1
    See stackoverflow.com/questions/1298035/… Commented Aug 22, 2009 at 0:37

3 Answers 3

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;
Sign up to request clarification or add additional context in comments.

3 Comments

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 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

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).

Comments

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

5 Comments

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?'';
Learn something new every day, especially in edge cases you avoid like the plague.
@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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.