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 )

Sunday, March 31, 2013

defined hash

The reason that defined(%hash) is deprecated is apparently that it returns some internal memory-allocation property of the hash, and that this property is true when the hash is undefined in the Perl sense of things (eg having been set to undef).

Just %h is a perfectly good test and returns false if %h has been set to undef or (undef) (which seems to be the same thing).

Furthermore, you can't undefine a hash or array by assigning undef to them:

  • %h = undef results in %h having one key, '', mapped to undef (and also gives an 'odd number of hash elements' warning)
  • @a = undef results in @a having one element, undef
The two correct ways to undefine an array or hash are:
  • undef %h
  • %h = ()

Wednesday, March 27, 2013

Windows start command

You need to start an application with spaces in its path - the answer is obvious right, put quotes round it?

start "C:\program files (x86)(stupid)\appname\app.exe"

Oh no. What start thinks you want there is to start no application at all, in a window with the title C:\program files (x86)(stupid)\appname\app.exe. Something I'm sure hundreds of users need to do every day.

start interprets the first quoted argument as a window title, so the way round this is to use

start "" "C:\program files (x86)(stupid)\appname\app.exe"

Friday, March 8, 2013

Redefining Perl built-ins

Suppose you're writing one of those 'extender' sort of packages that exports subs with nice friendly names. And you want to call one of the subs 'connect', or any other name that's also the name of a Perl built-in function.
  • You can't do seamlessly if the built-in function has complex arguments - eg print. You can test this by checking prototype("CORE::functionname") - if you get back a Perl prototype (eg "$$"), you can redefine the function as an exact replacement.
  • Put use subs "functionname" in your package
  • Define functionname in your package as you normally would
  • If you want the function to be overridden in ALL packages, even ones that haven't imported your extender package, export the function with *{"CORE::global::functionname"} = \&functionname as well as with *{"main::functionname"}. If you only want it to be overridden in importing packages, which is what I want if I'm just trying to give my 'extender' functions friendly names, export it with *{"main::functionname"} as usual.
  • It is sensible to point out in documentation that functionname replaces the built-in (so caller can say CORE::functionname if they wish to use the original)

http://perldoc.perl.org/perlsub.html#Overriding-Built-in-Functions

Thursday, March 7, 2013

De-crapping Windows 7

Although I've sidestepped W7 on the new laptop by using Ubuntu, I can't do that at work. So I'll be accumulating notes here on how to make W7 work properly.

Explorer - address bar: Alt-D to replace drop-down lists with an edit box that you can actually type a fucking location into

Get rid of fading/expanding windows etc: Control Panel - 'Ease of Access Center' - 'Make it easier to focus on tasks' [now 'Make things on screen easier to see'] - 'Turn off all unnecessary animations'

Get rid of rubbish flashy Alt-Tab previews: Control Panel\All Control Panel Items\Personalization, set Theme to 'Windows Classic'. (This gets rid of a whole load of other crap too, notably it sets the buttons, menus and title bars back to grown-up colours).

Taskbar: in Properties set 'Use small icons' and 'Never combine'.

Start menu: Untick both 'Store' options. You can also use Customise to get rid of some of the useless menu options, but All Programs can't be made to expand like it did in XP (unfixed bug).

  • Be warned that Windows 7 doesn't seem to be able to have MRU on the Run dialogue box without having MRU on the Start menu. I got round this by creating various batch files with very short names to run the things I want most often (like 'goe' - Go Explorer - to run Explorer). Well done Microsoft for forcing users to revert to the state of the art circa 1991.
  • It may be possible to get rid of the search box by going 'Start -> Control Panel -> Programs and Features -> Turn Windows Features On or Off -> uncheck Windows Search' but haven't tested that yet (and it probably fucks up a load of stuff behind the scenes, like taking IE off the desktop used to)
  • You can get your own items onto the main area of the Start Menu by dragging them out of Explorer and dropping them on the button. There doesn't seem to be a grown-up way of doing this.
Turn off various taskbar crappery like pinning and Office buttons thusly:
  1. Unpin everything you don't want.
  2. Use gpedit.msc to set the following settings in User Configuration/Administrative Templates/Start Menu and Taskbar to Enabled:
    • Do not allow pinning programs to the Taskbar
    • Remove pinned programs from the Taskbar
    • Remove pinned programs from the Start Menu

This may not be W7-specific, but with a new computer you may have unwittingly installed intellipoint mouse features. This will stop middle/wheel click from closing tabs in Firefox - instead the button takes you into some bizarre fourth-dimensional space between applications, presumably a 'feature' in W7. Use Control Panel/Mouse to set the middle button action back to 'middle-click'.

I love the implicit admission in 'Turn off all unnecessary animations' btw. They might as well have an option labelled 'Make Windows 7 less crap'.

All still working October 2015 - NB the change in the Ease of Access 'Center''s subheadings.

Saturday, March 2, 2013

Perl hash slices

Well blow me tight. Not only can you do hash slices in Perl, but that functionality's been there since Perl 4 at least.

I suppose my understanding of Perl must have been built in such a logical way that it ran a hundred years to the day...

So just as you might have this:

 
@a = (1,2,3); 
print @a[1,2];
and get '23', then you might have this:
%a = qw(alpha 1 beta 2 gaga 3); 
print @a{ qw(beta gaga) };
and also get '23'.

NB that it is an @ and not a % as the first character there.

To slice a hash reference just replace @a above with @$r or @{$r} .

(New) You can also slice in a sort of 'select column' way, where your result is a subset of the hash's keys and values rather than just values:

%a = qw(alpha 1 beta 2 gaga 3); 
%b = %a{ qw(beta gaga) };
and end up with %b being (beta => 2, gaga => 3);

To do that with a reference, replace %a above with %$r or %{$r} .

This works in Perl 5.24 but not in 5.8.2 .

Friday, March 1, 2013

The Deacon's Masterpiece

I've always loved the 'Deacon's Masterpiece' reference in Weinberg's excellent Psychology of Computer Programming - it was
built in such a logical way
It ran a hundred years to the day.
I always imagined the 'Masterpiece' to be some kind of wonderful watch or clock that kept time far faster than necessary, and whenever I found a bug in my code, I'd think 'Yeah, I built it in such a logical way / It ran a hundred years to a day.'

So I'm a bit disappointed, having finally followed up the reference, to find that it's a just some rustic humour by Oliver Wendell Holmes about a 'wonderful one-hoss shay' - that's a chaise to us, one of those bouncy little horse-drawn carriages.

Followers

Blog Archive