open(DATA,"+>>file.txt") || die "Couldn't open file file.txt, $!";
close(DATA) || die "Couldn't close file properly";
< or r Read Only Access
> or w Creates, Writes, and Truncates
>> or a Writes, Appends, and Creates
+< or r+ Reads and Writes
+> or w+ Reads, Writes, Creates, and Truncates
+>> or a+ Reads, Writes, Appends, and Creates
Almost the same except it uses the system open() (instead of perl open?)
sysopen(DATA, "file.txt", O_RDWR|O_TRUNC );
O_RDWR Read and Write
O_RDONLY Read Only
O_WRONLY Write Only
O_CREAT Create the file
O_APPEND Append the file
O_TRUNC Truncate the file
O_EXCL Stops if file already exists
O_NONBLOCK Non-Blocking usability
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#!/usr/bin/perl
# get a file as an array
open(DATA,"<import.txt") or die "Can't open data";
@lines = <DATA>;
close(DATA);
unlink ("/usr/test/file1.txt"); # delete a file
rename ("/usr/test/file1.txt", "/usr/test/file2.txt" );
# get character
getc FILEHANDLE
# read a block of data
read FILEHANDLE, SCALAR, LENGTH, OFFSET
use File::Copy;
copy("file1","file2") or die "Copy failed: $!";
copy("Copy.pm",\*STDOUT);
move("/dev1/fileA","/dev2/fileB");
use File::Copy "cp";
$n = FileHandle->new("/a/file","r");
cp($n,"x");
#!/usr/bin/perl
# Open file to read
open(DATA1, "<file1.txt");
# Open new file to write
open(DATA2, ">file2.txt");
# Copy data from one file to another.
while(<DATA1>)
{
print DATA2 $_;
}
close( DATA1 );
close( DATA2 );
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
tell FILEHANDLE # returns the position of the file pointer, in bytes
seek FILEHANDLE, POSITION, WHENCE # whence is either start (0) , end, or current
my (@description,$size);
if (-e $file)
{
push @description, 'binary' if (-B _);
push @description, 'a socket' if (-S _);
push @description, 'a text file' if (-T _);
push @description, 'a block special file' if (-b _);
push @description, 'a character special file' if (-c _);
push @description, 'a directory' if (-d _);
push @description, 'executable' if (-x _);
push @description, (($size = -s _)) ? "$size bytes" : 'empty';
print "$file is ", join(', ',@description),"\n";
}
-A Age of file (at script startup) in days since modification.
-B Is it a binary file?
-C Age of file (at script startup) in days since modification.
-M Age of file (at script startup) in days since modification.
-O Is the file owned by the real user ID?
-R Is the file readable by the real user ID or real group?
-S Is the file a socket?
-T Is it a text file?
-W Is the file writable by the real user ID or real group?
-X Is the file executable by the real user ID or real group?
-b Is it a block special file?
-c Is it a character special file?
-d Is the file a directory?
-e Does the file exist?
-f Is it a plain file?
-g Does the file have the setgid bit set?
-k Does the file have the sticky bit set?
-l Is the file a symbolic link?
-o Is the file owned by the effective user ID?
-p Is the file a named pipe?
-r Is the file readable by the effective user or group ID?
-s Returns the size of the file, zero size = empty file.
-t Is the filehandle opened by a TTY (terminal)?
-u Does the file have the setuid bit set?
-w Is the file writable by the effective user or group ID?
-x Is the file executable by the effective user or group ID?
-z Is the file size zero?
opendir DIRHANDLE, EXPR # To open a directory
readdir DIRHANDLE # To read a directory
rewinddir DIRHANDLE # Positioning pointer to the begining
telldir DIRHANDLE # Returns current position of the dir
seekdir DIRHANDLE, POS # Pointing pointer to POS inside dir
closedir DIRHANDLE # Closing a directory.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Extract from a file into an array only lines containing "app.service1." , pass array to sub by reference
#!/usr/bin/perl
use Switch;
use strict;
use constant SERVICEHEADER1 => "app.service1.";
my @filedata = fileToArray( "/var/lib/tomcat6/webapps/authgateway/WEB-INF/app.properties" );
my @service1 = getServiceConfiguration( \@filedata , SERVICEHEADER1 );
printArray( \@service1 );
sub fileToArray( ;$ )
{
my $data_file = $_[0];
open( DATA , "$data_file" ) or die "can't open $data_file \n";
my @array_of_data = <DATA>;
close( DATA );
return @array_of_data;
}
sub getServiceConfiguration()
{
my( $ref_filedata, $servicenumber) = @_;
my @result;
foreach my $line ( @{$ref_filedata} )
{
my $match = $line =~ m/$servicenumber/ ;
if( $match )
{
push( @result , $line );
}
}
return @result;
}
sub printArray()
{
my( $ref_array ) = @_;
foreach my $value ( @{$ref_array} )
{
print $value;
}
}