{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lesson5 - learning Perl ppt

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Spring, 2009 1 Perl 入门和提高 Lesson 5 周晓方 [email protected] Spring, 2009 2 " my ", " local ", and " our " (perl5.6+) 字典作用域 my declares a lexical variable totally hidden from the outside world 只用于当前作用域,不自动传递给所调用的子程序 可以模仿 C 语言的 auto 变量 要模仿 C 语言的 static 变量,可以这样写 动态作用域 local 自动传递给所调用的子程序 全局作用域 our (perl5.6+) use vars qw( 变量列表 ), use vars qw($frob @mung %seen) ; 缺省情况下都是全局变量 , 可以用 $v, $::v, $main::v 引用 { my($s) = 0; # 单独用一对 {} ,在里面定义 my 变量和子程序 sub getnext { $s++; } } # $s 对外不可见,但每次调用 getnext 之间都保留 $s 的值 print getnext; print getnext; print getnext; Spring, 2009 3 Sample a(); our $x=7; our $y = 17; a(); sub a { my $x = 10; local $y = 5; print "ax$x, ay$y\n"; b(); } sub b { print "bx$x, by$y\n"; } ax10, ay5 有字典变量 x ,有动态变量 y ,都是在 a 中定义的 bx, by5 没有全局、动态和字典变量 x ,有动态变量 y( a 中定义的 ) ax10, ay5 有字典变量 x ,有动态变量 y ,都是在 a 中定义的 bx7, by5 有全局变量 x( our 定义的 ) ,有动态变量 y( a 中定义的 ) Spring, 2009 4 Program • Perl 程序的一般结构 ( 务必用 my 定义变量 ) #!/usr/local/bin/perl -w use strict; use lib "path"; use module_names; require "sourcefile"; main code sub routine1 { } sub routine2 { } sub routine3 { } 1; __END__ pod usually starts here 回家作业 ( 附件 学号 -No.4.pl ) 1. 用级数展开 sin(x) (x<pi) 2. 编写子程序 sub mysin ,第一 个参数是 x, 第二个参数是 n, sin(x) 级数展开到第 n+1 3. 主程序调用 mysin(1,$n) ,其 $n=(1..10) ,显示每次计 算的结果,运行结果如下 : 1 0.833333333333333 2 0.841666666666667 3 0.841468253968254 4 0.841471009700176 5 0.841470984648068 6 0.841470984808658 7 0.841470984807894 8 0.841470984807897 9 0.841470984807897 10 0.841470984807897 Spring, 2009 5
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern