# perl/Tk used to find your State Representatives



## regexorcist (Feb 2, 2010)

I wrote this little script as a perl (gui) experiment.
You will need perl and the Tk module for perl. 

Enter a State abbrev. and it retrieves all the representatives for that state via a web API.
Now if I could only make those representatives see things my way 


```
#!/usr/bin/perl

require Tk;
use Tk;

#simple perl/tk script
#by regexorcist

#NOTE: Only tested on Frugalware and Arch Linux so far
#To run it, you must have perl and Tk installed
#copy this code into a file with a .pl extension,
#then make that file executable and run it
#no special permissions needed.

#The program displays State Representatives contact info for state abbr. you supply
#Configuration files differ between Linux distros, so a few
#may not be available.

#Create Tk objects
$var_window = new MainWindow;
$var_frame1 = $var_window -> Frame() -> pack(-fill => 'x');
$var_frame2 = $var_window -> Frame() -> pack();
$var_label = $var_frame1 -> Label() -> pack(-side => 'left');
$var_entry = $var_frame1 -> Entry() -> pack(-side => 'left');
$var_button = $var_frame1 -> Button() -> pack(-side => 'right');
$var_text = $var_frame2 -> Text() -> pack(-side => 'left');
$var_scroll = $var_frame2 -> Scrollbar() -> pack(-side => 'right');

#Configure Tk objects
$var_window -> configure(-title => '(regexorcist) Contact Your State Rep(s)', -background => 'blue',);
$var_window -> geometry('+200+200');
$var_frame1-> configure(-relief => 'groove', -borderwidth => 3, -background => 'white');
$var_frame2 -> configure(-relief => 'groove', -borderwidth => 3, -background => 'white');
$var_label -> configure(-text => 'Enter 2-letter State Abbr. (AL, CA, NJ, etc...)', -background => 'white', -foreground => 'black');
$var_entry -> configure(-width => 3, -background => 'white');
$var_button -> configure(-text => 'Enter', -command => \&pass_variable);
$var_text -> configure(-width => 80, -height => 25, -wrap => word, -yscrollcommand => [set => $var_scroll]);
$var_scroll -> configure(-orient => 'v', -command => ['yview' => $var_text]);

MainLoop();

#Routine called when "Enter" button is pressed
sub pass_variable()
{
   $var_state = $var_entry -> get();
   $info_link = "http://whoismyrepresentative.com/getall_reps_bystate.php?state=$var_state";
   system($var_response = `wget -q $info_link -O -`);
  
   $var_position = 1;
   while($var_position > 0)
   {
#Seperate Rep Info into Substrings
      $var_begin_sub = index($var_response, '<rep');
      $var_end_sub = index($var_response, '<', ($var_begin_sub + 4));
      $var_substring = substr($var_response, $var_begin_sub, ($var_end_sub - $var_begin_sub));

#Extract and Write Rep Name to TextArea
      $var_count1 = index($var_substring, 'name=');
      $var_count2 = index($var_substring, 'party=');
      $var_name = substr($var_substring,($var_count1 + 6),(($var_count2 - 2) - ($var_count1 + 6)));
      $var_text -> insert('end', "Representive: $var_name\n");

#Extract and Write Government Party to TextArea
      $var_count1 = index($var_substring, 'party=');
      $var_count2 = index($var_substring, 'state=');
      $var_party = substr($var_substring,($var_count1 + 7),(($var_count2 -2) - ($var_count1 + 7)));
      $var_text -> insert('end', "Party: $var_party\n");

#Extract and Write State Abbr. to TextArea
      $var_count1 = index($var_substring, 'state=');
      $var_count2 = index($var_substring, 'district=');
      $var_state = substr($var_substring,($var_count1 + 7),(($var_count2 -2) - ($var_count1 + 7)));
      $var_text -> insert('end', "State: $var_state\n");

#Extract and Write District Number to TextArea
      $var_count1 = index($var_substring, 'district=');
      $var_count2 = index($var_substring, 'phone=');
      $var_district = substr($var_substring,($var_count1 + 10),(($var_count2 -2) - ($var_count1 + 10)));
      $var_text -> insert('end', "District: $var_district\n");

#Extract and Write Phone to TextArea
      $var_count1 = index($var_substring, 'phone=');
      $var_count2 = index($var_substring, 'office=');
      $var_phone = substr($var_substring,($var_count1 + 7),(($var_count2 -2) - ($var_count1 + 7)));
      $var_text -> insert('end', "Phone: $var_phone\n");

#Extract and Write Rep Name to TextArea
      $var_count1 = index($var_substring, 'office=');
      $var_count2 = index($var_substring, 'link=');
      $var_office = substr($var_substring,($var_count1 + 8),(($var_count2 -2) - ($var_count1 + 8)));
      $var_text -> insert('end', "Office: $var_office\n");

#Extract and Write URL Link to TextArea
      $var_count1 = index($var_substring, 'link=');
      $var_count2 = index($var_substring, '/>');
      $var_link = substr($var_substring,($var_count1 + 6),(($var_count2 -2) - ($var_count1 + 6)));
      $var_text -> insert('end', "URL Link: $var_link\n\n");

#Crop Initial Result ob Substring one Rep at a time
      $var_response = substr($var_response, ($var_end_sub + 1));
      $var_position = index($var_response, '<rep');
   }
}
```

enjoy...


----------

