Recent Notes
Displaying keyword search results 1 - 10
Created by Dr. Xi on June 20, 2011 15:18:06
Last update: June 20, 2011 15:18:06
Perl does not have built-in functions to get the base name or dir name from a file path. But on the Unix platforms we can rely on the executables basename and dirname .
#!/usr/bin/perl
$f = '/A path with/space "/file...
The quotes, escapes, and chomp are necessary:
# This fails when there's a space in $f
$base =...
Created by jinx on April 20, 2011 08:40:38
Last update: April 20, 2011 08:48:09
The PHP function time returns the current time in seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). This is the same function as the Perl time() .
<?php
print time() . "\n";
?>
Calling mktime without any arguments does the same:
<?php
echo mktime(), "\n";
?>
Created by Dr. Xi on March 28, 2011 11:11:33
Last update: March 28, 2011 11:13:21
grep is a versatile command with many variations (grep, egrep, fgrep, then various implementations). It uses a regula expression (regex) pattern to filter input. But then there are basic and extended flavors of regex - leading to even more confusion. And, beware that there are lots of bad examples of regex in the wild... There are two critical questions to ask when you use grep: which grep implementation are you using? what is the flavor of the regex? Here are some examples for gnu grep v2.7:
# Find all numbers (no decimal point), basic regex... Use the -o flag to show only the matching part instead of the whole matching line: grep -o -E '\b[0-9]{2}\b' The good thing about the gnu grep is that it...
Created by Dr. Xi on February 09, 2011 15:35:41
Last update: February 09, 2011 15:36:08
Perl BEGIN and END blocks are executed at the beginning and at the end of a running Perl program. By the Perl doc : A BEGIN code block is executed a s soon as possible , that is, the moment it is completely defined, even before the rest of the containing file (or string) is parsed . You may have multiple BEGIN blocks within a file (or eval'ed string); they will execute in order of definition. Because a BEGIN code block executes immediately, it can pull in definitions of subroutines and such from other files in time to be visible to the rest of the compile and run time. Once a BEGIN has run, it is immediately undefined and any code it used is returned...
Created by Dr. Xi on February 09, 2011 13:47:50
Last update: February 09, 2011 13:47:50
This note demonstrates a technique to load a module generated at runtime, whose name is pre-determined but path is created at runtime.
Sample module code:
# DO NOT EDIT!
# This module is generated at ru...
Method one (use require ):
C:\>perl
$rpath = 'runtimepath';
require "${...
Method two (use use with eval ):
C:\>perl
$rpath = 'runtimepath';
eval("use l...
Created by Dr. Xi on February 09, 2011 13:27:51
Last update: February 09, 2011 13:27:51
By the perldoc ,
use Module LIST;
is exactly equivalent to:
BEGIN { require Module; Module->import( LIST); }
Because of the BEGIN block, use is executed immediately. Therefore, it is not suitable for lazy loading of modules at runtime.
use does not work with a runtime variable:
C:\>perl
$cgi = "CGI";
require $cgi;
prin...
require works:
C:\>perl
$cgi = 'CGI';
require "${cgi}.pm";
...
Also, file extension is required if require is not passed a bareword:
// this works
require CGI;
// so does th...
Created by Dr. Xi on February 09, 2011 12:29:51
Last update: February 09, 2011 12:29:51
Perl package variables are accessed with the expression: $PackageName::variableName (for scalar, similarly for vector etc), not PackageName::$VariableName .
Example:
C:\>perl
use CGI;
print $CGI::VERSION;
^Z...
Created by Dr. Xi on February 03, 2011 13:47:22
Last update: February 03, 2011 13:47:22
Read a directory and sort files by modified date (oldest to newest):
$dirname = ".";
opendir(DIR, $dirname) or die $...
Same, but with glob (note that the output is a bit different):
$dirname = ".";
@files = sort { -M $b <=> -M $...
Switch the places of $a and $b to order from newest to oldest.
Created by Dr. Xi on November 18, 2010 22:13:36
Last update: November 18, 2010 22:13:36
You can use the eval function to substitute a value defined by a variable. Suppose you have a string literal $a = 'a $string' and you want Perl to substitute $string with the value of the $string variable. This is normally not a problem. Because if you use double quote, Perl does the interpretation automatically:
$string = 'theactual';
$a = "a $string";
pri...
But this doesn't work if the value of $string isn't available when you define the template $a . In this case, you have to use single quote to preserve the template definition. But you can use eval to do the replacement when the value of $string becomes available:
#!/usr/bin/perl
$a = 'a $string';
$string = ...
Created by Dr. Xi on November 15, 2010 21:38:07
Last update: November 15, 2010 22:07:16
Use the e switch to call a subroutine in regex replacement:
C:\>perl
@a = ('a', 'b', 'c');
for (@a) {
...
Without the e switch, the above would look like:
C:\>perl
@a = ('a', 'b', 'c');
for (@a) {
...
From the perldoc page for s/PATTERN/REPLACEMENT/msixpogce :
e Evaluate the right side as an expression.
ee Evaluate the right side as a string then eval the result.