Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Displaying an array in a cgi generated textarea

by martymart (Deacon)
on Feb 27, 2003 at 18:10 UTC ( [id://239182]=perlquestion: print w/replies, xml ) Need Help??

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

Fellow Monks,
I have a text file that I'd like to display in a form textarea. I read this file into an array @source, and put that into the textarea of my cgi script...
use CGI; $co=new CGI; #some code here $co->center($co->textarea ( -name=>'textarea', -default=>"@source", -rows=>20, -columns=>80, )), #other code here
The problem is that while the first line displays fine, all the subsequent lines are shifted to the right by one space. I think it may be because the array is in between quotation marks (I could be wrong though!). However, when I get rid of the quotation marks it really messes up the order of the elements in the array. It seems to display to the text area like this:
print "@source";
and what I want it to do is display the array like this:
print @source;
I'd appreciate an ideas or suggestions.
Martymart

Replies are listed 'Best First'.
Re: Displaying an array in a cgi generated textarea
by fruiture (Curate) on Feb 27, 2003 at 18:18 UTC

    An array in double quotes (which equals qq//) is interpolated and the result is ...

    print "left @array right"; # try perl -MO=Deparse for that: print 'left '.join($",@array).' right'

    So you see, perl uses $" as the seperator of the array elements, $" is by default just a single space character, you can simply change that. Use local() to avoid problems with other code, because CGI.pm afaik uses $", too.

    local $" = ''; #... -default => "@source", #... #or even -default => do { local $" = ''; "@source" }, #or better -default => join('',@source)

    HTH, see perlvar

    --
    http://fruiture.de
      Thanks fruiture,
      just tried it... works great,
      valuable lesson learned...
      Martymart
Re: Displaying an array in a cgi generated textarea
by pfaut (Priest) on Feb 27, 2003 at 18:25 UTC

    When you read the file into the array, you read the line terminators as well. Those line terminators start a new line in the textarea, too. When you interpolated the array into a string with -default=>"@source", perl used the value of $" (a space by default) to separate the elements. This is where the extra space came from on the second and subsequent lines.

    There are a couple of ways I can think of to fix this. Choose one of these to suit your tastes.

    • Use chomp @source to remove the newline characters before interpolating into the string. This will allow the textarea to rewrap the text.
    • Set $" to an empty string before the interpolation. This will keep the current line breaks when loaded into the textarea. You might want to use local ($") = "" here. The local will reset $" back to its previous value when your program leaves the current scope.
    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://239182]
Approved by LAI
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-23 15:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found