Perl Perl Programming Quick Tips

Perl – Getting Package Names

Had an interesting question from a student i’ve been helping with a project. They were after the package name, for various reasons, and so I prepared a small example with 3 ways of doing 2 things. Heres the example:

use strict;
use warnings;

use 5.020;

  package Role::PrintClass;

  use Moo::Role;

  requires 'class_name';

  sub print_class {
    my $self = shift;
    say "Class: " . $self->class_name;
    my @split_class = split ( '::', $self->class_name );
    say "Parts: " . $_ for @split_class;

  sub print_ref {
    my $self = shift;

    say ref($self);

  package My::Class::A;

  use Moo;

  sub class_name { return __PACKAGE__ };

  with 'Role::PrintClass';

  package My::Class::B;

  use Moo;

  sub class_name { return __PACKAGE__ };

  with 'Role::PrintClass';

my $a = My::Class::A->new;

my $b = My::Class::B->new;

say "Package A";
say "$a";
say ref($a);

say "Package B";

say "$b";
say ref($b);

The two main ways here, are with the __PACKAGE__ token, or using the ref function.

This also has an example of using Moo::Roles, and some of the basic things you can do with them.

Hopefully this is useful for people!

Quick Tips XFCE

Quick Tip – Mouse Scrolling in XFCE

Hey guys, another quick tip! So was just getting annoyed about my mouse scrolling being odd – I had natural scrolling set in my main XFCE settings, however it did not behave the same for everything. In a random google session, I found out that theres a lower level setting for it. So, heres the instructions I ran – you’l have to modify them for your specific needs though! as your mouse may not have the same name. You will also need to disable natural scrolling in XFCE to make it work properly!

sudo apt-get install xinput
xinput --list
xinput --list-props "PixArt USB Optical Mouse"
xinput set-prop "PixArt USB Optical Mouse" "Evdev Scrolling Distance" -1 1 1

So theres another random quick tip!

Postgres Quick Tips

Quick Tip – Postgres Data Dumping

Hey guys, just a quick tip on something I used recently – how to dump data to CSV from postgres.

So, if you want to dump something on the server itself, you can run the following commands:

Copy (Select * From foo) To '/tmp/test.csv' With CSV DELIMITER ',';

Which will dump out the data into a CSV at the specified path. If you are logging in through a remote connection on a client, then you can do the same using a similar command:

\copy (Select * From foo) To '/tmp/test.csv' With CSV

This will then dump out locally to your client.

On both of these commands, the file will be written with the permissions of the item in question – with the first one, it will be written as the user which the server is running under (usually postgres) – in the second one it will be as whoever you ran psql as.

This was pulled from a stackoverflow answer, so go upvote them, its a very good answer and is very well written – much better than I have managed here.