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

Ok, I have a file that holds the html code for any users of my site.
Right now though, I have it set so that when the user modifies their page in a textarea box, when they save it, I strip all the carriage returns.
I do this because when I attempt to load their page from the file, It stops reading that user's page when it hits a carriage return.
I split each user's page with '&..&' and their username and page content with ':..:'
So the file looks like this:
User1:..:

Hello

&..&User2:..:

Welcome


This works fine, except that when you go to edit your page again, All the code is bunched up on a couple lines...
What I'd like to do is to not have to strip all carriage returns, and then read from the file.
So, basically, here's my question:

How can I read an entire file into a hash (username and content), and not have the hash contain only the content of a user's page up until a carriage return?

Replies are listed 'Best First'.
Re: Split Function
by chromatic (Archbishop) on Apr 16, 2000 at 00:24 UTC
    Based on what you've said, I suspect you may want to look at the $/ (input record separator) variable. If your code looks something like this:
    open (PAGE, $filename) || die "Can't open $filename: $!"; my $text = <PAGE>; close PAGE || die "Can't close $filename: $!"; # parse $text somehow
    then your error is that you're asking for only one line from the file. The magic variable $/ tells Perl which character comes at the end of a line, and the < filehandle > operator gets one line from that filehandle.

    The solution might look something like this:

    my $text; # outside of the block so it's in scope after we leav +e the block { local $/; open (PAGE, $filename) || die "Can't open $filename: $!"; $text = <PAGE>; close (PAGE) || die "Can't close $filename: $!"; }
    That will temporarily undefine the input record separator and place the whole of $filename into $text. You can set $/ to anything you want, such as "\n--\n\n", if you have a better record separator in mind. ("&..&", from your example above.)
Re: Split Function
by btrott (Parson) on Apr 15, 2000 at 23:32 UTC
    You might think about using a DBM file instead of a flat text file... DBMs associate keys with values, so you could associate user names with their content.

    You might use something like this:

    use DB_File; my %users; my $file = "foo"; tie %users, 'DB_File', $file, O_CREAT|O_RDWR; $users{'User1'} = <<HTML; <center> <h1>Hello!</h1> </center> HTML my $html = $users{'User1'}; print $html;
    That's just an example; but it might make your life a lot easier, because then you wouldn't have to worry about carriage returns and the like.
Re: Split Function
by Anonymous Monk on Apr 15, 2000 at 22:58 UTC
    To see what I mean, go here, and logon as 'guest', with the password 'guest'.
Re: Split Function
by Anonymous Monk on Apr 15, 2000 at 22:59 UTC
    Damn, I forgot: once your at the main screen after you logon, click on 'Edit Page'
Re: Split Function
by Anonymous Monk on Apr 15, 2000 at 23:22 UTC
    Didn't you read the message on that chat page? It is under construction!
RE: Split Function
by Anonymous Monk on Apr 15, 2000 at 23:30 UTC
    While I'm at it, that cheap chat page I have...
    Can anyone suggest how I could see if a user has not done anything for 3 minutes? What I'd like to do, is that after 3 minutes of inactivity, I print to the screen the words "User guest has left the room"
    That way, I could also keep a list of all chatters in the room.
A reply falls below the community's threshold of quality. You may see it by logging in.