use takes effect as soon as it is compiled - even before BEGIN, or more precisely, before any BEGIN blocks that are read after it, because use is effectively inside its own BEGIN block.
And as the documentation points out (I now notice) this is why you can't skip a use by putting it in the false branch of an if. There is a use if pragma for this purpose instead. Or you could use require.
I shall be thinking of use as a compiler directive from now on.
Anti-nuisance lawsuit warning: The purpose of these notes is to remind me, Zoegond, of stuff or to help me work stuff out. They may contain mistakes.
Quick
- ($a, $b....) = unpack("A2A7...", $packed)
- push( array, list )
Tuesday, March 27, 2012
Perl file::find
wanted
When you give File::Find a subroutine reference, the 'wanted' subroutine will find $_ set to the file base name - it will not find element 0 of @_ set to the file base name. Element 0 of @_ appears to be a reference to a hash whose keys resemble the File::Find variables (so it's probably a reference to a File::Find object).
So I should have
sub process {
local ($file) = ($_);
...
not
sub process {
local ($file) = (@_);
...
as I normally would.
preprocess
The 'preprocess' sub does get passed arguments in @_ - an array of strings representing the directory's contents. The strings don't have the directory path prefixed.
If a name isn't included in the list you return from preprocess, not only does it not get passed to wanted, it doesn't get searched (if it's a directory) either.
When you give File::Find a subroutine reference, the 'wanted' subroutine will find $_ set to the file base name - it will not find element 0 of @_ set to the file base name. Element 0 of @_ appears to be a reference to a hash whose keys resemble the File::Find variables (so it's probably a reference to a File::Find object).
So I should have
sub process {
local ($file) = ($_);
...
not
sub process {
local ($file) = (@_);
...
as I normally would.
preprocess
The 'preprocess' sub does get passed arguments in @_ - an array of strings representing the directory's contents. The strings don't have the directory path prefixed.
If a name isn't included in the list you return from preprocess, not only does it not get passed to wanted, it doesn't get searched (if it's a directory) either.
Thursday, March 15, 2012
Cuisenaire tower
Wet dinnertimes always finished before we could finish building our Cuisenaire towers, so we never got to find out how tall the complete tower would be. The towers were built by laying two parallel rods E-W, then two more across them N-S, and so on upwards, starting with the longest rods.
Using the rod quantities from the modern 'International' set (sadly the rods, 35 years on, are now plastic rather than wood)
In practice it would have been impossible to make many stable layers with the shorter rods (4cm downwards) so the maximum practical height would have been about 50cm.
The theoretical maximum using the 2-per-layer system would have been 99cm; one 1cm block on top would have reached the 100cm level, and I suppose under laboratory conditions the other 105 1cm blocks could have been stacked on top to reach 205cm.
Using the rod quantities from the modern 'International' set (sadly the rods, 35 years on, are now plastic rather than wood)
Rod colour Rod length (cm) Rods Layers Total height reached (cm)
Orange 10 10 5 5
Blue 9 12 6 11
Brown 8 12 6 17
Black 7 14 7 24
Dk green 6 16 8 32
Yellow 5 20 10 42
Purple 4 28 14 56
Lt green 3 36 18 74
Red 2 50 25 99
White 1 106 53 *
In practice it would have been impossible to make many stable layers with the shorter rods (4cm downwards) so the maximum practical height would have been about 50cm.
The theoretical maximum using the 2-per-layer system would have been 99cm; one 1cm block on top would have reached the 100cm level, and I suppose under laboratory conditions the other 105 1cm blocks could have been stacked on top to reach 205cm.
Monday, March 12, 2012
Sunday, March 11, 2012
Modify searches via Firefox search bar
Information: Firefox provides a Search Bar to allow quick searches. But you can't easily edit the search engine parameters.
Intention: To edit the Google search in the Search Bar to use 'verbatim' mode (aka 'work properly' mode).
Method: The account here (under 'How do I customise a search engine in 30 seconds') is correct. But an additional step is required: Firefox secretly caches the Search Bar configuration in two files in the profile directory, search.json and search.sqlite. Changes made to (eg) google.xml in the searchplugins directory are not picked up if they don't match these two files.
The solution is to (with Firefox closed) delete search.json and search.sqlite. Firefox will rebuild them, incorporating your changes, next time it starts.
Incidentally, to achieve the original intention the addition to google.xml is
<Param name="tbs" value="li:1"/>
(apologies for the misleading original title of this post) Btw, here's a precised copy of 'How do I customise a search engine in 30 seconds' in case it gets DMCAed by the Nazis:
Intention: To edit the Google search in the Search Bar to use 'verbatim' mode (aka 'work properly' mode).
Method: The account here (under 'How do I customise a search engine in 30 seconds') is correct. But an additional step is required: Firefox secretly caches the Search Bar configuration in two files in the profile directory, search.json and search.sqlite. Changes made to (eg) google.xml in the searchplugins directory are not picked up if they don't match these two files.
The solution is to (with Firefox closed) delete search.json and search.sqlite. Firefox will rebuild them, incorporating your changes, next time it starts.
Incidentally, to achieve the original intention the addition to google.xml is
<Param name="tbs" value="li:1"/>
(apologies for the misleading original title of this post) Btw, here's a precised copy of 'How do I customise a search engine in 30 seconds' in case it gets DMCAed by the Nazis:
Open the searchplugins folder located inside the Firefox installation directory. Open the file google.src with a text editor like notepad and make a simple change in the following lineWith Firefox Portable you'll find the searchplugins directory in ....FirefoxPortable\App\Firefox\browser , and the profile directory in ....FirefoxPortable\Data .
action="http://www.google.com/search"
change this to..
action="http://www.google.co.in/search" or
action="http://www.google.es/search"
Thursday, March 8, 2012
Perl::Tk tabbing
This is the encapsulation I've been using to fix the incredibly annoying broken tabbing feature on Tk Text boxes. NB it doesn't work on multiline Texts but then I've not so far wanted tab-to-next field behaviour on those.
I call it when I've created and packed my Texts:
enable_tab($textcontrol, $control_to_tab_to);
sub enable_tab {
my ($control, $nextcontrol) = @_;
# NB closure below - when the code runs, $control and $nextcontrol will have
# the values they do in this context here.
$control->bind("<Tab>" =>
sub {
$nextcontrol->focus();
$control->break();
} );
# make sure our new binding has priority
my @tags = $control->bindtags;
($tags[0], $tags[1]) = ($tags[1], $tags[0]);
$control->bindtags([@tags]);
}
I call it when I've created and packed my Texts:
enable_tab($textcontrol, $control_to_tab_to);
Unxutils tar
GNU tar helpfully ignores a leading slash in archived file paths - so \data\pages\t1.txt will be extracted into .\data\pages\t1.txt - unless -P is used to explicitly disable this.
Unxutils tar, although it has some other GNU tar features, doesn't have this one.
Unxutils tar, although it has some other GNU tar features, doesn't have this one.
Tuesday, March 6, 2012
Perl map and grep
map evaluates an expression (or block) for each member of a list in turn.
Each evaluation 'sees' $_ as set to the current list member.
The results of the applications are accumulated in a list, which map returns.
Eg
join(",", map $_ . " => " . $h{$_}, keys %h )
shows the members of a hash (map returns a list of strings which join agglutinates into a string).
In my experience the arguments to map should be left unparenthesised.
grep on the other hand evaluates the block/expression for each member of the list; the return value of grep is a list of the elements from LIST for which the block/expression returned a true value. (Or a count of those elements, in scalar context).
These two statements have the same result:
@out = grep {EXPR} @in;
@out = map { EXPR ? $_ : () } @in;
And a rough SQL metaphor:
map: select expr(item) from list
grep: select item from list where expr(item)=1
Each evaluation 'sees' $_ as set to the current list member.
The results of the applications are accumulated in a list, which map returns.
Eg
join(",", map $_ . " => " . $h{$_}, keys %h )
shows the members of a hash (map returns a list of strings which join agglutinates into a string).
In my experience the arguments to map should be left unparenthesised.
grep on the other hand evaluates the block/expression for each member of the list; the return value of grep is a list of the elements from LIST for which the block/expression returned a true value. (Or a count of those elements, in scalar context).
These two statements have the same result:
@out = grep {EXPR} @in;
@out = map { EXPR ? $_ : () } @in;
And a rough SQL metaphor:
map: select expr(item) from list
grep: select item from list where expr(item)=1
Subscribe to:
Comments (Atom)