从Perl中的文本文件将文本写入textarea [英] Writing text to textarea from a text file in perl

查看:89
本文介绍了从Perl中的文本文件将文本写入textarea的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用文本填充文本区域。
文本将是用户发表的评论。它将从名为comments.txt的文件中加载。

I am trying to populate a textarea with text. The text will be comments a user has made. It will be loaded from a file called comments.txt

文件模板为:

 username1
 commentscomments
 commentscomments
 username2
 commentscommentscome
 comchefhjshfhhfjdjdj
 dfhfhjdijedhdjdjdjdj
 username3
 februgusyfvretgtyef

我还有一个名为account.txt的文件,用于存储用户名。

I also have a file called accounts.txt which stores the usernames.

我当前的代码只是将整个comments.txt文件写入textarea

My current code just writes the entire comments.txt file to the textarea

  my $username=(param('username')); 
  open my $FHIN, '<', "comments.txt" || die "$!";
  my @fhin = <$FHIN>;
  print textarea(-name=>"CommentArea",-default=>"@fhin",-rows=>10,-columns=>60);

我在想是否应该有一个子函数返回一个字符串数组,这些字符串是用户的评论?我应该如何构造它,是否应该有一个循环的注释文件,检查每行是否等于用户名,并且是否打印每行,直到出现与account.txt上的一行匹配的另一行为止。

I was thinking should I have a sub that returns an array of strings that are the comments of the user? And how should I structure it should I have a loop of the comment file that checks if each line eq the username and if it does it prints each line until it comes to another line that matches a line on the accounts.txt

基本上文本区域应该只显示:

Basically the textarea should only show:

 commentscommentscome
 comchefhjshfhhfjdjdj
 dfhfhjdijedhdjdjdjdj

如果username2是已登录的用户。

if username2 is the user thats logged on.

我们将不胜感激!

推荐答案

假设您拥有所有用户帐户的列表,放入哈希中,您可以按以下步骤进行操作。

Assuming you have a list of all the user accounts and you put that into a hash, you could do it as follows.

sub get_comments {
  my ($username, $filename, $all_users) = @_; # $all_users is a hashref

  open my $FHIN, '<', $filename or die "Cannot open $filename for reading: $!";

  my @comments;
  my $found; # starts out as undef
  while (my $line = <$FHIN>) {
    chomp $line;

    if (exists $all_users->{$line}) {
      last if $found; # stop once we find another user
      if ($line eq $username) {
        $found++;
        next;
      }
    }

    push @comments, $line if $found;
  }
  return \@comments;
}

my $comments = get_comments(param('username'), 'comments.txt', $all_users);
print textarea(
  -name    => "CommentArea",
  -default => join("\n", @{ $comments }),
  -rows    => 10,
  -columns => 60,
);

它将打开文件并检查用户名。如果找到我们的用户名,它将在此之后开始保存行,直到找到其他用户名并停止。文件名是一个参数,因此您不必依赖单个文件即可从config中获取文件名(或使用测试文件进行单元测试)。

It opens your file and checks for usernames. If it finds our username, it will start saving the lines after that until it finds a different username and stops. The file name is an argument so you do not have to rely on a single file and can get that from config (or use a test file for unit testing).

有不需要关闭文件句柄,因为它将超出范围并隐式在子句末关闭。

There is no need to close the file handle as it will go out of scope and implicitly close at the end of the sub.

它打印:

<textarea name="CommentArea"  rows="10" cols="60">commentscommentscome
comchefhjshfhhfjdjdj
dfhfhjdijedhdjdjdjdj</textarea>

这篇关于从Perl中的文本文件将文本写入textarea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