title: How to get query string from URL - perl
body: <p>I'm not aware of how to grab the query string from the URL using Perl. I've tried a couple of ways, example:</p>
  
  <pre><code>my $qs = $ENV{'QUERY_STRING'};
  my @d = split(/&amp;/, $qs);
  
  if ( $d[1] eq 'reports' ) {
      ... do something
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>use CGI;
  my $q = CGI-&gt;new;
  my $page = $q-&gt;param('page');
  </code></pre>
  
  <p>But am not getting the value of the key. Right now I'm manually placing in the query string, i.e. <code>project.local/routes.cgi?page=reports</code> but I get the following from the command line: </p>
  
  <pre><code>Use of uninitialized value $qs in split at ./routes.cgi line 23.
  Use of uninitialized value $d[1] in string eq at ./routes.cgi line 25.
  </code></pre>
  
  <p>I'm not sure why this warning exists as it should just be undef if it doesn't exist?</p>
  
  <p>How should I store and check a query string variable? I'm primarily using the routes.cgi script as a routes controller where all links point to i.e. <code>&lt;a href="project.local/routes.cgi?page=xx"&gt;</code> and then I process a template toolkit file and redirect to that page (good concept?)</p>
  
  <p>Results of diagnostics:</p>
  
  <pre><code>Use of uninitialized value $qs in split at ./routes.cgi line 23 (#1)
      (W uninitialized) An undefined value was used as if it were already
      defined.  It was interpreted as a "" or a 0, but maybe it was a mistake.
      To suppress this warning assign a defined value to your variables.
  </code></pre>
  
  <p><strong>Edit:</strong></p>
  
  <pre><code>use URI qw( );
  use URI::QueryParam qw( );
  
  my $u = URI-&gt;new($base_url);
  my ($p) = $u-&gt;query_param('page');
  
  if ( $p eq 'reports' ) {
      ...do something
  }
  </code></pre>