我需要编写一个应该有一个TCP套接字和一个命名管道的守护进程.通常,如果我需要使用"纯"套接字实现多IO服务器,则基于选择的多IO模型始终是我将选择的模型.所以你们中的任何人都曾经选择使用命名管道,或者你可以告诉我这是不可能的.提前致谢.
我正在尝试用 Perl 编写一个脚本,允许用户上传文件。目前,它说它正在工作,但实际上并没有上传文件!
这是代码:
#!/usr/bin/perl
use CGI;
my $cgi = new CGI;
my $dir = 'sub';
my $file = $cgi->param('file');
$file=~m/^.*(\\|\/)(.*)/;
# strip the remote path and keep the filename
my $name = $2;
open(LOCAL, ">$dir/$name") or print 'error';
while(<$file>) {
print LOCAL $_;
}
print $cgi->header();
print $dir/$name;
print "$file has been successfully uploaded... thank you.\n";enter code here
Run Code Online (Sandbox Code Playgroud) 感谢所有帮助我实现这一目标的人.
现在我的新问题.我正在使用2003年编写的一本书,该教程试图抓住一个已经改变的页面.
原始地址是:" http://www.oreilly.com/catalog/prdindex.html "此页面不再存在,但会重定向到新页面:" http://oreilly.com/store/complete.html "
问题"我认为"是html代码在7年内发生了变化.代码曾经是这样的:
<tr bgcolor="#ffffff">
<td valign="top">
<a href="http://oreilly.com/catalog/googlehks">Google Hacks</a><br />
</td>
<td valign="top" nowrap="nowrap">0-596-00447-8</td>
<td valign="top" align="right">$24.95</td>
<td valign="top" nowrap="nowrap" align="center">
<a href="http://safari.oreilly.com/0596004478">Read it on Safari</a>
</td>
<td valign="top" nowrap="nowrap">
<a href="http://examples.oreilly.com/googlehks">Get examples</a>
</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
所以无论如何html已经改变了.您可以通过查看浏览器上的源代码来查看它.
当我运行脚本时,我收到此错误:
在/usr/lib/perl5/site_perl/5.8.8/HTML/TreeBuilder.pm第93行的子例程条目中使用未初始化的值.无法在./SpiderTutorial_19_09.pl第67行的未定义值上调用方法"as_HTML".有0本Perl书籍和0本Java书籍.Java比Perl多0个.
这是我正在尝试运行的代码.
#!/usr/bin/perl -w
use strict;
use LWP::Simple;
use HTML::TreeBuilder;
my $url = 'http://oreilly.com/store/complete.html';
my $page = get( $url ) or die $!;
my $p = HTML::TreeBuilder->new_from_content( $page );
my($book);
my($edition);
my @links = $p->look_down(
_tag => …Run Code Online (Sandbox Code Playgroud) 我是perl的新手新手.我正在尝试创建一个返回哈希值的函数.下面的代码只返回哈希的最后一个索引.我用Google搜索,无法找到我需要的东西.感谢是否有人能告诉我哪里出错了.
我期待,如果我通过"he_1",我应该得到1的返回值等等.但我看到的只有9.
#!/usr/bin/perl
my %IndexMap = ();
my $MAX_V = 5;
my $MAX_T = 10;
sub InitIndexMap {
foreach my $i (0..$MAX_V-1) {
$IndexMap["he_".$i] = $i;
print "he_".$i;
print $IndexMap["he_".$i];
}
foreach my $i ($MAX_V..$MAX_T-1) {
$IndexMap["un".$i] = $i;
print "un".$i;
print $IndexMap["un".$i];
}
}
sub GetVal {
my ($name) = @_;
return $IndexMap[$name];
}
&InitIndexMap();
my ($index) = &Getval("he_4");
print $index;
Run Code Online (Sandbox Code Playgroud) 我是perl的新手,我正在尝试读取和写入perl中的csv文件.但没有任何事情可以帮助我解决问题所在.我可以使用'<'读取没有问题,但我无法写.
use strict;
use warnings;
use Text::CSV_XS;
my $file = 'file.csv';
my $csv = Text::CSV_XS->new();
open (InCSV, '+>>', $file) or die $!;
while (<InCSV>) {
if ($csv->parse($_)) {
my @columns = $csv->fields();
if($columns[1] eq "01") {
my $str = "Selected $columns[6] \n ";
push(@columns,$str);
print InCSV join("," , @columns), "\n";
}
} else {
my $err = $csv->error_input;
print "Failed to parse line: $err";
}
}
close InCSV;
Run Code Online (Sandbox Code Playgroud) http://perldoc.perl.org/Text/Balanced.html说:
该
extract_delimited函数形式化了从字符串的开头提取单字符分隔子字符串的常用习惯用法.例如,要提取单引号分隔的字符串,通常使用以下代码:Run Code Online (Sandbox Code Playgroud)($remainder = $text) =~ s/\A('(\\.|[^'])*')//s; $extracted = $1;
\\.(这是'\.')似乎被覆盖([^'])*.为什么\\.|在那里?为什么不用s/\A('[^']*')//s?
我有一个数字数组,并希望删除数组开头的所有非正数(即零或负数).这就是我所拥有的:
shiftlbl:
$shift = shift @ary;
if (0 >= $shift) {goto shiftlbl;}
else {unshift @ary, $shift;}
Run Code Online (Sandbox Code Playgroud)
有没有一种方法更好(更快),或者工作方式大致相同,但更多Perlish或更容易阅读?
use strict;
use warnings;
my $tmp = join "\n", <DATA>;
my @biblables = ();
Run Code Online (Sandbox Code Playgroud)
列表项将被提取并存储到while循环中的@biblables中
while($tmp=~m/\\bibitem\[([^\[\]]*)\]{([^\{\}]*)}/g)
{
push(@biblables, "\\cite{$2}, ");
}
print @biblables;
Run Code Online (Sandbox Code Playgroud)
打印时我们得到的输出如下:
\cite{BuI2001},\cite{BuI2002},\cite{BuI2003},\cite{BuI2004},\cite{BuI2005},\cite{BuI2006},
Run Code Online (Sandbox Code Playgroud)
但是我们需要像这样的输出
\cite{BuI2001},\cite{BuI2002},\cite{BuI2003},\cite{BuI2004},\cite{BuI2005},\cite{BuI2006}.
Run Code Online (Sandbox Code Playgroud)
因此,我们可以使用post regex在数组中的listitem末尾插入点
while($tmp=~m/\\bibitem\[([^\[\]]*)\]{([^\{\}]*)}/g)
{
my $post = $';
if($post!~m/\\bibitem\[([^\[\]]*)\]{([^\{\}]*)}/)
{ push(@biblables, "\\cite{$2}."); }
else { push(@biblables, "\\cite{$2}, "); }
}
print @biblables;
Run Code Online (Sandbox Code Playgroud)
如果有很短的方法可以得到这个输出,请你告诉我
#
__DATA__
\bibitem[{BuI (2001)}]{BuI2001}
\bibitem[{BuII (2002)}]{BuI2002}
\bibitem[{BuIII (2003)}]{BuI2003}
\bibitem[{BuIV (2004)}]{BuI2004}
\bibitem[{BuV (2005)}]{BuI2005}
\bibitem[{BuVI (2006)}]{BuI2006}
Run Code Online (Sandbox Code Playgroud) $a=10
print "$a";
print "${a}";
print "$(a)";
Run Code Online (Sandbox Code Playgroud)
print "$a";并且 print "${a}"; 工作方式完全相同,看起来perl中的两种语法都支持打印标量变量,但 print "$(a)";打印出奇怪的值,想要低估当发生什么时发生的事情 print "$(a)";
我需要比较两个文件中的数字.
例如在file1.txt中:
人的
1 2 3 4 5 6 7 8 9
在另一个file2.txt中:
人的
1 2 3 8 0 7 6
以上两个文件应作为输入.输出将如下:
1 2 3 6 7 8(即匹配的数字)如果可能,输出必须在另一个文件中.