Powered by Ajaxy
Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Perl: Creating a temporary file

#!/usr/bin/perl
use File::Temp qw(tempfile);
# Create temporary file
($fh, $fileName) = tempfile( );

Perl: Treating files in binary mode

open (FILEHANDLE, ">c:/test.txt")
    or croak ("Opening Failed");
binmode FILEHANDLE;
print FILEHANDLE "Welcom to perl.tubedir.com";
close (FILEHAND

Perl: How to modify the read, write, and execute permissions on a file.

#! /usr/local/bin/perl
$file_1="c:/test.txt";
chmod(0400, $file_1);  
chmod(0200, $file_1);  
chmod(0001, $file_1);

Perl: Copy a binary file

open file_src, "c:/icon2.gif"; 
binmode( file_src );
binmode( file_dest );
while ( read( file_src, $buffer, 1024 ) )
{
    print file_dest $buffer;

Perl: Change the owner and group of a list of files

$ uid=9496;
$ gid=40;
$number=chown($uid, $gid, 'test.vim', 'you.vim');

Perl: Open file for reading, writing, or updating

# Read only
open(FILEHANDLE, "", $path)                                or die $!;
sysopen(FILEHANDLE, $path, O_WRONLY|O_TRUNC|O_CREAT)        or die

Perl: Copying or Moving a File

#! /usr/local/bin/perl
use File::Copy;
$rep="c:/data.txt";
copy($rep,"d:/test")
    or die "can't copy, : $!";
# Move the file under a new name
move(

Perl: Reading files in a particular encoding

#!usr/bin/perl 
$file="c:/test";
 # Open a file encoded in utf-8
open(FH, "< :encoding(UTF-8)", $file)
    or die "can't open $file: $!";

Perl: How to Copy a file

open file_src, "c:/data.txt";  # target file
while (defined($line = ))  #read source file
{                                     # line by line
    #

Perl: PrograssBar Example

#!/usr/bin/perl
binmode(STDOUT,":unix");
for (0..20) {
    print "."; sleep 1;
}

Perl: Reading a Particular Line in a File

  #!/usr/bin/perl -w
  use Tie::File;
  use Fcntl;
  # Read Line 2
  @read_argv = ('c:/test.txt', 2)
  	or die "Error";
  ($filename, $line_number) =

Perl: Directory File Listing using opendir

chdir '..';                          #Current Directory
# Open
opendir(Dir, '.')
    or die "Unable to open directory.";
# List files
print join (',

Perl: Rename or delete a file/directory

#!usr/bin/perl
$rep="c:/test";
# Rename the file
rename($rep,"c:/test2");
# Delete the file
unlink($rep)
	or die "Unable to delete $rep: $!";

Perl: Counting Lines in a File

$file="c:/index.html";
open(FILE, ";
print "$file contains ",$count," lines";