Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1503138
  • 博文数量: 289
  • 博客积分: 11086
  • 博客等级: 上将
  • 技术积分: 3291
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-22 17:06
个人简介

徐小玉的博客。

文章分类

全部博文(289)

文章存档

2023年(6)

2022年(1)

2021年(2)

2020年(9)

2019年(9)

2018年(6)

2017年(10)

2016年(10)

2014年(3)

2013年(4)

2011年(12)

2010年(16)

2009年(14)

2008年(119)

2007年(48)

2006年(20)

我的朋友

分类: PERL

2019-11-01 11:37:39

把大象装入冰箱,需要三步。

在perl中装载自己的模块,也同样需要三步。
(MacOS下测试通过。)


1: 先创建一个自己的模块库:
/Users/mc/codes/perl/mylib

2:在这个模块库下,创建一个模块:average.pl:
cat average.pl

#!/usr/bin/perl

package average; # Declare a package

     # Average a list of grades

   sub ave {

       my(@grades)=@_;

        my($num_of_grades)=$#grades + 1;

        foreach $grade ( @grades ){

             $total += $grade;

        }

       $total/$num_of_grades;   # What gets returned

    }

1; # Make sure the file returns true or require will not succeed!




3:在你的工作目录下,在你的程序中将这个模块库倒入。
我的工作目录是:/Users/mc/codes/perl 

/Users/mc/codes/perl/12_modules.pl:

#!/usr/bin/perl

unshift(@INC, "/Users/mc/codes/perl/mylib");

require "average.pl";

print "Enter your midterm scores.\n";

@scores=split(' ', );

printf "The average is %.1f.\n", average::ave(@scores);

# The ave subroutine is found in a file called average.pl





阅读(765) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~