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

The code adds the delimiter to the file between all parameters but to remove and display it on the browser is another story cant do it making a silly mistake somewhere
#!/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; $'="|"; sub Writing_to_file { my @input_to_file=@_; open (DAT, ">>$filename") || die ("no file exists"); print DAT join($', @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/\$'/\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();
Please advise thanks jinnks

Replies are listed 'Best First'.
Re: Special variable...
by Lawliet (Curate) on Aug 06, 2008 at 02:00 UTC

    As mentioned in the CB, $' is the same as substr($var, $+[0])

    The reason you have to use @- is $' is a read only variable. (Also, it is a memory hog)

    <(^.^-<) <(-^.^<) <(-^.^-)> (>^.^-)> (>-^.^)>
Re: Special variable...
by kyle (Abbot) on Aug 06, 2008 at 02:30 UTC

    Welcome to the Monastery!

    I think instead of $' (which is read-only), you want $", which is what gets put between list elements interpolated into a double quoted string.

    As an aside, I'd suggest that this:

    open (DAT, ">>$filename") || die ("no file exists");

    ...be more like this:

    open my $dat, '>>', $filename or die "Can't append '$filename': $!";

    That way you get a better error message, and it won't matter if $filename starts with some strange characters. I'm opening a lexical scalar as a filehandle here too, so you'd use "$dat" everywhere you had "DAT" before.

    Updated to add that I also agree with toolic that using a special variable here is not really necessary.

Re: Special variable...
by toolic (Bishop) on Aug 06, 2008 at 02:29 UTC
    Lawliet has correctly diagnosed your problem: that you should not assign to the $' special variable (see perlvar).

    However, I do not understand why you are trying to use that variable at all in your code. If you are trying to join and split using "|" as a delimiter, you can just do:

    print DAT join('|', @input_to_file), "\n"; @data_to_print=split /\|/, $data_to_read;