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

cant seem to find a way to remove the delimiter from the contents of the file and display them on the browser the code is as follows
#!/usr/bin/perl -w use strict; use warnings; use CGI; use CGI::Carp qw(fatalsToBrowser); my $query= new CGI; my $filename= "testfile.txt"; my $value; my $fname; my $lname; my $textarea; my $delimiter_used="|"; sub Writing_to_file { my @input_to_file=@_; open (DAT, ">>$filename") || die ("no file exists"); print DAT join($delimiter_used, @input_to_file), "\n"; close(DAT); } sub display_on_browser { open (DAT, $filename) || die ("no file exists"); my @data_in_the_file=<DAT>; close(DAT); my $data_to_read; print "Content-type:text/html\n\n"; print"<html><head><title>Guestbook</title></head><body>"; foreach $data_to_read(@data_in_the_file) { my @delimiter_to_remove=$data_to_read; @delimiter_to_remove=~s/\$delimiter_used/\n/g; print "<table border=1><tr><td>@delimiter_to_remove\n< +br></td></tr></table>"; } print "<a href=http://guestbook.ds5403.dedicated.turbo +dns.co.uk/index.html><h1>Back to guestbook</a>"; print "</body></html>"; } $fname=$query->param('name'); $lname=$query->param('name1'); $textarea=$query->param('comments'); Writing_to_file($fname,$lname,$textarea); display_on_browser();
how do i solve this issue as smoothly as possible thanks jinnks

Replies are listed 'Best First'.
Re: removing the delimiter
by ikegami (Patriarch) on Aug 06, 2008 at 04:13 UTC

    (This appears to be a followup to the OP's very recent thread.)

    @delimiter_to_remove=~s/\$delimiter_used/\n/g;

    There are four problems in that line.

    • s/// operates on a scalar, not an array. Thus the "Can't modify array dereference in substitution (s///)" error message. Considering your array always contains a single element, I don't know why you have an array at all.
    • Escaping the "$" is exactly what you don't want to do. You want interpolation to occur.
    • $delimiter_used holds a string, not a regexp. You can convert from one to the other using \Q..\E or quotemeta.
    • Newlines are pretty much the same thing as spaces in HTML, so I think you want to use <br> rather than \n as the replacement.

    Fix:

    $data_to_read =~ s/\Q$delimiter_used\E/<br>/g;

    Or if you plan on having more than one element in @delimiter_to_remove,

    s/\Q$delimiter_used\E/<br>/g for @delimiter_to_remove;
Re: removing the delimiter
by Anonymous Monk on Aug 06, 2008 at 04:16 UTC
    perldoc -f quotemeta
    perldoc perlintro
    my $delim = quotemeta '|'; $delim = "\Q|\E"; my @foo = qw[ a|b|c d|e|f g|g|g|g ]; foreach(@foo){ s/$delim/\n/g; } print $_,$/,'--',$/ for @foo; __END__ a b c -- d e f -- g g g g --