ichimunki has asked for the wisdom of the Perl Monks concerning the following question:

I've got the following code which strikes me as sloppy, plus I don't like the idea of allocating a scalar (even in a limited scope) just to handle this:
my $html = $self->{'HTML'}; my $parse_it = HTML::TokeParser->new( \$html );
It's got to be passed as a reference or the constructor goes looking for a file (as per the documentation).

I've tried putting $self->{'HTML'} into every different kind of bracket strategy I could, to no avail. Please help!

Replies are listed 'Best First'.
Re: Passing Indirect as Reference
by chipmunk (Parson) on Jan 10, 2001 at 07:13 UTC
    How about: my $parse_it = HTML::TokeParser->new( \( $self->{'HTML'} ) ); \(LIST) maps \ onto each element of LIST, and returns a list of references.
      But couldn't you do just
      my $parse_it = HTML::TokeParser->new( \ ${$self->{'HTML'} } );
      As far as my limited understanding of references goes, you force the $self part to be interpreted as a scalar (string), and pass a reference to that string.

      If I'm wrong here (correct me if not ;-), you also could do

      my $parse_it = HTML::TokeParser->new( \ "$self->{HTML}" );
      as the quotes are not necessary, and the \ should give a reference to the resulting string.

      Hope this helps,

      Jeroen
      I was dreaming of guitarnotes that would irritate an executive kind of guy (FZ)

             Quote:     my $parse_it = HTML::TokeParser->new( \ ${$self->{'HTML'} } ); That code dereferences $self->{'HTML'} as a scalar reference; if $self->{'HTML'} were already a scalar reference, this whole exercise would be unnecessary. So that won't work.

             Quote:     my $parse_it = HTML::TokeParser->new( \ "$self->{HTML}" ); That works, passing a reference to a copy of $self->{HTML}, instead of to $self->{HTML} itself. That has the (probably not significant) disadvantage of allocating memory for a duplicate of $self->{HTML}, but the advantage of not having to worry about HTML::TokeParser changing the value of $self->{HTML} via the reference. That's a good suggestion.

        thanks to both of you. FYI, looking at jeroenes' last example I tried
        my $parse_it = HTML::TokeParser->new( \ $self->{HTML} );
        which worked just fine without the double quotes around the construct.

        (I use the quoted hash keys by habit and early example, I suppose I can drop that habit soon. It doesn't affect hash keys with spaces in them?)