Skip to main content.
home | support | download

Back to List Archive

Re: Filter question on spider.pl

From: Bill Moseley <moseley(at)not-real.hank.org>
Date: Wed Dec 10 2003 - 14:53:49 GMT
On Wed, Dec 10, 2003 at 04:46:37AM -0800, redna@euskalerria.org wrote:
> Hi all:
> 
> I've defined a callback function in filter-content, and this function calls
> another function. My problem is that when I return the filtered text, I get
> a value like this SCALAR(0x99137DC). What's wrong? Can anyone help me?
> 
> This is my configuration:
> .
> .
> .
> filter_content => sub {
>                    my $content_ref = $_[3];                    
>                ($kont,$$content_ref)=\&Clean($$content_ref);
                                       ^
                                     here

You are asking for a reference.  You are returning an array of two 
scalars.  The "\" is then causing perl to give you references to those 
two scalars.  You are doing this:

   my ( $x_ref, $y_ref ) = \($x, $y);

You simply want:

   ($kont,$$content_ref) = Clean($$content_ref); 

Although I'd probably do:

   ($kont,$$content_ref) = Clean($content_ref); # pass a reference

and dereference in your subroutine.  It doesn't really matter with Perl, 
though, because Perl always passes by reference[1].  The first is 
passing a reference to the text.  The second one is passing a reference 
to the reference.

So even with the second one you may be able to avoid making a copy of 
the data in memory (if you work with $_[0] for example).  But the other 
advantage is you don't really have to return the new content:

   $kont = Clean( $content_ref );

   sub Clean {
      my $content_ref = shift;
      $$content_ref = "new text"; # replace content
      return 1;
   }



[1]  That is: 

    foo( $bar );  # no copy is done here

    sub foo {
        my ($text) = @_;  # copy done here

@_ really contains references to the original scalar:

    $x = "hello";
    foo( $x );
    print $x; # prints "hello there"

    sub foo { $_[0] .= ' there' }


-- 
Bill Moseley
moseley@hank.org
Received on Wed Dec 10 14:53:56 2003