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 )

Thursday, November 22, 2012

Perl one-liners

Reverse order of lines:

pclip | perl -ne "chop; unshift (@a,$_); END { print join(qq/\n/,@a)}"

Friday, November 16, 2012

Perl :: and class methods

If you call an instance method as a static method - ie if you usually use it thus

$dalek->say("Exterminate");

and call it thus

Dalek::say("Exterminate");

then the method will NOT get the class name as its first argument, as I always assumed it would. It will only have the arguments explicitly supplied, which will probably mean it won't work, because in our example "Exterminate" will end up in the $inst or $self position in say.

To get the expected result, put

Dalek->say("Exterminate");

which I didn't think was allowed. This will supply "Dalek" as $_[0] as expected.

Or you could put

Dalek::say("Dalek", "Exterminate");

which I think is not as clear.

Actually, I now realise Perl doesn't recognise any difference between instance and static methods. What I should be saying above is that

Dalek::say("Exterminate")

means simply 'say("Exterminate") using the say subroutine in package Dalek', whereas

$dalek->say("Exterminate")

means 'Check that $dalek is a reference to something blessed, and if it is, call say with the name of the class it was blessed into as the first argument, and "Exterminate" as the second.
And for completeness, presumably the other example above

Dalek->say("Exterminate")

means 'Enquote the bareword to the left of ->, that gives us "Dalek", now call Dalek::say with "Dalek" as first argument and "Exterminate" as second.

index and undef

Be warned, the result of

index("hello", undef)

is 0, not -1.

Perl import and BEGIN

NB that code in a BEGIN block in a module runs before the call to import which is triggered by use-ing the module

import btw is a method like any other, the first argument will be the module name and subsequent arguments will be any strings that you placed after it. Eg with

use Extract "nodb";

the two arguments to Extract::import will be "Extract" and "nodb".

Followers

Blog Archive