Monthly Archives: May 2009

Perl – my, use vars, our, local, use strict

Some notes for me as I attempt to get a better understanding of Perl’s variable scoping rules. A work in progress…

  • Perl has 2 separate ‘namespaces’ for variables – package aka global (Perl 4) and my aka private aka lexical scoping (Perl 5)
  • the default package is main, this can be changed with the statement package foo; which then applies to all subsequent code until another package statement is encountered
  • lexical scoping is done with {} (either via subroutines/operators or ‘naked blocks’); the top level is the file itself (‘file level’).
  • use strict; (specifically use strict ‘vars’;) requires that variables either use a package qualifier (eg $main::foo) if they’re package variables or be lexical variables declared with my. You can get around this extra typing for package variables with use vars (or no strict ‘vars’ for a block)
  • our($foo,$bar) is like use vars qw($foo $bar), however it’s lexical ie can be restricted by {}’s (an equivalent no use vars doesn’t exist). Looked at it another way, our ‘spans’ package declarations (as it’s lexical) whereas use vars is ‘reset’ by package declarations.

Also:

  • dynamic scoping is done with local – see Wikipedia – Scope (programming). Lexical scoping is the norm in modern programming languages, dynamic scoping is a Lisp’y idea that uses a sort of stack for variable scope resolution, that can only be determined at runtime.  When to use local? Rarely… Seven Useful Uses of local
  • local only works with package variables not lexical/my variables; an example of where local can be useful is to temporarily change a Global Special Variable (eg $) $EFFECTIVE_GROUP_ID) before calling a subroutine: “local saves away the value of a package global and substitutes a new value for all code within and called from the block in which the local declaration is made” eg
{
    local $) = getgrnam(USER) if $< == 0;
    enable($host);
}
  • do C-style static variables in this manner:
        { my $seed = 1;
          sub my_rand {
            $seed = int(($seed * 1103515245 + 12345) / 65536) % 32768;
            return $seed;
          }
        }
  • xx

Some Links:

Firewalling on Solaris 10

Firewalling on Solaris 10:

  • config file: /etc/ipf/ipf.conf
  • flush all rules: ipf -Fa
  • reload: ipf -f /etc/ipf/ipf.conf

Email from Julian:

The native firewall that comes with Solaris is “ipf”.

Configuration files are in the directory /etc/ipf and the file is “ipf.conf”, NAT rules in “ipnat.conf”. Unlike iptables, where the configuration file is a series of “iptable” commands, “ipf.conf” is purely a configuration file. Traffiic must be enabled on each interface, so you have “pass in” to allow traffic in on interface A and a “pass out” to allow traffic out on interface B, if it is acting as a firewall, obviously this is not.

As of Solaris 10, processes are started via service manager. To check if ipf is running, you can:

# svcs -a |grep ipf
online May_05 svc:/network/ipfilter:default

“online” status tells you that it is running.

Commands to see what is happening.

“ipfstat”: show statistics, bytes in, bytes out etc.

“ipfstat -i” to display input running rule set

“ipfstat -o” to display output running rule set

“ipf -f /etc/ipf/ipf.conf” to load rules from config file.

“ipmon -s [file]” to have ipf log to “file”

To restart using service manager

“svcadm restart svc:/network/ipfilter:default”

See man page for “ipnat” for options to display NAT options.

Link from Rusty’s blog: http://ozlabs.org/~rusty/index.cgi/2006/08/15

Last word: Solaris’s version of tcpdump is “snoop”. So to monitor traffic: “snoop -d e1000g0 not port 22″ you can add “-v” etc.

Perl – use, require, import, and do

Some notes on Perl’s use, require, import and do. Quick notes for me; not meant to be authoritative…

  • use is done at ‘compile-time’ and require is done at ‘run-time’ (ie can conditionally load modules)
  • require is the older method, but use uses require to do it’s work:

