Difference between revisions of "Perl"

From Dreamwidth Notes
Jump to: navigation, search
(merge in contents of Perl Tips page)
(Perl Documentation: +perldoc; +commonly referenced doc pages)
 
(One intermediate revision by the same user not shown)
Line 9: Line 9:
 
* [http://search.cpan.org/ Module documentation]
 
* [http://search.cpan.org/ Module documentation]
 
* [http://faq.perl.org/ FAQ]
 
* [http://faq.perl.org/ FAQ]
 +
* Too, a linux or Mac OS X machine with Perl installed will almost always have the <tt>perldoc</tt> tool installed...so if you like, you can also access all of the above information from the command line:
 +
** <tt>perldoc perl</tt> will give you a list of all the main perldoc pages
 +
** <tt>perldoc ''Module::Name''</tt> gives you documentation for that module
 +
** <tt>perldoc -f ''function''</tt> shows the documentation for that function
 +
** <tt>perldoc -q ''search-term''</tt> searches the Perl FAQ for the term given
 +
** and of course <tt>perldoc perldoc</tt> tells you more about how to use <tt>perldoc</tt> =)
 +
 +
=== Commonly referenced perldoc pages ===
 +
 +
* [http://perldoc.perl.org/perlsyn.html perlsyn] - Perl syntax
 +
* [http://perldoc.perl.org/perlop.html perlop] - Perl operators 
 +
* [http://perldoc.perl.org/perlfunc.html perlfunc] - Perl functions
 +
* [http://perldoc.perl.org/perlvar.html perlvar] - Perl special variables
 +
* [http://perldoc.perl.org/perlreref.html perlreref] - Perl-style regular expression reference
 +
* [http://perldoc.perl.org/perlmodlib.html perlmodlib] - The Perl Standard Module Library
  
 
== Regular Expressions ==
 
== Regular Expressions ==
Line 20: Line 35:
 
=== Strings ===
 
=== Strings ===
  
==== Quoting variables inside text ====
+
==== Interpolating variables inside text ====
 +
 
 +
Most of the time, you can simply place a variable inside a double-quoted string, and Perl will expand it for you in the way you want:
 +
 
 +
<source lang="perl">
 +
my $foo = "Kim";
 +
print "Hi, $foo!";    # prints «Hi, Kim!»
 +
</source>
 +
 
 +
But if the variable name is ambiguous, you can use {} characters to set it apart like so:
  
 
<source lang="perl">$string = "${foo}worthy"</source>
 
<source lang="perl">$string = "${foo}worthy"</source>
  
 +
And this way it won't get confused with another variable like <tt>$foow</tt>, or <tt>$fooworth</tt>, or what have you.
 +
 +
==== Clarifying complicated string constructions using <tt>printf()</tt> or <tt>sprintf()</tt> ====
 +
 +
When you're trying to build up a complex string by assembling many different variables and bits of text, things can quickly get unreadable:
 +
 +
<source lang="perl">
 +
my $msg = "At " . DateTime->now() . ", " . $user->name . " did $action" .
 +
    ", which made " . $action->object . " become " . $action->result . ".";
 +
</source>
 +
 +
The Perl function <tt>[http://perldoc.perl.org/functions/sprintf.html sprintf()]</tt> lets you build up strings using a kind of mini-templating language.  You specify the literal text you want first, with markers that indicate where you want to insert a variable.  For instance, <tt>%s</tt> means "insert a string here".  Then list the variables you want inserted.
 +
 +
So, the above example could be reformatted like this:
 +
 +
<source lang="perl">
 +
my $msg = sprintf("At %s, %s did %s, which made %s become %s.",
 +
    DateTime->now(), $user->name, $action, $action->object, $action->result);
 +
</source>
 +
 +
<tt>[http://perldoc.perl.org/functions/printf.html printf()]</tt> works the same as <tt>sprintf()</tt>, except it immediately prints the string you built -- so, <tt>printf(''blah'')</tt> is pretty much the same as <tt>print sprintf(''blah'')</tt>.
  
 
[[Category: Development]]
 
[[Category: Development]]

Latest revision as of 04:08, 23 April 2013

Beginning programming in Perl

Perl Documentation

  • Manual Pages
  • Module documentation
  • FAQ
  • Too, a linux or Mac OS X machine with Perl installed will almost always have the perldoc tool installed...so if you like, you can also access all of the above information from the command line:
    • perldoc perl will give you a list of all the main perldoc pages
    • perldoc Module::Name gives you documentation for that module
    • perldoc -f function shows the documentation for that function
    • perldoc -q search-term searches the Perl FAQ for the term given
    • and of course perldoc perldoc tells you more about how to use perldoc =)

Commonly referenced perldoc pages

Regular Expressions

Perl programmers tend to make extensive use of regular expressions, but they are also used in many other tools (eg vim, less, grep).

Perl Tips

Strings

Interpolating variables inside text

Most of the time, you can simply place a variable inside a double-quoted string, and Perl will expand it for you in the way you want:

my $foo = "Kim";
print "Hi, $foo!";     # prints «Hi, Kim!»

But if the variable name is ambiguous, you can use {} characters to set it apart like so:

$string = "${foo}worthy"

And this way it won't get confused with another variable like $foow, or $fooworth, or what have you.

Clarifying complicated string constructions using printf() or sprintf()

When you're trying to build up a complex string by assembling many different variables and bits of text, things can quickly get unreadable:

my $msg = "At " . DateTime->now() . ", " . $user->name . " did $action" . 
    ", which made " . $action->object . " become " . $action->result . ".";

The Perl function sprintf() lets you build up strings using a kind of mini-templating language. You specify the literal text you want first, with markers that indicate where you want to insert a variable. For instance, %s means "insert a string here". Then list the variables you want inserted.

So, the above example could be reformatted like this:

my $msg = sprintf("At %s, %s did %s, which made %s become %s.", 
    DateTime->now(), $user->name, $action, $action->object, $action->result);

printf() works the same as sprintf(), except it immediately prints the string you built -- so, printf(blah) is pretty much the same as print sprintf(blah).