Thursday, September 16, 2010

Perl System Routines: Functions for process

exec: abandon this program to run another

exit: exits from program


fork: creates new process just like this

getppid: get parent process ID

getpriority: returns current priority of the process

kill: sends a signal to a process or group of process

setpriority:

sleep:

syscall:

system:

times:

wait:

waitpid:

warn:

unlink in Perl: to delete the file

Unlink : this command permanently deletes a list of file from system.
Return: returns number of files successfully deleted. on failure sets value of "$!"  and returns false. It will not tell you the name of file which it couldn't delete.
Do not try deleting directory instead use rmdir command.

unlink("file1.txt");
unlink "a.txt", "b.txt";
unlink @list;
unlink glob "*.bak";

chmod in perl: setting file permissions

Changes the permission of the specified list of files. First argument should be numerical octal value. Returns number of files successfully changed.

usage is as follow:

$result = chmod 0777, "file1.txt","file2.txt";
chmod 0755, @filelist;
$mode  = 0655; chmod $mode, @filelist;

$mode = "0655"; chmod oct($mode), @filelist;
$mode = "0655"; chmod $mode, @filelist; # Not a good usage Should avoid, instead use above method

How to decide mode octal value:
mode is of 4 octal digits e.g. 0754, equivalent to (userID)(user)(group)(Other)
0755: u: full, GO: read and execute
0777: all : full permission
0555: all : read and execute

 symbolic representation of file permissions:

Representation
Class
Description
u
User
Owner
g
Group
Members of file group
o
Others
Neither owner nor group
a
All
Everyone

Octal notation:

Octal
System
Description
0
---
No permission
1
--x
execute
2
-w-
write
3
-wx
Write and execute
4
r--
Read
5
r-x
Read and execute
6
rw-
Read and write
7
rwx
Read, write and execute

link in perl

create a new file linked to oldfile. function creates a hard link if you want symbolic link then use symlink function.
returns 1 on success and 0 on failure. NewFile should not be present else will throw an error. This feature is similar to copying and renaming the file.

link OLDFILE, NEWFILE;
link "file1.txt", "file3.txt";

Perl File operations: Functions for file handling and interacting with system

chdir(EXPR): changes the working directory to EXPR, if EXPR is omitted then changes to home directory. refer to chdir in perl for more details.

chmod: changes the permission on a list of files.

glob:expand filenames using wild card

link: create a hard link in the system

lstat: stat a symbolic link

mkdir: makes a directory, similar to md dos command

opendir: opens a directory in code and gives directory handle.

readlink: determines where a symbolic link is pointing

rename: change a filename

unlink: deletes a file permanently

setting mode permission from command line

type following command in command prompt.
chmod {mode} filename, file2, file3

In C language its used as:
int chmod(const char *File_path, mode_t mode_value);

How to decide mode octal value:
mode is of 4 octal digits e.g. 0754, equivalent to (userID)(user)(group)(Other)

0755: u: full, GO: read and execute

0777: all : full permission

0555: all : read and execute


 symbolic representation of file permissions:

Representation
Class
Description
u
User
Owner
g
Group
Members of file group
o
Others
Neither owner nor group
a
All
Everyone

Octal notation:

Octal
System
Description
0
---
No permission
1
--x
execute
2
-w-
write
3
-wx
Write and execute
4
r--
Read
5
r-x
Read and execute
6
rw-
Read and write
7
rwx
Read, write and execute

These octal values are decided based on binary position of R W X.
R W X
0  0  0
0  0  1 : execute
0  1  0 : Write
1  0  0 : Read

Ex: changing permission for files in whole directory recursively as read, write, execute
chmod -R -v 777 ./*

reference: chmod

Tuesday, September 14, 2010

chdir in perl for changing working directory

usage:
chdir EXPR
chdir(EXPR)

Changes the working directory to EXPR, if possible. If EXPR is omitted, changes to directory specified by $ENV{HOME}, if not set, changes to $ENV{LOGDIR} directory . if none of these is set then does nothing.

Returns 1 upon success, 0 otherwise.
Example:
chdir("C:\temp\working_dir\");

Using file copy and move in perl

copy function is used for copying a file from one location to another. Its present in File::copy module.
 
use File::Copy; 
copy("C:\temp\file1", "C:\tmp\file1.orig") or die "copy failed: $!";  # will make a copy of file1
copy("C:\temp\Fileout.txt", *STDOUT) or die "copy failed: $!";  # will print Fileout.txt into STDOUT
move("C:\temp\Fileout.txt", "C:\temp2\Fileout.txt") or die "move failed: $!";    # will move first argument to 2nd argument

This doesnt support features like creating backups of file, recursive copying etc. these functions also support file handles as argument.