Search code examples
perllwp

How to concatenate multiple LWP get commands


I am using LWP to get html from three different web pages (defined dynamically) and assign it to a scalar $content. Sometimes one or more of the pages I search will not exist, so get will sometimes return undef. How should I handle this, such that $content will include all successful get commands?

I have the following which works if only one of my get requests returned a defined value:

unless ($content = get $page_one)
{
   unless ($content = get $page_two)
   {
      unless ($content = get $page_three)
      {
         $content = "";
      }
   }
}

but obviously, it doesn't get all the content if more than one page was going to return a defined value.


Solution

  • That's because you nest conditions and make them depend on success of previous gets, when they shouldn't.

    my $result;
    if ($content = get $page_one) { $result .= $content }
    if ($content = get $page_two) { $result .= $content }
    if ($content = get $page_three) { $result .= $content }