Sunday, July 24, 2011

getting variables from GET method in perl (cgi)

Suppose we have a page with the following form:

<form action="" method="get">
<input type="text" name="search_for" size="30" />
<input type="submit" value="search" />

So, there's a text input field named "search_for", and a submit button.
The following code gets the variables set in QUERY_STRING (whatever is after, into the $arg associative array. So we can get the value of "search_for" by $arg{"search_for"}.

for (split /\&/, $ENV{QUERY_STRING}) {
($key,$val) = split /\=/;
$val =~ s/\+/ /g;
$val =~ s/%([0-9a-fA-F]{2})/chr(hex($1))/ge;
$arg{$key} = $val;
print "data searched for is =

Friday, July 22, 2011

apache cgi enabled site on specific dir

As an example, using /home/username/some_test_dir/, although it's obviously better not to use home dirs for this kind of things.

These settings allow you to access the files in /home/username/some_test_dir on the web by localhost/some_web_dir (set in alias).
And also enables you to run cgi scripts there.
In the shell:
a2enmod cgi #enable cgi
a2ensite site-filename
/etc/init.d/apache2 reload #in debian

Where filename, is the name of a file containing the following text, placed in /etc/apache2/sites-available/:
<VirtualHost *:80>

 DocumentRoot /home/username/some_test_dir/
 <Directory "/home/username/some_test_dir">
  AllowOverride None
  Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
         allow from all
                AddHandler cgi-script .cgi .pl .sh
        Alias /some_web_dir "/home/username/some_test_dir/"

 ErrorLog ${APACHE_LOG_DIR}/some_web_dir_error.log

 # Possible values include: debug, info, notice, warn, error, crit,
 # alert, emerg.
 LogLevel warn

 CustomLog ${APACHE_LOG_DIR}/some_web_dir_access.log combined

simple cgi shell script

You just print out an html page. The only other thing you have to care about is outputting:
Content-type: text/html

(followed by two newlines characters)
And then, as long as your apache settings are working and the file has executable permission (chmod a+x filename), your browser should display the proper page.

#you need this part for cgi handling
printf "Content-type: text/html\n\n"

# contents
echo "<html>"
echo "<head><title>Dude</title></head>"
echo "<body>"
echo "<h1>some header</h1>"
echo "awesome"
echo "</body>"
echo "</html>"

Thursday, July 21, 2011

connect to mysql with perl

Just as the title says:
Install the package libdbi-perl.

Create the databases/tables/users/permissions as needed.
This is a sample script:

#!/usr/bin/env perl

use DBI;

$host = "localhost";
$database = "your_database";
$tablename = "test";
$user = "this_is_me";
$pw = "this_is_my_password";

#connect to database
$connect = DBI->connect('DBI:mysql:database=' . $database . ';host=' . $host, $user, $pw);

# select stuffs
my $sel = $connect->prepare("select * from $tablename");
while( @row = $sel->fetchrow_array) {
    foreach (@row) {
 print "\t";
    print "\n";

This code prints every row, tab-separated from the selected table.

Tuesday, July 19, 2011

damn Osaka

I finally got her.
The perfect Japanese girl I've longed so much for.
I'm crazy about her, and she for some reason likes me too!
I ask her to marry me, she says OK!

And then...

Then she gets transferred to Osaka><

For god's sake...
Twenty five f*cking years living like crap, hoping for a better tomorrow, enduring so much sh*t, only for the day I'd meet someone like her, and make me feel it was all worth it...
And in the 3th month since we joined the company, she gets transferred to Osaka...
It has to be a bad joke.

Who do I have to beg for to bring her back to the Tokyo branch?><

Whatever, I'll either wait for her to come back here, or just over there myself.
I do like my job (? what I think it will be like), but I know I'll never find someone like her again and I'm not planning to waste that.

Oh, come on, we've had enough for a life time, just bring her back to Tokyo and let us be together (@whoever can change this...).