Difference between revisions of "Perl"

From Dreamwidth Notes
Jump to: navigation, search
(merge in contents of Perl Tips page)
(Perl Tips: expand on string interp; +printf/sprintf for clarity)
Line 20: Line 20:
 
=== 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]]

Revision as of 03:03, 6 April 2013

Beginning programming in Perl

Perl Documentation

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).