blank string | if($mystring =~ /^\s*$/) {...} # Is $mystring blank? |
foreach |
@a=(1,2,3,4,5); foreach $element (@a){ print $element; } |
pattern matching |
$mystring =~ m/sheep/; # True if $mystring contains "sheep" $mystring !~ m/sheep/; $mystring =~ s/old/new/; # Replace old with new. $mystring =~ /\bsheep\b/i; # Boundary and case insensitive |
strict; | use strict; Using "use strict" will require the variables to be made private with the "my" operator. This avoids accidentally overwriting same-named variables in the main program. or use strict 'vars'; use vars qw($fee $fie $foe $foo @sic); # allow listed variables global |
triming |
# Triming leading and trailing white spaces: $mystring =~ s/^\s+//; $mystring =~ s/\s+$//; |
Sample | #!/usr/bin/perl -w use Env; use DBI; $dbd = 'Oracle'; require 'pl_include.perl'; require 'lib_err_log.perl'; ... die unless(env('DATA_DIR')); die unless($ENV{PATH}); ... usage() if ($#ARGV != 4); ... my ($dbinstance , $user, $project, $file_in, $file_out ) = @ARGV; chomp ($password = $datadir = $ENV{DATA_DIR}; open (FILEIN, "sort -t~ -k 4,4 $file_in |") || die "Can't open $file_in. Program stoped!\n\n"; $sql = "select FirstName, LastName from Employee where DEPT = :A"; $conn = DBI->connect ($dbinstance, $user, $password, $dbd); if (!$conn) { die "Error while trying to connect to database; $DBI::errstr\n"; } $cur = $conn->prepare($sql); die "Error preparing Select statement; $DBI::errstr\n" if ($DBI::err); ... $cur->bind_param_inout( ":A", \$DepartmentID, 24); # Bind once. while (defined ( $line = #$cur->bind_param( ":A", $DepartmentID ); Use "bind_param_inout" instead. chomp($line); @InputRec = split(/~/, $line . "fake~"); # Process record here... } ... $conn->disconnect; close(FILEIN); |