Shotglasses Etc.

Get The Last Modification Time Of A File

# ## get the modification time of a file # $mtime = (stat($file))[9];

Get the Size of a File

# ## get the size of a file # $size = (-s $file);

read input interactively

# ## read input line into variable $buf # print "Enter a number : "; chomp($buf = <STDIN>);

Read a File quickly into a variable

# ## slurp a file -- read $size number of bytes into variable $buf # $size = (-s $file); open(IN,"$file"); read(IN,$buf,$size); close(IN);

Creating Directories

# ## To avoid umask calculations, ## save the current umask while setting the umask to 000, ## make the directory, then set the umask back to what it had been # if (! -d $localdir) { $old = umask(000); mkdir($localdir,0775); umask($old); }

Get The Current Directory

# ## get the current directory # use Cwd; $pwd = cwd();

Read A Directory

# ## read a directory $dir ( ignoring . and .. ) # opendir(DIR,"$dir") || die "opendir $dir failed"; @files = grep(!m#^\.\.?$#,readdir(DIR)); closedir(DIR);

Recursively Read a Directory

# ## recursively read a directory, $startdir finding all files ## at the end all directory names are in @dirs, all files are in @files # push(@dirs,$startdir); # add an element to an array foreach $dir (@dirs) # loop through the elements of an array { opendir(DIR,"$dir") || die "opendir $dir failed"; @list = grep(!/^\.\.?$/,readdir(DIR)); closedir(DIR); foreach $item (@list) { $fullname = $dir."/".$item; # add the directory name to the file name if (-d $fullname) { push(@dirs,$fullname); } elsif (-f $fullname) { push(@files,$fullname); } else { print "What?: $fullname\n"; } } }

Breaking out or "short circuiting" a loop

# ## break out of a loop # open(PS,"/bin/ps -ef |"); while ($procline = <PS>) { if ($procline =~ m#httpd#) # find a webserver { ($junk,$user,$pid,$ppid) = split (/\s+/,$procline); # split the line on whitespace last; # jump out of the while loop } } close(PS);

Determining The PID of a Child Process

# ## determining the PID of a child process # $childpid = open(PS,"/bin/ps -ef |");

get the loginID

# ## get the loginID # $user = (getpwuid($<))[0];

get the hostname

# ## get the hostname # use Sys::Hostname; $host = hostname();

concatenate the elements of an array, separated by $delimiter

# ## concatenate the elements of an array, separated by $delimiter # $list = join($delimiter,@groups);

Working with references to a hash

# ## concatenate the elements of an array, separated by $delimiter # my (%mainhash); # pass the reference to mainhash by putting the "\" (slash) in front update(\%mainhash); printit(\%mainhash); sub update { ($hash) = @_; # # #update the "referenced" hash by using $$ # $$hash{"a"} = 1; $$hash{"b"} = 2; $$hash{"c"} = 3; $$hash{"d"} = 4; } sub printit { ($hash) = @_; # ## "keys" cannot work with the reference $$hash so use %{$hash} # foreach $key (keys %{$hash}) { print "$key\n"; } }

print using a "here" document

# ## print using a "here" document -- print out html code without lots of quoting games ## variables between <<End_Text; and End_Text will be expanded ## note that End_Text MUST start in the first column # print <<End_Text; <SCRIPT LANGUAGE=JAVASCRIPT> <!-- document.write('<img src="/cgi-bin/get-img.cgi?'+document.referrer+'"alt="logo" height=103 width=419>') // --> </script> <NOSCRIPT> <img src="/cgi-bin/get-img.cgi?noscript" alt="logo" height=103 width=419> </NOSCRIPT> End_Text

parse script arguments -- either from the command line or as a cgi script

# ## parse script arguments -- either from the command line or as a cgi script ## Note: Arguments are passed to a cgi script in an environment variable ## named QUERY_STRING. These arguments are usually in the form of ## name=value pairs separated by an ampersand (&) ## CGI: test.pl?one=1&two=2 ## Command Line: test.pl one=1 two=2 ## If the QUERY_STRING variable does not exist, then we can assume the script ## is running from the command line (it might be running as a CGI with no ## inputs, but then there will be no command line inputs either) ## If the script is running from the command line, add the command line arguments ## to the QUERY_STRING environment variable. ## This simplifies argument handling -- the same routine can then be used to parse ## the arguments whether they come form the command line or from a web server. ## This routine stores the name-value pairs in a hash named inputs ## ## THIS DOES NOT CHECK FOR BAD OR DANGEROUS CHARACTERS SUCH AS SHELL ESCAPES!!!!! # # command line inputs if ($ENV{QUERY_STRING} eq "") { $ENV{QUERY_STRING} = join("&",@ARGV); $ENV{REQUEST_METHOD} = "GET"; # set a "default" value $inputs{file} = "../data.htm"; } # cgi-script if ($ENV{REQUEST_METHOD} eq 'GET') { # split the query into keywords foreach $input (split("&",$ENV{QUERY_STRING})) { if ($input =~ /(.*)=(.*)/) { ($key,$value) = ($1, $2); if ($debug) { print "[$key] [$value]<br>\n"; } # replace "+" with " " $value =~ s/\+/ /g ; # convert hex characters $value =~ s/%(..)/pack('c',hex($1))/eg; # add keyword/value pair to a list $inputs{$key} = $value; } } }