http://www.perlmonks.org?node_id=433505

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

I am attempting to avoid the following syntax structures.
###-- using cgi.pm print qq{ <sometag> }; print textfield(-name=>'cgifield',-size=>'20'); print qq{ <sometag> };

Replies are listed 'Best First'.
Re: Is it possible to print out CGI fields using the qq operator?
by kutsu (Priest) on Feb 22, 2005 at 22:35 UTC

    As far as I know you cannot, as tye put it on the chatterbox:

    data structure dereferencing is interpretted inside of double quotes, but method calls and code dereferencing aren't

    This is more easily seen if you use the OO form of CGI.pm:

    use CGI; $cgi = CGI->new; print "$cgi->textfield(-name=>'cgifield',-size=>'20')"; print qq/$cgi->textfield(-name=>'cgifield',-size=>'20')/; print <<EOL; $cgi->textfield(-name=>'cgifield',-size=>'20') EOL #all print CGI=HASH(0x...)
Re: Is it possible to print out CGI fields using the qq operator?
by BUU (Prior) on Feb 23, 2005 at 01:16 UTC
    Better yet, either use a "Real Templating Engine" (such as HTML::Template) or just manually create your html inside strings, don't mix html in strings with CGI.pm's html functions, that's just ugly.
Re: Is it possible to print out CGI fields using the qq operator?
by hossman (Prior) on Feb 23, 2005 at 00:47 UTC
Re: Is it possible to print out CGI fields using the qq operator?
by chas (Priest) on Feb 23, 2005 at 02:01 UTC
    How about:
    use CGI qq(:standard); print qq{ <sometag> @{[textfield(-name=>'cgifield',-size=>'20')]} <sometag> };

    (This prints:
    <sometag>
    <input type="text" name="cgifield" size="20" />
    <sometag>

    Admittedly, it is still a bit cumbersome...
    chas
    (I'm a little confused about why the /> instead of >. The code by the hok si la also produces the same thing...) (Updated to fix typos, errors and formatting.)
      (I'm a little confused about why the /> instead of >. The code by the hok si la also produces the same thing...)

      That's because GGI.pm's HTML generation methods produce XHTML, rather than HTML 4. They have, in fact, been doing so for a long time, as enthusiastically pointed out by merlyn here.