这两个例子之间有什么区别?
#!/usr/bin/perl
use warnings;
use 5.012;
my $str = "\x{263a}";
open my $tty, '>:encoding(utf8)', '/dev/tty' or die $!;
say $tty $str;
close $tty;
open $tty, '>:bytes', '/dev/tty' or die $!;
say $tty $str;
close $tty;
# -------------------------------------------------------
binmode STDOUT, ':encoding(utf8)' or die $!;
say $str;
binmode STDOUT, ':bytes' or die $!;
say $str;
Run Code Online (Sandbox Code Playgroud) 在这种情况下,习惯上只打开一次文件吗?
#!/usr/bin/env perl
use warnings;
use 5.012;
use autodie;
my $file = 'my_file';
open my $fh, '>>', $file;
say $fh "Begin";
close $fh;
$SIG{INT} = sub {
open my $fh, '>>', $file;
say $fh "End";
close $fh;
exit
};
my $result;
while ( 1 ) {
$result++;
# ...
# ...
# ...
open my $fh, '>>', $file;
say $fh $result;
close $fh;
sleep 3;
}
Run Code Online (Sandbox Code Playgroud) 为了使它正常工作,我必须更改什么?
my $str = "start middle end";
my $regex = / start ( .+ ) end /;
$str.=subst( / <$regex> /, { $0 } ); # dies: Use of Nil in string context
say "[$str]";
Run Code Online (Sandbox Code Playgroud) 是否有可能在 END 块内知道程序是否死亡?在 Perl 中我会这样做
END {
if ( $? == 255 ) {
# ...
}
}
Run Code Online (Sandbox Code Playgroud) 我在Mail :: IMAPClient中找到了这个.在什么地方$_
在$SEARCH_KEYS{ uc($_) }
从何而来?
sub _quote_search {
my ( $self, @args ) = @_;
my @ret;
foreach my $v (@args) {
if ( ref($v) eq "SCALAR" ) {
push( @ret, $$v );
}
elsif ( exists $SEARCH_KEYS{ uc($_) } ) {
push( @ret, $v );
}
elsif ( @args == 1 ) {
push( @ret, $v ); # <3.17 compat: caller responsible for quoting
}
else {
push( @ret, $self->Quote($v) );
}
} …
Run Code Online (Sandbox Code Playgroud) 由于几乎所有模块都有,并且反复说我们不应该重新发明轮子,因为我不为CPAN编写模块,所以我不需要使用OO.但我的问题是,专业的Perl代码主要是用OO风格编写的吗?
删除相应的符号表条目后,为什么要从"$ n"和"$ m"中获取值?
#!/usr/bin/env perl
use warnings;
use 5.012;
package Foo;
our $n = 10;
our $m = 20;
delete $Foo::{'n'};
delete $Foo::{'m'};
say $n; # 10
say $m; # 20
Run Code Online (Sandbox Code Playgroud) 在使用用户定义的变量时,有没有"@" - sigil我不能做的事情?
#!perl6
use v6;
my $list = <a b c d e f>;
my @list = <a b c d e f>;
$list.list.perl.say;
@list.perl.say;
$list[2..4].say;
@list[2..4].say;
$list.elems.say;
@list.elems.say;
$list.end.say;
@list.end.say;
say 'OK' if $list ~~ /^c$/;
say 'OK' if @list ~~ /^c$/;
Run Code Online (Sandbox Code Playgroud) 有没有一种更短的方式来在JavaScript中写这个?
var data = [];
var table = document.getElementById( 'address' );
var rows = table.getElementsByTagName( 'tr' );
for ( var x = 0; x < rows.length; x++ ) {
var td = rows[x].getElementsByTagName( 'td' );
for ( var y = 0; y < td.length; y++ ) {
var input = td[y].getElementsByTagName( 'input' );
for ( var z = 0; z < input.length; z++ ) {
data.push( input[z].id );
}
}
}
Run Code Online (Sandbox Code Playgroud) #!/usr/bin/env perl
use warnings;
use 5.012;
use utf8;
use WWW::Mechanize::Cached;
use Some::Module qw(some_method);
my $url = '...';
my $result = some_method( $url );
Run Code Online (Sandbox Code Playgroud)
该some_method()
应用本身get()
形成LWP::Simple
.
我怎么能用这个脚本覆盖get()
我的my_get()
?
sub my_get {
my $url;
my $mech = WWW::Mechanize::Cached->new();
$mech->get( $url );
my $content = $mech->content( format => 'text' );
return $content;
}
Run Code Online (Sandbox Code Playgroud)