为什么第一个示例不输出警告?
#!/usr/bin/env perl
use warnings;
use 5.012;
my $c = "9\n";
say $c * 2;
my $d = "6a";
say $d * 2;
# 18
# Argument "6a" isn't numeric in multiplication (*) at ./perl8.pl line 9.
# 12
Run Code Online (Sandbox Code Playgroud) 当我设置locale为it_IT.UTF-8(导出LC_ALL = it_IT.UTF-8)并运行此脚本时
#!/usr/bin/env perl
use warnings;
use 5.012;
use POSIX qw(strftime);
say strftime "%A %B %e %H:%M:%S %Y", localtime;
Run Code Online (Sandbox Code Playgroud)
我得到这个输出:
martedì marzo 15 08:50:07 2011
Run Code Online (Sandbox Code Playgroud)
但阅读本文(来自The-use-locale-pragma):
By default, Perl ignores the current locale.
The use locale pragma tells Perl to use the current locale for some operations:
...
The POSIX date formatting function (strftime()) uses LC_TIME .
为什么我的语言环境设置会对strftime-output产生影响而不使用localepragma?
如果我关闭Firefox窗口或者以某种方式停止此脚本,我怎么能管理这里作为后台进程调用的morbo-server会自动关闭/终止?
#!/bin/bash
morbo Mojolicious_Lite.pl &
firefox -new-window http://localhost:3000/
Run Code Online (Sandbox Code Playgroud) 如果我这样做
#!/usr/local/bin/perl
use warnings;
use 5.014;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new();
my $res = $ua->get( 'http://www.perl.org' );
Run Code Online (Sandbox Code Playgroud)
我可以调用这样的HTTP::Response方法
say $res->code;
Run Code Online (Sandbox Code Playgroud)
是否有可能HTTP::Request从$res对象调用方法或需要HTTP::Request显式创建对象?
my $ua = LWP::UserAgent->new();
my $method;
my $res = $ua->get( 'http://www.perl.org' );
$ua->add_handler( request_prepare => sub { my( $request, $ua, $h ) = @_; $method = $request->method; }, );
say $method; # Use of uninitialized value $method in say
Run Code Online (Sandbox Code Playgroud) 保留带有未定义值的选项(在本例中为'maxdepth')是否可以?
#!/usr/bin/env perl
use warnings;
use 5.012;
use File::Find::Rule::LibMagic qw(find);
use Getopt::Long qw(GetOptions);
my $max_depth;
GetOptions ( 'max-depth=i' => \$max_depth );
my $dir = shift;
my @dbs = find( file => magic => 'SQLite*', maxdepth => $max_depth, in => $dir );
say for @dbs;
Run Code Online (Sandbox Code Playgroud)
或者我应该这样写:
if ( defined $max_depth ) {
@dbs = find( file => magic => 'SQLite*', maxdepth => $max_depth, in => $dir );
} else {
@dbs = find( file => magic => 'SQLite*', in => $dir …Run Code Online (Sandbox Code Playgroud) media:group虽然它发生在dom中,为什么不起作用?
#!/usr/bin/env perl
use warnings;
use 5.12.0;
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new();
my $id = 'E7511681ABEA8635';
my $url = 'http://gdata.youtube.com/feeds/api/playlists/' . $id . '?v=2';
my $tx = $ua->get( $url );
say $tx->res->dom->at( 'category' )->type;
say $tx->res->dom->at( 'media:group' )->type;
# category
# Can't call method "type" on an undefined value at ./perl.pl line 13
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Net :: IMAP :: Client编写一个脚本来输出电子邮件的正文,但到目前为止,我尝试从模块输出的每个变量都显示为:ARRAY(0x86f5524)或者给出错误"不能将未定义的值用作SCALAR引用."
模块文档说明了这一点
# fetch full messages
my @msgs = $imap->get_rfc822_body([ @msg_ids ]);
print $$_ for (@msgs)
Run Code Online (Sandbox Code Playgroud)
应包含对标量的引用.@msg_id应该是收件箱中电子邮件号码的数字数组,但也会作为数组引用返回.
我不确定如何正确输出这些数据,因此它是可读的.这是模块参考:Net :: IMAP :: Client
这是我的代码的片段:
use Net::IMAP::Client;
use Net::IMAP;
use Net::SMTP;
use strict;
use warnings;
my $imap = Net::IMAP::Client->new(
server => ,
user => , # i omitted this data for privacy
pass => ,
ssl => ,
port => ,
) or die "could not connect to IMAP server";
$imap->login or die('Login Failed: ' . $imap->last_error); …Run Code Online (Sandbox Code Playgroud) 这个问题参考了池上的评论:
[...] But if you're going to put an eval around every statement, just use RaiseError => 0. [...]
Run Code Online (Sandbox Code Playgroud)
在这个线程中。
如果我在这种情况下这样RaiseError做,我会得到什么?0
#!/usr/bin/env perl
use warnings;
use 5.10.1;
use DBI;
my $db = 'my_test_sqlite_db.sqlite';
open my $fh, '>', $db or die $!;
close $fh or die $!;
my ( $dbh, $sth );
eval {
$dbh = DBI->connect( "DBI:SQLite:dbname=$db", "", "", {} );
};
if ( $@ ) { print $@ };
my $table = 'my_sqlite_table';
say …Run Code Online (Sandbox Code Playgroud) 这三个版本的行为都不一样吗?
use open qw( :encoding(UTF-8) :std );
use open qw( :encoding(UTF8) :std );
use open qw( :utf8 :std );
Run Code Online (Sandbox Code Playgroud)