Topics:
DVR
nvrec
Mplayer
Links
Misc
Commands
Humor
New user
uploaded files
|
(links)-> (Parent)->Perl code samples and links |
submited by Russell Fri 27 Jan 06 Edited Sat 05 May 12 |
Random File I/O with Perl
binary direct read from file
read FILEHANDLE, $VAR, LENGTH [ , OFFSET ]
Reads LENGTH binary bytes from the file into the variable at OFFSET. Returns number of bytes actually read.
Perl Array Functions This is part of a larger Online Lecture on perl Contains lots of good and simple examples.
Perl fork without waiting on child process:
waitpid PID,FLAGS
use POSIX ":sys_wait_h";
do {
$x=fork();
if ($x==0)
{ ## this is the child
## do something that could take a long time or never end
exit(0); ## exits the child
}
$PIDS{$x}=$x;
foreach (keys %PIDS )
{
print "waiting for $PIDS{$_} ";
$retval=waitpid($_,&WNOHANG);
if ($retval > -1) {delete $PIDS{$retval};print "($retval exited)"};
} ## end foreach
print "\n";
sleep 30;
}until (1==2);
This code sample will start the child every 30 seconds, no matter what. (even if the child takes a long time to complete) The script can be left running continiously. if it didn't call wait() , or in this case waitpid(), you will end up fork() bombing your system. this solves that problem and solves the problem where some instances of the child take a long time to complete. ( slow/bad network conections etc.. see my issues with optium oinline )
BTW, &WNOHANG has a value of 1. So if the 'use' command doesn't work for you, just do "waitpid($PID,1);" that's bad form, and perhaps it's not 1 on all systems, but perl code isn't really about good form, it's about getting the job done with the least time spent coding possible.
perl get current username
($my_user_name, $pass, $uid, $gid, $quota, $comment, $gcos, $dir, $shell, $expire) = getpwuid($<);
gets current username using the numeric user-id and the password file contents. ( on modern systems, that use a shadow password file, $pass will be "x" which is only a placeholder )
Remove HTML tags leaving only text
$htmlCode =~ s|<.+?>||g;
Check out the discussion on stack overflow about how that is probably not a good idea, and you should use a more powerful tool.
|
Add comment or question...:
|