use Foo;   # equivalent to:
require Foo; Foo->import();
.
use Foo qw (foo bar);   # equivalent to:
require Foo; Foo->import(qw(foo bar));
.
use Foo();   # equivalent to:
require Foo;   # ie don't import anything, not even the default things

  • a Library is a just a file of code; a Module has package Foo in it (and usually other stuff)
  • use only works with modules, whereas require works with both modules and libraries. Corollary: use only works with module names (Foo), whereas require also works with paths (eg ~/lib/foo.pm)
  • require Foo will check if Foo has already been loaded, whereas do Foo will unconditionally reload Foo
  • better practice is to write modules rather than librarys, to prevent namespace pollution. A simple module:

package Foo;   # minimal. Usually add things like:
use base qw (Exporter);
our @EXPORT = qw(qux slarken);   # keep this list small - namespace pollution

  • to use this module:

use lib '~/lib'; # add this to %INC
use Foo;   # loads module, imports symbols in @EXPORT
Foo->bar();   # correct
Foo::bar();   # works, but not for inherited methods
qux();   # works, due to export
bar();   # carks

Some links:

Syncing Palm Pilot with JPilot and visor on Ubuntu

(Updated May/09 for Ubuntu 8.10/Ibex): for a long time I couldn’t get JPilot syncing with my Palm Pilot on Ubuntu 8.04 and 8.10. Turns out I wasn’t reading my own instructions… here’s the cleaned up version:

  • load visor module sudo modprobe visor and check loaded lsmod | grep visor
  • add visor module to /etc/modules
  • click sync button on Palm Pilot and check USB devices are being created:
    • in one terminal, tail -f /var/log/syslog /var/log/messages
    • in a second terminal, check the ownership of links ls -al /dev/ttyUSB* and check you are a member of the appropriate group (usually dialout) grep dialout /etc/group
  • sudo aptitude install jpilot and start JPilot
  • read this step carefully: no need to change the Serial Port setting in Preferences – JPilot will pick up the correct one on the first sync (which is now unexpectedly Other rather than /dev/ttyUSBO or usb:)
  • read this step carefully: from jpilot File > Install User, click sync button on Palm Pilot, count to 5, then click Install User button in JPilot dialog box
  • click sync button on Palm Pilot, count to 5, then click Sync in JPilot
  • if syncing isn’t working (especially on a machine that’s been upgraded from an older version of Ubuntu), try closing JPilot, mv .jpilot .jpilot.bak, restarting JPilot, then doing the Install User steps again

See also:

http://www.linuxquestions.org/questions/showthread.php?t=79965

http://ask.slashdot.org/article.pl?sid=05/05/13/0234225

http://www.linuxmuse.com/articles.php?action=printerf&article=29

http://www.linuxquestions.org/questions/showthread.php?t=177780

http://www.linuxquestions.org/questions/showthread.php?t=221854

http://www.rockhopper.dk/linux/hardware/pda.html#chap5_sect1

http://archives.mandrivalinux.com/expert/2003-02/msg02508.php

http://www.faqs.org/docs/Linux-HOWTO/PalmOS-HOWTO.html#PC-CONNECT-USB

http://pilot-link.org/README.usb

Carsten Clasohm’s Blog : USB Palm and Fedora Core 3

Writing udev rules by Daniel Drake

Linux Magazine – The Linux Device Model

apt sources.list for old versions of Ubuntu

Discontinued versions of Ubuntu are mirrored under http://old-releases.ubuntu.com/ubuntu/, which can be used to setup apt’s sources.list.

For example, for Gutsy (7.10), sources.list would look like:

deb http://old-releases.ubuntu.com/ubuntu/ gutsy main restricted
deb http://old-releases.ubuntu.com/ubuntu/ gutsy-updates main restricted
deb http://old-releases.ubuntu.com/ubuntu/ gutsy universe
deb http://old-releases.ubuntu.com/ubuntu/ gutsy-updates universe
deb http://old-releases.ubuntu.com/ubuntu/ gutsy multiverse
deb http://old-releases.ubuntu.com/ubuntu/ gutsy-updates multiverse
deb http://old-releases.ubuntu.com/ubuntu/ gutsy-security main restricted
deb http://old-releases.ubuntu.com/ubuntu/ gutsy-security universe
deb http://old-releases.ubuntu.com/ubuntu/ gutsy-security multiverse

Thanks to Marty, JohnF, and others on the SLUG list.