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 = ×(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 <[email protected]>',
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
Perl Script and Deployment
猜你喜欢
转载自sillycat.iteye.com/blog/2269188
今日推荐
周排行