Class 9: Miscellaneous
Optional abusive example: camel.pl
TOPIC: Context
print "a", "b", "c"; vs. print "a" . "b" . "c";
sub x { return ("a", "b", "c") }
print "w", &x(), "y\n"; # vs.
print "w" . &x() . "y\n";
sub x {
my @array = ("a", "b", "c");
return @array;
}
print "w", &x(), "y\n"; # vs.
print "w" . &x() . "y\n";
$x = 4 + (2, 4, 6);
Array in scalar context = length; list in scalar context = last element!
Contexts: scalar, list, void, boolean, lvalue
Scalar context: number, string or boolean -- we can't tell which we have!
SUBTOPIC: wantarray
localtime -- e.g. assigning an array to a scalar -- what else?
wantarray vs defined(wantarray)
Example: context.pl
The scalar function forces scalar context in list context but there is no operator to force list context in a scalar context
Example: stuffit.pl
In a scalar context grep returns a count of the selected elements not the elements themselves -- e.g. like an array but an array is not created
Optional: What about HASH context?
Optional example: hashtest.pl
Even '..' behaves differently in scalar or list context:
TOPIC: Range Operators & the Yada-yada operator
..
The two-dot range operator, is two different operators depending on context. In list context, it returns a list of values counting up by one.
The range operator is useful for foreach loops and slicing arrays:
my @subset = @complete[5 .. 9];
In scalar context, the range operator is a bistable flip-flop. Each range operator maintains a boolean state, even across calls to a subroutine that contains it. It is false as long as its left operand is false. Once the left operand is true, the range operator stays true until the right operand is true, after which the range operator becomes false again.
if ( m/START/ .. m/END/ ) {
# process all the lines
# between START and END
...
}
...
The range operator can test its right operand and become false on the same evaluation on which it became true. If you don't want it to test the right operand until the next evaluation, use the three dot variation. In all other regards, the three dot range operator behaves like two dot one:
if ( m/thead/ ... m/thead/ ) {
# process all the lines
# between <thead> and </thead>
...
}
...
The three-dot operator, aka yada-yada, whatever, or et cetera, is a placeholder for missing code. It parses without error, but when you execute a yada-yada, Perl throws an exception. You can only use yada-yada to stand in for syntactically complete statements -- you can't use it to stand in for just part of a statement otherwise Perl might confuse it with the three-dot version of the range operator above.
Summarized from: http://perldoc.perl.org/perlop.html
TOPIC: Perl Poetry
study, write, study,
do review (each word) if time.
close book. sleep? what's that?
TOPIC: Bio Modules
Some of the non-BioPerl Bio::* Modules on CPAN
perl Makefile.PL PREFIX=...
make
make test
make install
But Macs need Apple Developer (XCode) software! (For "make", etc.)
You can use -I switch to tell Perl where to look for your libraries or you can set the operating system environment variable PERL5LIB
TOPIC: Tie
Object implementation independent of type being tied -- a scalar can be controlled by a hash object -- Tie mechanism maps between them.
Example: Stopwatch.pm stopwatch.pl
Example: Insensitive.pm histogram
Tie can be used to implement persistent data:
Example: phonedbm.pl words.pl (earlier example broken up)
Some Tie-based modules in Perl Core:
Some Tie-based modules on CPAN:
Exercise:
sleep;
pipe (drip, drip);
listen (drip, drip);
kill noises;
kill dripping;
close pipe soon, NOW;
sleep again;
listen (drip, drip);
sleep (not now);
exit (do it);
accept destiny, now;
alarm neighbors;
get the keys now, &;
#open (up, &survey the);
;
crypt of,darkness;
not a single; pipe here,anywhere;
# http://incompetech.com/gallimaufry/perl.html
TOPIC: Eval
Example: infix.pl
-n assumes an input loop around the script.
-l enables automatic line ending processing,
-e may be used to enter a single line of script.
Two forms, good: eval { ... } vs. evil: eval "..."
SUBTOPIC: Good eval { ... } and exception handling
Example: errors.pl
SUBTOPIC: Defunct Error.pm (catch & throw)
Although incorporated in the Root class of Bioperl, use of the "Error" module, which provides the syntactic sugar of try { ... }, catch { ... } on top of eval, is no longer recommended.
Optional example: banking.pl
The standard method of using eval {}; if ($@) {} is prone to subtle bugs, as its far too easy to stomp on the error in error handlers. And also eval isn't the nicest idiom. (From the description of the TryCatch module)
Alternatives to Error.pm module:
Errors are handled separately from where they happen (don't trickle up.)
Errors don't have to be combined with return values!
SUBTOPIC: Evil eval "..."
Can be used to get around shortcomings in Perl -- tr with $variables:
The idiom:
$_ = $string;
eval sprintf "tr/%s/%s/", map quotemeta, $oldlist, $newlist;
$string = $_;
But there's a performance cost.
Example: alphametic_tran.pl
Optional example: alphametic_hash.pl (use hash slices to avoid tr///)
Optional example: sevens.pl
Optional Google: four fours puzzle
Optional: http://cdl.best.vwh.net/Puzzles/Numbers
SUBTOPIC: Eval embedded in substitution: s///e
Example: sub_eval.pl
OPTIONAL SUBTOPIC (of Eval): Do
do returns the value of the last expression evaluated.
Inclusion of library modules is better done with "use" and "require", which also do error checking and raise exceptions if there are problems
TOPIC: Database
Perl uses DBI and DBD::Database
Example: whichdbd.pl
Example: database.pl
Example: sakila-test.pl
My essay on using prepare() and/or prepare_cached()
TOPIC: (Free) Perl IDEs
GOOGLE: site:perlmonks.org perl development tools
Features of a code (vs. text) editor:
Padre -- Perl Application Development and Refactoring Environment
Emacs -- with CPerl Mode
To replace the standard perl-mode with cperl-mode in all cases you need the following in your ~/.emacs file (or your InitFile):
;;; cperl-mode is preferred to perl-mode
(defalias 'perl-mode 'cperl-mode)
Perl plugins for multiple language IDEs:
Eclipse with EPIC (Eclipse Perl Integration)
Microsoft Windows-specific:
#gk_poetry 5 novembre 98 Guy Kastenbaum gkasten@filnet.fr
$s="tay here ";
open(YOUR,"eyes");
open(MY,"heart");
while (($time_is=<YOUR>)&&$s)
{
$time_is=~s/e/n/s;
$time_is=~s/t/e/m;
$time_is=~s/a/d/;
$time_is=~s/A/D/ism;
print MY $time_is."\n\t";
$s.="top " if $time_is=~/over/;
$s.="till " if $time_is=~/money/;
last if $s=~/tay here till top/;
}
close MY;
print "I ".((-f "heart")?"beat":$s) ;
close YOUR;$s;
__OPTIONAL__
OPTIONAL TOPIC: Bio::NEXUS
Application programming interface (API), available from CPAN and SourceForge. The 22 Bio::NEXUS modules define 351 methods. An object-oriented interface to reading, writing and manipulating the contents of NEXUS files. Also available as Python Bio.Nexus
Example: reroot.pl example D tree.nex
Problem with definition of equal, tutorial misleading regarding reroot, etc.
OPTIONAL TOPIC: Bio::Emboss
Allows Perl programmers to access functions of the EMBOSS
(European Molecular Biology Open Software Suite) package.
Example: seqret.c seqret.acd
Example: seqret.pl
Example: seqret_obj.pl
You can obtain a list of EMBOSS programs with the command wossname. Useful qualifiers for wossname are:
% wossname –alphabet –auto