目前日期文章:200810 (4)

瀏覽方式: 標題列表 簡短摘要

Source: http://www.perlmonks.org/?node_id=287647

open FILEHANDLE, 'somefile.txt' or die $!;
my $string = do { local $/; <FILEHANDLE> };

How it works
The first piece of this code is the do "function", which returns the value of the last expression executed in it's block. In this case, that last expression is simply <FILEHANDLE>.

The expression <FILEHANDLE> will either return the next line from the filehandle if it is called in scalar context, or it will return the entire contents from the filehandle as a list if called in list context:

my $scalar = <FILEHANDLE>;    # one line
my @array  = <FILEHANDLE>;    # whole file

The reason why <FILEHANDLE> only returns one line is because by default, the built-in Perl variable $/ is set to one newline. $/ is the input record seperator, and it can be used to modify the behavior of how many records are read in when you use the diamond operators (<FILEHANDLE>). The docs explain that if $/ is set to undef, then accessing <FILEHANDLE> in scalar context will grab everything until the end of the file:

undef $/;
my $scalar = <FILEHANDLE>;   # whole file

However, changing Perl's built-in variables can be dangerous. Imagine you wrote a module that others use. Inside this module you set $/ to undef, thinking that everywhere else $/ will be the default value. Well, wrong. You just changed $/ for everyone that uses your module. This is one of those few places where local is the right choice.

Which brings us to the FIRST expression in our do block:

local $/;

This is the same thing as explicitly assigning $/ to undef:

local $/ = undef;

But not the same as:

$/ = undef;           # Danger Will Robinson! Danger!

Because we are inside a do block when we use local, the value of $/ is temporarily changed, and we can rest assured that it will not affect code outside of our block (or scope). If we were not inside another block or scope, local $/ will only affect the package it was encountered in, but it's better to contain local $/ inside a temporay scope, unless you enjoy debugging hard to find bugs. 

wenching520 發表在 痞客邦 留言(0) 人氣()

http://www.perlmonks.org/?node_id=517176

$array = \(); doesn't create a reference to an empty array. You probably want $array = [];

\() creates individual references to each entity inside of the ().

Since there is no entity inside of your (), no reference is created.

[] is the anonymous array constructor. That's what you're looking for.

$array = [];

wenching520 發表在 痞客邦 留言(0) 人氣()

http://code.activestate.com/recipes/499314/

import re
starts = [match.start() for match in re.finditer(re.escape(S), T)]

finditer() won't find overlapping strings. The regular expression methods, including finditer(), find non-overlapping matches. To find overlapping matches you need a loop.

wenching520 發表在 痞客邦 留言(0) 人氣()

http://www.perl.com/pub/a/2004/06/18/variables.html

The Field Record Separators

Next, we'll look at $/ and $\ which are the input and output record separators respectively.

wenching520 發表在 痞客邦 留言(0) 人氣()