title: How do I remove white space in a Perl string?
body: <p>If I declared a variable <code>$myString</code> with the value <code>'3 '</code> (notice the white space).
  Is there any function to remove the white space for the return value.
  A little like <code>SomeFun($myString)</code>  then return <code>'3'</code> (without white space).</p>
  
  <pre><code>#!C:\Perl\bin\perl.exe
  use strict;
  use warnings;
  use Data::Dumper; 
  
  my $fh = \*DATA; 
  
  print Dumper parse_constant_spec( $fh ); 
  
  
  # Parse a constant spec file. 
  # Pass in a handle to process. 
  # As long as it acts like a file handle, it will work. 
  sub parse_constant_spec { 
      my $fh = shift; 
  
      my %spec; 
  
      # Until file is done: 
          # Read in a whole block 
      while( my $block = read_block($fh) ) { 
  
          # Parse the and return key/value pairs for a hash. 
          my %constant = parse_block( $block ); 
  
          # Store a ref to the hash in a big hash of all blocks, keyed by constant_name. 
          $spec{ $constant{const_name} } = \%constant; 
  
      } 
  
      # Return ref to big hash with all block data 
      return \%spec; 
  } 
  
  # Read a constant definition block from a file handle. 
  # void return when there is no data left in the file. 
  # Otherwise return an array ref containing lines to in the block.  
  sub read_block { 
      my $fh = shift; 
  
      my @lines; 
      my $block_started = 0; 
  
      while( my $line = &lt;$fh&gt; ) { 
  
          $block_started++ if $line =~ /^constant/; 
  
          if( $block_started ) { 
  
              last if $line =~ /^\s*$/; 
  
              push @lines, $line; 
          } 
      } 
  
      return \@lines if @lines; 
  
      return; 
  } 
  
  
  sub parse_block { 
      my $block = shift; 
      my ($start_line, @attribs) = @$block; 
  
      my %constant; 
  
      # Break down first line: 
      # First separate assignment from option list. 
      my ($start_head, $start_tail) = split /=/, $start_line; 
  
      # work on option list 
      my @options = split /\s+/, $start_head; 
  
      # Recover constant_name from options: 
      $constant{const_name} = pop @options; 
      $constant{options} = \@options; 
  
      # Now we parse the value/type specifier 
      @constant{'type', 'value' } = parse_type_value_specifier( $start_tail ); 
  
      # Parse attribute lines. 
      # since we've already got multiple per line, get them all at once. 
      chomp @attribs; 
      my $attribs = join ' ', @attribs; 
  
      #  we have one long line of mixed key = "value" or key = &lt;TYPE VALUE&gt;  
  
      @attribs = $attribs =~ /\s*(\w+\s+=\s+\w+\s+|\w+\s+=\s+".*?"|\w+\s+=\s+&lt;.*?&gt;)\s*/g; 
  
      for my $attrib ( @attribs ) { 
          warn "$attrib\n"; 
          my ($name, $value) = split /\s*=\s*/, $attrib; 
  
          if( $value =~ /^"/ ) {  
              $value =~ s/^"|"\s*$//g; 
          } 
          elsif( $value =~ /^&lt;/ ) { 
             $value = [ parse_type_value_specifier( $start_tail ) ]; 
          } 
          else { 
              warn "Bad line"; 
          } 
  
          $constant{ $name } = $value; 
      } 
  
      return %constant; 
  } 
  
  sub parse_type_value_specifier { 
      my $tvs = shift; 
  
      my ($type, $value) = $tvs =~ /&lt;(\w+)\s+(.*?)&gt;/; 
  
      return $type, $value; 
  } 
  
  __DATA__ 
  constant fixup GemEstabCommDelay = &lt;U2 20&gt; 
      vid = 6 
      name = "ESTABLISHCOMMUNICATIONSTIMEOUT" 
      units = "s" 
      min = &lt;U2 0&gt; 
      max = &lt;U2 1800&gt; 
      default = &lt;U2 20&gt; 
  
  
  constant fixup private GemConstantFileName = &lt;A "C:\\TMP\\CONST.LOG"&gt; 
      vid = 4 
      name = ""  units = "" 
  
  
  constant fixup private GemAlarmFileName = &lt;A "C:\\TMP\\ALARM.LOG"&gt; 
      vid = 0 
      name = "" 
      units = ""   
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>D:\learning\perl&gt;hello1.pl
  vid = 6
  Bad line at D:\learning\perl\hello1.pl line 102, &lt;DATA&gt; line 8.
  name = "ESTABLISHCOMMUNICATIONSTIMEOUT"
  units = "s"
  min = &lt;U2 0&gt;
  max = &lt;U2 1800&gt;
  default = &lt;U2 20&gt;
  vid = 4
  Bad line at D:\learning\perl\hello1.pl line 102, &lt;DATA&gt; line 13.
  name = ""
  units = ""
  vid = 0
  Bad line at D:\learning\perl\hello1.pl line 102, &lt;DATA&gt; line 18.
  name = ""
  units = ""
  $VAR1 = {
            'GemAlarmFileName' =&gt; {
                                    'vid' =&gt; '0      ',
                                    'options' =&gt; [
                                                   'constant',
                                                   'fixup',
                                                   'private'
                                                 ],
                                    'value' =&gt; '"C:\\\\TMP\\\\ALARM.LOG"',
                                    'name' =&gt; '',
                                    'type' =&gt; 'A',
                                    'const_name' =&gt; 'GemAlarmFileName',
                                    'units' =&gt; ''
                                  },
            'GemEstabCommDelay' =&gt; {
                                     'vid' =&gt; '6      ',
                                     'options' =&gt; [
                                                    'constant',
                                                    'fixup'
                                                  ],
                                     'value' =&gt; '20',
                                     'min' =&gt; [
                                                'U2',
                                                '20'
                                              ],
                                     'name' =&gt; 'ESTABLISHCOMMUNICATIONSTIMEOUT',
                                     'max' =&gt; [
                                                'U2',
                                                '20'
                                              ],
                                     'default' =&gt; [
                                                    'U2',
                                                    '20'
                                                  ],
                                     'type' =&gt; 'U2',
                                     'units' =&gt; 's',
                                     'const_name' =&gt; 'GemEstabCommDelay'
                                   },
            'GemConstantFileName' =&gt; {
                                       'vid' =&gt; '4      ',
                                       'options' =&gt; [
                                                      'constant',
                                                      'fixup',
                                                      'private'
                                                    ],
                                       'value' =&gt; '"C:\\\\TMP\\\\CONST.LOG"',
                                       'name' =&gt; '',
                                       'type' =&gt; 'A',
                                       'const_name' =&gt; 'GemConstantFileName',
                                       'units' =&gt; ''
                                     }
          };
  
  D:\learning\perl&gt;
  </code></pre>
  
  <p>You could notice that <code>'vid' =&gt; '0      ',</code> (notice the white space) </p>
  
  <p>The code above from <a href="https://stackoverflow.com/questions/1968702/how-can-i-detect-a-blank-line-in-perl">the answer</a>. I am studying it. :-)</p>
  
  <p>Thank you. </p>