Coding Bash vs. Perl vs. Python (by Evan Liu)


Topics about function

Function: define and call function

In bash: function definition has no parameters within the parentheses, arguments are obtained by the positional parameters: $1,$2. the return value is obtained by 'echo $?' and must be numeric

In perl:arguments in subroutines are passed in the @_ array. the word 'subroutine' is the same as function, they are used interchangeably.

Tasks:
1.1. define a function that prints information about the program and arguments
2.2. call the function and print the return value 
		  
Bash Perl Python
  1. #1.1. define a function that prints information about the program and arguments
  2. func1() for w ; do echo "$w"; done
  3. func2() { for w ; do echo "$w"; done; echo $$; return 255; }
  4. ##print the func definition
  5. declare -f func1
  6. declare -f func2
  7. #2.2. call the function and print the return value
  8. echo $$
  9. ##function will be called in the same shell with a return code
  10. func2 param{1..3} #or func param1 param2 param3
  11. echo "the return value is $?"
  12. echo $$
  13. -------output----------
  1. #1.1. define a function that prints information about the program and arguments
  2. ##print the func definition
  3. func1 ()
  4. {
  5. for w in "$@";
  6. do
  7. echo "$w";
  8. done
  9. }
  10. func2 ()
  11. {
  12. for w in "$@";
  13. do
  14. echo "$w";
  15. done;
  16. echo $$;
  17. return 255
  18. }
  19. #2.2. call the function and print the return value
  20. 18824
  21. ##function will be called in the same shell with a return code
  22. param1
  23. param2
  24. param3
  25. 18824
  26. the return value is 255
  27. 18824
  1. #1.1. define a function that prints information about the program and arguments
  2. sub func{\
  3. print "\$0 returns the program name: $0\n";\
  4. print "\$_[0] returns the 1st argument: $_[0]\n";\
  5. print "\@_ returns the argument list: @_\n";\
  6. print "\$\$ returns the shell PID: $$\n";\
  7. print "\$#_ returns the argument number : 1+$#_\n";\
  8. return scalar(@_);}
  9. # or func (param1,param2,param3);
  10. $ret=func (param1..param3);
  11. print "the return value is $ret";
  12. -------output----------
  1. #1.1. define a function that prints information about the program and arguments
  2. # or func (param1,param2,param3);
  3. $0 returns the program name: -e
  4. $_[0] returns the 1st argument: param1
  5. @_ returns the argument list: param1 param2 param3
  6. $$ returns the shell PID: 18825
  7. $#_ returns the argument number : 1+2
  8. the return value is 3
  1. # function, no positional parameters?
  2. def func(p1,p2):
  3. print(p1, p2)
  4. func("param1","param2")
  5. # or
  6. tup=("param1","param2"); func(*tup)
  7. # use tuple as parameter
  8. def func2(*prms):
  9. print(prms) #*prms is tuple
  10. func2("one",2,3.0)
  11. tup=("one",2,3.0); func2(*tup)
  12. # use dict as parameter
  13. def func3(**ages):
  14. print(ages) #**ages is dictionary
  15. func3(dad=42,mom=48,lisa=7)
  16. # or
  17. dic={'dad':42,'mom':48,'lisa':7}; func3(**dic)
  18. # parameters with default value
  19. def func4(p1='tst1',p2='tst2'):
  20. print(p1+" "+p2)
  21. func4()
  22. -------output----------
  1. # function, no positional parameters?
  2. ('param1', 'param2')
  3. # or
  4. ('param1', 'param2')
  5. # use tuple as parameter
  6. ('one', 2, 3.0)
  7. ('one', 2, 3.0)
  8. # use dict as parameter
  9. {'dad': 42, 'lisa': 7, 'mom': 48}
  10. # or
  11. {'dad': 42, 'lisa': 7, 'mom': 48}
  12. # parameters with default value
  13. tst1 tst2

Function: generators and yield keyword

In python, generator is a "function", which will return a generator type, which will yield a series of values , instead of returning only a single value. 
generator function -> return type generator-->yield values by next() method
normal function     -> return a singal value

Tasks:

		  
Bash Perl Python
  1. #not applicable
  2. -------output----------
  1. #not applicable
  1. #not applicable
  2. -------output----------
  1. #not applicable
  1. def simple_generator():
  2. yield("Venus")
  3. yield("Mars")
  4. yield("Jupiter")
  5. yield("Saturn")
  6. # use the next() method
  7. x = simple_generator()
  8. print "the type of x is: ", type(x);
  9. print x.next(),x.next(),x.next(),x.next()
  10. # use for loop
  11. y = simple_generator()
  12. for i in y:
  13. print i,
  14. -------output----------
  1. # use the next() method
  2. the type of x is: <type 'generator'>
  3. Venus Mars Jupiter Saturn
  4. # use for loop
  5. Venus Mars Jupiter Saturn