Modify a string passed by reference in perl

I don’t know why I feel the need to write this down, but I was having a slow brain day and needed to actually sit and think about using string references within a perl subroutine!

my $string = "STRINGY";

print "Before: $string\n";
modify_string(\$string);
print "After: $string\n";

sub modify_string
{
my ($string_ref) = @_;
my $length = length($$string_ref);
$$string_ref .= "-$length";
}

This tries to demonstrate two things. Firstly using the string reference (i.e. into the length command and secondly modifying the string itself (appending a dash and the length of the string to it). Don’t worry about the “-$length” bit — it’s not trying to do anything cleverer than append to the string… :)

Technorati Tags: ,

Attempt to bless into a reference (perl)

It’s saying this because for some reason the second param to bless in your ->new() constuctor is not the name of the Object you want your reference to be, but instead it is itself a reference!

It could be that you’re passing a string reference to bless rather than a string itself in which case you’ll need to double $$ it. But in my case, like a donkey, I’d written:

my $fish = new Fish->new();

Fantastic.

I’ve just done it again oddly. No more than an hour later.
This time. I’ve tried to create a new object from it’s own handle. i.e.

$self->new();

rather than

TheClassName->new();

So bless is getting $self rather than “TheClassName” as it’s second parameter.

How to get the Perl debugger to kick in, codewise.

How often do you need to use the perl debugger? Often I presume, either that or you’re a superstar ninja coder or you have no hair left. Anyways, it’s achingly boring trying to step through code and typeing ‘n’ to step into a function when you meant to press ’s’. Well, you could set a breakpoint, but in a loop you’re still going to annoy yourself. A lot.

What you can do is use this:

$DB::single = 1;

CHRIS YOU FOOL! You may cry. That’s no different to setting a debug point anyway. Well, it is if you use your head and Perl’s logic to set it like thus:

while (something_is_happening())
{
$DB::single = 1 if (noises_from_woodshed());

...All your usual gubbins....
}

See, I’m not as silly as I look!

Get the table names used in an SQL query

I had cause to analyse a query for the table names used in it before sending it off to the database. I don’t actually do it this way any more. But this is a solution using the SQL::Statement module from Cpan in perl

#
# First fetch table names from query
#
my $sql = “SELECT blah FROM blah, blah, blah”;

# Using AnyData rather than ANSI because ANSI catches reserved
# words even if they are quoted or fully qualified (bug in SQL::Parser)
my $parser = SQL::Parser->new(‘AnyData’);
$parser->{RaiseError} = 0;
$parser->{PrintError} = 1;

# Parse the table names from the query and clean them up.
my $statement = SQL::Statement->new($sql,$parser);
foreach my $table ($statement->tables())
{
    # remove quotes (") from table name and convert to lower case
    my ($table_name_clean) = lc($table->name());
    $table_name_clean =~ s/"//g;

    push(@table_names, $table_name_clean);
}