从我读过的关于使用Perl模块的所有内容来看,基本用法是:
.pm扩展名的模块文件,其中包含语句package <name>,其中<name>是没有扩展名的模块的文件名.use <name>;.我正在编码的应用程序有一个主代码脚本,它使用大约5个模块.我忘记package <name>在模块中包含该语句,但我的代码仍然可以正常运行该use <name>语句.我开始收到Undefined subroutine其中一个模块的错误,所以我将package语句添加到每个模块中.现在其余的模块停止工作了.是什么赋予了?
例:
mainapp.pl
#!/usr/bin/perl
use UtyDate;
my $rowDate = CurrentDate("YYYYMMDD");
Run Code Online (Sandbox Code Playgroud)
UtyDate.pm
#!/usr/bin/perl
package UtyDate;
sub CurrentDate
{
#logic
}
return 1;
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,我收到错误Undefined subroutine &main::CurrentDate called at....但是,如果我package UtyDate;从UtyDate.pm中删除该行,我没有错误.这种情况存在于几个但不是所有模块中.
显然有很多代码我没有展示,但我很困惑,我没有展示的任何代码可能会影响我在这里展示的包/使用结构.
在我的Perl脚本中,我获得了可能包含环境变量的文件路径字符串,例如$FONTS/test.ttf或$TMP/file.txt.
我现在想要打开这样的文件:
open my $handle, "<$filename" or die $!;
Run Code Online (Sandbox Code Playgroud)
我现在如何在打开调用之前扩展环境变量,例如bash shell会做什么?
有Perl模块名称,如Moose和MooseX或DBI和DBIx或Catalyst和CatalystX或Mojo和MojoX或PPI和PPIx等.
X后缀是什么意思?
例如,在:
int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
int *outl, unsigned char *in, int inl);
Run Code Online (Sandbox Code Playgroud)
......可以out== in?
我有一个ViewPager和一个GridView显示月历.将GridView有大约30种元素和刷卡左,右是很慢的.我做了一个示例项目来测试没有任何数据库访问,它也很慢.也许有人看到了问题,或者我是否必须对页面进行异步加载?
这是一个元素的布局GridView.我想在任何元素中显示9个小的5x5像素图标,但没有它们,它也很慢.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/Layout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="1dp">
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:id="@+id/date" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginLeft="5dp" android:gravity="center_vertical" android:textSize="14sp" android:textStyle="bold"/>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff">
<ImageView android:id="@+id/dot1" android:layout_width="wrap_content" android:drawingCacheQuality="auto" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:background="#ffffff" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:paddingBottom="1dp" android:paddingLeft="1dp" android:paddingTop="5dp"/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff">
<ImageView android:id="@+id/dot2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:background="#ffffff" android:paddingBottom="1dp" android:paddingLeft="1dp"/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff">
<ImageView android:id="@+id/dot3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_gravity="right" android:background="#ffffff" …Run Code Online (Sandbox Code Playgroud) 我有一个解析环境(Marpa :: R2 :: Scanless)需要使用单个Perl regexp字符类来控制标记化.我有一些令人难以置信的东西似乎不适合任何现有的角色类.所以,在perlunicode文档中挖掘之后,我想出了以下代码,除了它没有按预期工作.我希望看到一排点穿插着所有非字母数字(除了parens).相反,我得到一个关于无法找到字符类的运行时错误.
#!/usr/bin/env perl
use 5.018;
use utf8;
local $| = 1;
for my $i (map { chr($_) } 32 .. 127) {
if ($i =~ /\p{Magic::Wow}/) {
print $i;
}
else {
print ".";
}
}
package Magic;
sub Wow {
return <<'MAGIC';
+utf8::Assigned
-utf8::Letter
-utf8::Number
-0028
-0029
MAGIC
}
1;
Run Code Online (Sandbox Code Playgroud)
任何提示,提示,技巧或建议?
存在几个可以显示变量名称而不需要程序员明确重复名称的转储器.
› perl -MData::Dumper::Simple -e'my $foo = 42; print Dumper($foo)'
$foo = 42;
Run Code Online (Sandbox Code Playgroud)
诡计是源过滤器(经常中断).
› perl -MDDS -e'my $foo = 42; DumpLex $foo'
$foo = 42;
Run Code Online (Sandbox Code Playgroud)
技巧是PadWalker.
它们在某种程度上也适用于其他类型的变量,但切片或其他复杂表达式存在问题.
可以利用哪个现代(5.10之后)eval技巧来制作以下示例转储器(如:数据结构查看器,不可用的代码生成器)?重点是始终打印好的名称,接受多个表达式,而不需要使用额外的参考级别来更改表达式.
use 5.020; use Syntax::Construct qw(%slice);
use strictures;
use Acme::Hypothetical::Dumper 'd';
my %foo = (
Me => 'person',
You => 'beloved one',
Them => 'space aliens',
);
d %foo, $foo{'Me'}, @foo{qw(You Me)}, %foo{qw(You Me)};
# %foo = ('Me' => 'person', 'Them' => 'space aliens', 'You' => 'beloved one');
# $foo{'Me'} …Run Code Online (Sandbox Code Playgroud) 测试用例:
use 5.026;
use Perl6::Controls qw(for);
for (1..10) -> $n {
say $n;
}
loop {};
Run Code Online (Sandbox Code Playgroud)
期望:
没有包或对象引用,无法调用方法"循环"
拿到:
无限循环
让我们foo分或方法.我编写了一个阻塞和异步变体,因此从外部看,本质区别在于返回值.我首先想到在签名中指定它,但遗憾的是调度程序只查看传入的结尾而不是两者:
> multi sub foo (--> Promise) {}; multi sub foo (--> Cool) {};
> my Promise $p = foo
Ambiguous call to 'foo(...)'; these signatures all match:
:( --> Promise)
:( --> Cool)
in block <unit> at <unknown file> line 1
Run Code Online (Sandbox Code Playgroud)
我应该Bool :$async在签名上添加一个吗?我应该像JS一样添加名称后缀(即foo和foo-async)吗?两个人对我都不太感兴趣.目前这个问题有哪些解决方案?
sub make-regex {
my $what's-in-the-box = rand > .5 ?? 'x' !! 'y';
/$what's-in-the-box/
}
my $lol-who-knows = make-regex;
$lol-who-knows.gist.say;
Run Code Online (Sandbox Code Playgroud)
您如何看待正则表达式(即x或y)的内部结构?强制匹配不是解决方案。
perl ×6
perl-module ×2
android ×1
baseadapter ×1
gridview ×1
openssl ×1
performance ×1
perl6 ×1
raku ×1
regex ×1
signature ×1
unicode ×1