svsingh has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to create some HTML with Perl. The print stament has a variable immediately before some text and I read that in these cases, you should surround the variable name with brackets. Here's the code I'm using:

my $link = "<a href=\"" . &getQueryString('title') . "\">"; print "<th>${link}Title</a></th>\n";

No matter how I think about this, I can't see how using "&lt;th&gt;$linkTitle&lt;/a&gt; would be good. However, when I run this using strict and with the -w option, Perl spits out the following warning:

Ambiguous use of ${link} resolved to $link at movies.pl line 165.

I wish I could find where I read I could surround variables with brackets, but I can't. Is this valid? If so, then what's it called. Searching the web for "${" isn't proving too fruitful.

Note: After previewing the message it seems odd that I'm even splitting this into two lines. I repeat these lines with some slight modifications a few times and keeping everything in two lines keeps the code readable for me.

Thank you.

Replies are listed 'Best First'.
Re: Printing Scalars Next to Text
by bart (Canon) on May 03, 2003 at 08:44 UTC
    Perl 5.004, huh? I think your problem is related to your perl version. So it can be that ${link} will always give a problem with a perl this old. Note that link is a built-in function.

    To get around your problem, I'd suggest using sprintf or printf. In particular, the first line, with the sub call, will look a lot cleaner:

    my $link = sprintf '<a href="%s">', getQueryString('title'); printf "<th>%sTitle</a></th>\n", $link;
    or something even more compacted.

    p.s. you should really reconsider properly escaping these strings, for proper inclusion in HTML. The proper order is first URL-escaping, and next HTML-escaping the string. Perhaps you have already taken care of it, or part of it, in your sub, and I don't know what subs you have available for this purpose (it depends on the library you use), so I won't give the complete code.

(joshua) Re: Printing Scalars Next to Text
by joshua (Pilgrim) on May 03, 2003 at 05:55 UTC
    I can't recreate your problem on i686-linux with Perl 5.8.0. Curly brackets are definately appropriate to use in this case. Please do get rid of those nasty escaped quotes though:
    my $link = qq|<a href="| . &getQueryString('title') . qq|">|;

    -Joshua

Re: Printing Scalars Next to Text
by jonadab (Parson) on May 03, 2003 at 13:51 UTC

    Change the name of the variable. Perl allows you to give variables names that cause this sort of ambiguity, but it's generally a bad idea. Any word listed in chapter 3 of the Camel book you should avoid using as a variable name. Call your variable $href or something and your problem will go away.


    {my$c;$ x=sub{++$c}}map{$ \.=$_->()}map{my$a=$_->[1]; sub{$a++ }}sort{_($a->[0 ])<=>_( $b->[0])}map{my@x=(& $x( ),$ _) ;\ @x} split //, "rPcr t lhuJnhea eretk.as o";print;sub _{ord(shift)*($=-++$^H)%(42-ord("\r"))};
Re: Printing Scalars Next to Text
by jgallagher (Pilgrim) on May 03, 2003 at 05:52 UTC
    What version of Perl and what OS are you using? I get no warnings with your code with 5.6.0 on Mac OS X.
      It looks like my ISP is using 5.004_01. The code seems to be running without any problems on ActiveState 5.6.1. I just assumed that was an oversight on my part though. (I'm new, the error has to be my fault, right?)
        Reminds me of something I heard once... Software guys tend to always blame problems on the software, and insist it can be fixed in software, and almost never even consider the fact that it could be a hardware thing. I know I'm usually guilty of that. Same idea applies here, I think.

        Unfortunately, I can't help you any further. :-/ It would take someone more knowledgable of 5.004 to tell you why you're getting the warning under it.
Re: Printing Scalars Next to Text
by pfaut (Priest) on May 03, 2003 at 12:05 UTC

    Try changing the print to print "<th>" . $link . "Title</a></th>\n";.

    90% of every Perl application is already written.
    dragonchild
Re: Printing Scalars Next to Text
by Anonymous Monk on May 03, 2003 at 14:27 UTC
    Sorry everyone. This one turned out to be a false alarm. I found the 5.6.1 installation on my server and ran it with that. There were no warnings.

    jonadab, thanks for the tip. I'll review the list in chapter 3 and try to avoid those keywords in the future.

Re: Printing Scalars Next to Text
by LordWeber (Monk) on May 03, 2003 at 09:45 UTC