Welcome

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

What next?
1. Bookmark us with del.icio.us or Digg Us!
2. Subscribe to this site's RSS feed
3. Browse the site.
4. Post your own code snippets to the site!

install a spell-checking program

// Install the spell-checking program of your choice. Just update the path

;spell-checking
(setq-default ispell-program-name "/Program Files/Aspell/bin/aspell.exe")

starter Perl module

// Put this in the file MyPackage.pm, and call it with Use::MyPackage

package MyPackage;

#module code goes here...

1;

submit a POST query with parameters

// from The Perl Black Book, Holzner, p. 1247

#! /usr/bin/perl -w
use strict;
use HTTP::Request::Common;
use LWP::UserAgent;
#The Perl Black Book, Holzner, p. 1247


=item how to call submit_query():
#Method 1.
my %example = (test1 => 'noah', test2 => 'sussman');
submit_query("http://suburbanangst.com/reg.php", %example);

#Method 2.
submit_query("http://suburbanangst.com/reg.php?test1=foo&test2=bar;");
=cut

sub submit_query {
    my ($file, %query) = @_;
    my $user_agent = LWP::UserAgent->new;
    $user_agent->agent("MSIE/5.5 " . $user_agent->agent);
    my $request = POST
    #'http://suburbanangst.com/reg.php',
    $file,
    [%query];
    my $response = $user_agent->request($request);
    print $response->as_string;
}

click on an element across browsers

// If you have assigned an event handler to a container, you might want to remotely trigger it as if a child of the container had been clicked. That is, you might want to manually set the target of the 'event' object that is passed to the event handler on the container.

//assuming that 'menu' has an appropriate onclick handler:
    function simulateClick( itemToClick, menu) {
      if ( ! itemToClick.click ) {
	//== Non-IE:
	menu.onclick({target: itemToClick});
      } else {
	//== IE:
	itemToClick.click()
      }
    }


This helps to keep the number of event handlers down.

Yahoo General Header (XHTML)


<div id="yahooHeader">
	<a href="#" class="yahooBranch">Yahoo!</a>
	<ul>
		<li><a href="#" title="Home">Home</a> - </li>
		<li><a href="#" title="Help">Help</a></li>
	</ul>
</div><!-- close yahooHeader -->

heredoc xhtml 1.0 strict page template


<?php

$content = <<<HTML
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi in sapien interdum purus volutpat placerat. Sed ornare nisi eu magna. Cras vulputate. Phasellus vel lorem vel magna placerat iaculis. Aenean tortor leo, ultricies ut, dictum non, blandit nec, odio. Morbi vel odio. Etiam venenatis turpis suscipit nibh. Suspendisse tincidunt, metus vel consequat gravida, neque enim egestas est, et interdum diam tellus vel lorem. In hac habitasse platea dictumst. Fusce vitae velit. Suspendisse a libero et risus imperdiet tincidunt.
HTML;

$html = <<<HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>title</title>
	<link rel="stylesheet" href="./css/screen.css" type="text/css" media="screen" />

</head>
<body>
<div id="wrap">
	$content
</div>
</body>
</html>
HTML;

echo $html;

?>

Search bar for sites based on XSLT as target for results

// A rough search form based on usage with googleboxes and xslt templates for results.

<div id="searchbar">
 <form action="http://domain.com/search" method="get"> 
  <input type="text" name="q" value="Search Our Site" size="20" maxlength="120" id="searchInput" onfocus="if(this.value == 'Search Our Site'){this.value='';}">
  <input type="hidden" name="site" value="externalSearch">                    
  <input type="hidden" name="client" value="externalSearch">                  
  <input type="hidden" name="restrict" value="wwwexternal">  
  <input type="hidden" name="proxystylesheet" value="http://domain.com/searchStyle.xslt">                                             
  <input type="hidden" name="output" value="xml_no_dtd">     

  <input type="submit" name="btnG" value="Search" id="searchSubmit">
 </form>
</div>

Scrape Google from the command line

// This code is POC only -- actually using it would violate Google's TOS, which forbids scraping. It is published here for educational value only.

Hypothetically, the following command should return a list of the top 500 or so hits in Google for onemorebug.com.

The results will be prepended with digits, followed by a dot and some whitespace (Lynx adds these).

You must have Lynx and Wget installed on your system for this to work.

Keep in mind that *nix shells don't like it when you double-quote strings, see the comments.


perl -e "$i=0;while($i<1000){sleep 1; open(WGET,qq/|xargs lynx -dump/);printf WGET qq{http://www.google.com/search?q=site:onemorebug.com&hl=en&start=$i&sa=N},$i+=10}" | grep "\/\/[^/]*onemorebug.com\/"

Download file

// description of your code here

<?php

$filename = $_GET['filename'];

// Modify this line to indicate the location of the files you want people to be able to download
// This path must not contain a trailing slash.  ie.  /temp/files/download
$download_path = "ficheros/";
	
// Make sure we can't download files above the current directory location.
if(eregi("\.\.", $filename)) die("I'm sorry, you may not download that file.");
$file = str_replace("..", "", $filename);
	
// Make sure we can't download .ht control files.
if(eregi("\.ht.+", $filename)) die("I'm sorry, you may not download that file.");
	
// Combine the download path and the filename to create the full path to the file.
$file = "$download_path$file";
	
// Test to ensure that the file exists.
if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");
	
// Extract the type of file which will be sent to the browser as a header
$type = filetype($file);

// Get a date and timestamp
$today = date("F j, Y, g:i a");
$time = time();

// Send file headers
header("Content-type: $type");
header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary");
header('Pragma: no-cache');
header('Expires: 0');
// Send the file contents.
set_time_limit(0);
readfile($file);

?>

CSS3 Skewed Shadows


<!DOCTYPE HTML>
<html lang="en-US">
<head>
	<meta charset="UTF-8">
	<title>Tuts</title>

	<style>
	body {
		width: 500px;
		margin: 50px auto;
	}
	
	.box {
		position: relative;
		-webkit-box-shadow: 1px 2px 4px rgba(0,0,0,.5);
		-moz-box-shadow: 1px 2px 4px rgba(0,0,0,.5);
		box-shadow: 1px 2px 4px rgba(0,0,0,.5);
		
		/* Kokakify */
		padding: 10px;
		background: white;
	}
	
	.box img {
		max-width: 100%;
		border: 1px inset #8a4419;
	}
	
	.box:after {
		content: '';
		-webkit-box-shadow:  100px 0 10px 20px rgba(0,0,0,.2);
		-moz-box-shadow:  100px 0 10px 20px rgba(0,0,0,.2);
		box-shadow:  100px 0 10px 20px rgba(0,0,0,.2);
		position: absolute;
		width: 50%;
		height: 40px;
		bottom: 20px;
		right: 90px;
		z-index: -1;
		-webkit-transform: skew(-40deg);
		-moz-transform: skew(-40deg);
		transform: skew(-40deg);		
		
	}
	</style>
	
</head>

<body>

<div class="box">
	<img src="tuts.jpg" alt="Tuts" />
</div>

</body>
</html>