Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Loop behavior with HTML::TokeParser::Simple

by jsprat (Curate)
on Dec 20, 2003 at 02:20 UTC ( [id://315972]=note: print w/replies, xml ) Need Help??


in reply to Loop behavior with HTML::TokeParser::Simple

Probably the regex is failing because of the metacharacters in $src. Try s/\Q$src\E/$newsrc/ (untested) to make the regex work.

However, the better solution is to chuck the regex. It replaces $src in its entirety, so a simple $src = $newsrc (or replace both lines with $token->set_attr('src', $newsrc);) would be both easier to maintain and more effective.

HTH

Replies are listed 'Best First'.
Re: Re: Loop behavior with HTML::TokeParser::Simple
by WhiteBird (Hermit) on Dec 20, 2003 at 02:52 UTC
    Thank you for the prompt reply. Using the $src = $newsrc works nicely to make the replacement, and the combined $token->set_attr('src', $newsrc); doesn't complain so I'm assuming it works as expected.

    I get an odd bit in the next step of my code, though.

    print FILE $token->as_is; close FILE;
    I'm trying to save it all back to a file and all I get is a 1K file that merely has an end bold HTML tag in it. That's an improvement over before where I was getting an empty file, but I'm still missing something important. Am I getting something wrong in my set_attr call, or am I mis-understanding and misusing the token->as_is part? I'm looking at the documentation for the module, but I'm clueless. Any suggestions are appreciated.
      I haven't seen your entire code, but I imagine that your code looks somewhat like below -
      while ( $token=$p->get_token() ) { .... } ... print FILE $token->as_is; close FILE;
      If you want to print all the tokens, you should put the print statement inside your loop, otherwise the $token variable is replaced with the next token every time you call the $p->get_token() function.

        Roger, you are correct about the structure of the code, and that the print statement has to be in the proper place in the loop. I am now saving 99% of what I need into a new file.

        I have quite a few loops as I sweep through the HTML to get my file name, and now everything I want gets printed into my file except the bit of text that I pull out to use as a file name for the image. The relevant code is this:

        { push @next, $p->get_token(); if ($next[1][0] eq 'T') { my $title= $next[1][1]; #strip white space from title and get up to four words if ($title =~ /(\w+)\s*(\w+)\s*(\w+)\s*(\w+)/) { my $filetitle = $1 . $2 . $3 . $4; my $newsrc = $filetitle . "\.jpg"; while ( $token=$p->get_token() ) { if ($token->is_start_tag('img') ) { my $src = $token->return_attr->{src}; $token->set_attr('src', $newsrc); } print FILE $token->as_is; } }}}

        In keeping with wishes, and genies, and not trying people's patience, I ask my third and last question on this code...Is there a way to put a token back into the stream so that I can keep the file's title text?

        UPDATE:Taking a step or two back from this I realized a simple answer to my own question. I just added a print FILE $title; statement in after the regex and it did what I needed. I was so wrapped up in parsing the HTML that I had lost the thought that I could use another approach. Thanks, Monks, as always for your assistance.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://315972]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-03-29 13:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found