Perl Script and Deployment

本文深入探讨了Perl语言的关键特性,包括变量作用域、数组、哈希、子程序及返回值,通过实例展示了如何有效使用Perl进行编程。此外,文章还介绍了模块构建、单元测试、代码覆盖率评估以及构建流程,提供了完整的示例代码和测试用例,帮助开发者掌握Perl的高级应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Perl Script and Deployment

1 Recover the Perl Knowledge
http://sillycat.iteye.com/blog/1012882

my $a will not affect the global variables
$a = 3.14159;
{
my $a = 3;
println “In block, \$a = $a\n”;
println “In block, \$::a = $::a\n”;
}

In block, $a = 3
In block, $::a = 3.14159 $::a is the global variable.

my $array;

http://sillycat.iteye.com/blog/1012923

http://sillycat.iteye.com/blog/1012940

my %hash;

my $var = 1;
my @var = (1,2,3,4);
my %var;
$var{1} = 2;
$var{2} = 38;
print $var . “ “ . $var[3] . “ “ . $var{2}

console output:
1 4 38

sub and return value
my $return = &times(4);
print $return;

sub times {
my $max = shift;
my $total = 1;
for ( 1…$max){
$total *= $_;
}
return $total;
}

console output: 24 1*2*3*4 = 24

sub and list of parameters
my $return = &div(4,2);

sub div {
$_[0]/$_[1];
}

http://sillycat.iteye.com/blog/1017632

package Personal;

use Personal;
my $adv = Personal::adv()

http://sillycat.iteye.com/blog/1016428

http://sillycat.iteye.com/blog/1017590

2 How Sample Project Looks Like
>sudo cpan
cpan>install Devel::Cover

I have a pm file as my source codes
> cat lib/HelloWorld.pm

use strict;
use warnings;
package HelloWorld;

$HelloWorld::VERSION = '0.1';

__PACKAGE__->run( @ARGV ) unless caller();

sub run {
print "I'm a script!\n";
my $result = &hello();
print "result is " . $result . "\n";
}

sub hello {
return "Hello, Perl Build World!";
}

sub bye {
return "Goodbye, cruel world!";
}

sub repeat {
return 1;
}

sub argumentTest {
my ($booleanArg) = @_;

if (!defined($booleanArg)) {
return "null";
}
elsif ($booleanArg eq "false") {
return "false";
}
elsif ($booleanArg eq "true") {
return "true";
}
else {
return "unknown";
}

return "Unreachable code: cannot be covered";
}

1;

__END__

I have a test class called ModuleName.t
> cat t/HelloWorld.t

use strict;
use warnings;
use Test::More qw(no_plan);

# Verify module can be included via "use" pragma
BEGIN { use_ok('HelloWorld') };

# Verify module can be included via "require" pragma
require_ok( 'HelloWorld' );

# Test hello() routine using a regular expression
my $helloCall = HelloWorld::hello();
like($helloCall, qr/Hello, .*World/, "hello() RE test");

# Test hello_message() routine using a got/expected routine
is($helloCall, "Hello, Perl Build World!", "hello() IS test");

# Do not test bye() routine

# Test repeat() routine using a got/expected routine
for (my $ctr=1; $ctr<=10; $ctr++) {
my $repeatCall = HelloWorld::repeat();
is($repeatCall, 1, "repeat() IS test");
}

# Test argumentTest()
my $argumentTestCall1 = HelloWorld::argumentTest();
is($argumentTestCall1, "null", "argumentTest() IS null test");

# Test argumentTest("true")
my $argumentTestCall2 = HelloWorld::argumentTest("true");
is($argumentTestCall2, "true", "argumentTest() IS true test");

# Test argumentTest("false")
my $argumentTestCall3 = HelloWorld::argumentTest("false");
is($argumentTestCall3, "false", "argumentTest() IS false test");

# Test argumentTest(123)
my $argumentTestCall4 = HelloWorld::argumentTest(123);
is($argumentTestCall4, "unknown", "argumentTest() IS unknown test");

And my build.xml or similar
> cat Build.PL
use strict;
use warnings;
use Module::Build;

my $builder = Module::Build->new(
module_name => 'HelloWorld',
license => 'perl',
dist_abstract => 'HelloWorld short description',
dist_author => 'Author Name <luohuazju@gmail.com>',
build_requires => {
'Test::More' => '0.10',
},
);

$builder->create_build_script();

Here is how I Build/Run
> perl lib/HelloWorld.pm
I'm a script!
result is Hello, Perl Build World!

> perl Build.PL


> ./Build manifest

> ./Build test
t/HelloWorld.t .. ok
All tests successful.
Files=1, Tests=18, 0 wallclock secs ( 0.01 usr 0.01 sys + 0.02 cusr 0.00 csys = 0.04 CPU)
Result: PASS

> ./Build testcover

Install
> sudo ./Build install
Password:
Building HelloWorld
Installing /Library/Perl/5.18/HelloWorld.pm

Run it again
> perl /Library/Perl/5.18/HelloWorld.pm

References:
http://stackoverflow.com/questions/533553/perl-build-unit-testing-code-coverage-a-complete-working-example
http://mojolicio.us/
http://search.cpan.org/~leont/Module-Build-0.4214/lib/Module/Build.pm
https://robn.io/docker-perl/
http://sillycat.iteye.com/blog/2193773
http://www.vromans.org/johan/articles/makemaker.html

http://www.drdobbs.com/scripts-as-modules/184416165
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值