title: Perl - Using Variables from an Input File in the URL when a Variable has a Space (two words)
body: <p>What am I doing?  The script loads a string from a .txt (locations.txt), and separates it into 6 variables.  Each variable is separated by a comma.  Then I go to a website, whose address depends on these 6 values.</p>
  
  <p>What is the problem?  If there is a space as a character in a variable as part of a string in locations.txt.  When there is a space, it does not get the correct url.</p>
  
  <p>The input file is:</p>
  
  <pre><code> locations.txt = Heinz,Weber,Sierra Leone,1915,M,White
  </code></pre>
  
  <p>Because Sierra Leone has a space, the url is:</p>
  
  <pre><code>  https://familysearch.org/search/collection/results#count=20&amp;query=%2Bgivenname%3AHeinz%20%2Bsurname%3AWeber%20%2Bbirth_place%3A%22Sierra%20Leone%22%20%2Bbirth_year%3A1914-1918~%20%2Bgender%3AM%20%2Brace%3AWhite&amp;collection_id=2000219
  </code></pre>
  
  <p>But that does not get processed correctly in the code below.   </p>
  
  <p>I'm using the packages:</p>
  
  <pre><code> use strict;
   use warnings;
   use WWW::Mechanize::Firefox;
   use HTML::TableExtract;
   use Data::Dumper;
   use LWP::UserAgent;
   use JSON;
   use CGI qw/escape/;
   use HTML::DOM;
  </code></pre>
  
  <p>This is the beginning of the code :</p>
  
  <pre><code> open(my $l, 'locations26.txt') or die "Can't open locations: $!";
   open(my $o, '&gt;', 'out2.txt') or die "Can't open output file: $!";
  
   while (my $line = &lt;$l&gt;) {
        chomp $line;
         my %args;
        @args{qw/givenname surname birth_place birth_year gender race/} = split /,/, $line;
       $args{birth_year} = ($args{birth_year} - 2) . '-' . ($args{birth_year} + 2); 
        my $mech = WWW::Mechanize::Firefox-&gt;new(create =&gt; 1, activate =&gt; 1); 
        $mech-&gt;get("https://familysearch.org/search/collection/results#count=20&amp;query=%2Bgivenname%3A".$args{givenname}."%20%2Bsurname%3A".$args{surname}."%20%2Bbirth_place%3A".$args{birth_place}."%20%2Bbirth_year%3A".$args{birth_year}."~%20%2Bgender%3AM%20%2Brace%3AWhite&amp;collection_id=2000219");  
  
  # REST OF THE SCRIPT HERE. MANY LINES.
  </code></pre>
  
  <p>}</p>
  
  <p>As another example, the following would work:</p>
  
  <pre><code>locations.txt = Benjamin,Schuvlein,Germany,1913,M,White
  </code></pre>