fivemack: (spiky)
Tom Womack ([personal profile] fivemack) wrote2006-12-20 05:49 pm
Entry tags:

hair-tearing perl question

What I want: a subroutine footle such that, if you call footle(a,b) twice with the same a,b, it does nothing the second time

What I did:
use strict;
sub footle
{
  my ($a,$b,%done) = @_;
  my $concat = $a.$b;
  if ($done{$concat} == 0)
  {
    print "footling $a $b";
    $done{$concat} = 1;
  }
}

my %isdone = ();

footle("bootle","bumtrinket",%isdone);
footle("bootle","bumtrinket",%isdone);

But this doesn't work because parameters are passed by value.

But if I call as footle("bootle","bumtrinket",\%isdone), which passes isdone by reference, it still does the footling twice.

Even if I put $_[2]=%done before the end of the subroutine, it still does the footling twice.

And if I put print join "*",(keys %done); at the start of the subroutine, it says HASH(0x8188110)footling bootle bumtrinket

So how do I really pass the parameter by reference, as if I'd said void footle(int a, int b, set<string>& done) in C++?

[identity profile] hsenag.livejournal.com 2006-12-20 06:24 pm (UTC)(link)
If you're passing it by reference, you need to dereference it when using it. "References" in Perl are more like pointers in C/C++ in the sense that they don't magically dereference themselves when used.

Completely untested:

sub footle
{
my ($a,$b,$done) = @_;
my $concat = $a.$b;
if (!exists $done->{$concat})
{
print "footling $a $b";
$done->{$concat} = 1;
}
}

my %isdone = ();

footle("bootle","bumtrinket",\%isdone);
footle("bootle","bumtrinket",\%isdone);

[identity profile] dd-b.livejournal.com 2006-12-20 06:33 pm (UTC)(link)
One solution, perhaps not so elegant, is to use a global or local variable (but not a "my" I think) for %done. (C static, I forget how you do that in Perl offhand).

But there's something funny going on, because it *is* possible to pass a hash reference that you update, I've done it frequently I thought.

Maybe it's a syntax problem referring to the reference; I can't find an example where a hash reference is passed in, but when I dig one out of a structure in my working applications I see code like "if (keys(%{$picdb->{DBINFO}}))", that is, %{}.

Possibly using the right function template would help? (if only to make the calling sequence cleaner).

closure

[identity profile] http://the.earth.li/~alex/halley/ (from livejournal.com) 2006-12-20 08:37 pm (UTC)(link)

Or you could use a closure and do away with the isdone:

{
    my %c;
    sub foo {
        if ($c{$_[0]}{$_[1]}++) {
            print "$_[0] $_[1] already!\n";
            return;
        }
        print "$_[0] $_[1]\n";
    }
    sub clear { %c = () } # to reset the cache
}

foo("badger", "ferret");
foo("badger", "ferret");

which also avoids the minor bug you had if suffixes of A and prefixes of B could get mixed up.