1. subroutine
@this = bigger_than(5,1,5,15,30); # @this gets (15,30)
3.File-Level my( ) Variables
The my() operator can also be used at the outermost level of your program, outside of
any subroutines or blocks. While this isn't really a "local" variable in the sense
defined above, it's actually rather useful,especially when used in conjunction with a
Perl pragma:
use strict;
A pragma is a compiler directive. Other directives include those to set up integer
arithmetic, overload numeric operators, or request more verbose warnings and error
messages.
If you place this pragma at the beginning of your file, you will no longer be able
to use variables (scalars,arrays, and hashes) until you have first "declared" them.
And you declare them with my(), like so:
That last statement will be flagged at compile time as an error, because it referred
to a variable that had not previously been declared with my (that is, @c). In other
words,your program won't even start running unless every single variable being used
has been declared. The advantages of forcing variable declarations are twofold:
1. Your programs will run slightly faster (variables created with my are accessed
slightly faster than ordinary variables).
Variables created with my() are not found in any package.
2. You'll catch mistakes in typing much faster, because you'll no longer be able
to accidentally reference a nonexisting variable named $freed when you wanted $fred.
4.The last Statement
6.The redo Statement
7.Expression Modifiers
8.&& and || as Control Structures
9.Opening and Closing a Filehandle
10.A Slight Diversion: die
11.Using Filehandles
1
sub
say {
2 print " $_[0], $_[1]! " n " ;
3 }
4 say( " hello " , " world " ); # hello world, once again
5 say( " goodbye " , " cruel world " ); # silent movie lament
6
2.Private Variables in Functions
2 print " $_[0], $_[1]! " n " ;
3 }
4 say( " hello " , " world " ); # hello world, once again
5 say( " goodbye " , " cruel world " ); # silent movie lament
6
1
sub
bigger_than {
2 my ( $n , @values ); # create some local variables
3 ( $n , @values ) = @_ ; # split args into limit and values
4 my ( @result ); # temporary for holding the return value
5 foreach $_ ( @values ) { # step through the arg list
6 if ( $_ > $n ) { # is it eligible?
7 push ( @result , $_ ); # add it
8 }
9 }
10 return @result ; # return the final list
11 }
# some invocations:
2 my ( $n , @values ); # create some local variables
3 ( $n , @values ) = @_ ; # split args into limit and values
4 my ( @result ); # temporary for holding the return value
5 foreach $_ ( @values ) { # step through the arg list
6 if ( $_ > $n ) { # is it eligible?
7 push ( @result , $_ ); # add it
8 }
9 }
10 return @result ; # return the final list
11 }
@this = bigger_than(5,1,5,15,30); # @this gets (15,30)
3.File-Level my( ) Variables
The my() operator can also be used at the outermost level of your program, outside of
any subroutines or blocks. While this isn't really a "local" variable in the sense
defined above, it's actually rather useful,especially when used in conjunction with a
Perl pragma:
use strict;
A pragma is a compiler directive. Other directives include those to set up integer
arithmetic, overload numeric operators, or request more verbose warnings and error
messages.
If you place this pragma at the beginning of your file, you will no longer be able
to use variables (scalars,arrays, and hashes) until you have first "declared" them.
And you declare them with my(), like so:
1
use
strict;
2 my $a ; # starts as undef
3 my @b = qw(fred barney betty); # give initial value
4
5 push @b , qw(wilma); # cannot leave her out
6 @c = sort @b ; # WILL NOT COMPILE
2 my $a ; # starts as undef
3 my @b = qw(fred barney betty); # give initial value
4
5 push @b , qw(wilma); # cannot leave her out
6 @c = sort @b ; # WILL NOT COMPILE
That last statement will be flagged at compile time as an error, because it referred
to a variable that had not previously been declared with my (that is, @c). In other
words,your program won't even start running unless every single variable being used
has been declared. The advantages of forcing variable declarations are twofold:
1. Your programs will run slightly faster (variables created with my are accessed
slightly faster than ordinary variables).
Variables created with my() are not found in any package.
2. You'll catch mistakes in typing much faster, because you'll no longer be able
to accidentally reference a nonexisting variable named $freed when you wanted $fred.
4.The last Statement
1
while
(something) {
2 something;
3 something;
4 something;
5 if (somecondition) {
6 somethingorother;
7 somethingorother;
8 last ; # break out of the while loop
9 }
10 morethings;
11 morethings;
12 }
13 # last comes here
5.The next Statement
2 something;
3 something;
4 something;
5 if (somecondition) {
6 somethingorother;
7 somethingorother;
8 last ; # break out of the while loop
9 }
10 morethings;
11 morethings;
12 }
13 # last comes here
1
while
(something) {
2 firstpart;
3 firstpart;
4 firstpart;
5 if (somecondition) {
6 somepart;
7 somepart;
8 next ;
9 }
10 otherpart;
11 otherpart;
12 # next comes here
13 }
2 firstpart;
3 firstpart;
4 firstpart;
5 if (somecondition) {
6 somepart;
7 somepart;
8 next ;
9 }
10 otherpart;
11 otherpart;
12 # next comes here
13 }
6.The redo Statement
1
while
(somecondition) {
2 # redo comes here
3 something;
4 something;
5 something;
6 if (somecondition) {
7 somestuff;
8 somestuff;
9 redo ;
10 }
11 morething;
12 morething;
13 morething;
14 }
2 # redo comes here
3 something;
4 something;
5 something;
6 if (somecondition) {
7 somestuff;
8 somestuff;
9 redo ;
10 }
11 morething;
12 morething;
13 morething;
14 }
7.Expression Modifiers
1
exp2
unless
exp1;
#
like: unless (exp1) { exp2; }
2 exp2 while exp1; # like: while (exp1) { exp2; }
3 exp2 until exp1; # like: until (exp1) { exp2; }
4 some_expression if control_expression; # like if (control_expression) { some_expression; }
2 exp2 while exp1; # like: while (exp1) { exp2; }
3 exp2 until exp1; # like: until (exp1) { exp2; }
4 some_expression if control_expression; # like if (control_expression) { some_expression; }
8.&& and || as Control Structures
1
if
(this) { that; }
#
one way
2 that if this; # another way
3
4 #Here's a third (and believe it or not, there are still others):
5 this && that;
6 #Likewise, the logical-or works like the unless statement (or unless modifier). So you can replace:
7 unless (this) { that; }
8 #with:
9 this || that;
2 that if this; # another way
3
4 #Here's a third (and believe it or not, there are still others):
5 this && that;
6 #Likewise, the logical-or works like the unless statement (or unless modifier). So you can replace:
7 unless (this) { that; }
8 #with:
9 this || that;
9.Opening and Closing a Filehandle
1
open
(FILEHANDLE
,
"
somename
"
);
#
opens the filehandle for reading
2
3 open (OUT , " >outfile " ); # open a file for writing
4
5 open (LOGFILE , " >>mylogfile " ); # you can open a file for appending
6 close(LOGFILE);
2
3 open (OUT , " >outfile " ); # open a file for writing
4
5 open (LOGFILE , " >>mylogfile " ); # you can open a file for appending
6 close(LOGFILE);
10.A Slight Diversion: die
1
open
(DATAPLACE
,
"
>/tmp/dataplace
"
)
||
2 die " Sorry, I couldn't create /tmp/dataplace\n " ;
3
4 die " you gravy-sucking pigs " ; # prints the file and line number
5
6 die " you gravy-sucking pigs\n " ; # print either
7
8 open ( LOG , " >>logfile " ) || die " cannot append: $! " ; # $!: most recent operating system error
2 die " Sorry, I couldn't create /tmp/dataplace\n " ;
3
4 die " you gravy-sucking pigs " ; # prints the file and line number
5
6 die " you gravy-sucking pigs\n " ; # print either
7
8 open ( LOG , " >>logfile " ) || die " cannot append: $! " ; # $!: most recent operating system error
11.Using Filehandles
1
open
(EP
,
"
/etc/passwd
"
);
2 while ( < EP > ) {
3 chomp ;
4 print " I saw $_ in the password file!\n " ;
5 }
6 close(EP);
2 while ( < EP > ) {
3 chomp ;
4 print " I saw $_ in the password file!\n " ;
5 }
6 close(EP);
1
open
(OUT
,
"
>outfile
"
);
#
open a file for writing
2 print OUT " What's going on in this file " ;
3 close (OUT);
2 print OUT " What's going on in this file " ;
3 close (OUT);