Skip to main content

Posts

Showing posts with the label perl

Creating Win32 Process in Perl and handling priority of process

Main.pl -> parent process use strict; use warnings; use function; use Win32::Process; use constant ABOVE_NORMAL_PRIORITY_CLASS => 0x00008000; my $cmd="C:\\windows\\system32\\cmd.exe"; my $params = "cmd.exe /c perl.exe C:\\Dropbox\\Pet_Projects\\perl_process\\process.pl"; my $startFolder = shift || q{.}; my $procobj; print "\n before calling the child process \n"; #Creates a process with the command and the params and flags Win32::Process::Create( $procobj,$cmd, $params, 0,CREATE_NEW_CONSOLE|                                  ABOVE_NORMAL_PRIORITY_CLASS , $startFolder ); $procobj->Wait(INFINITE); -> wait for child to complete print "\n after calling the child process \n"; Process.pl -> child process use strict; use warnings; use function; for (my $i =0;$i < 120;$i...

Arrays in Perl

One Dimensional Array:- Arrays are great in Perl, we can store heterogeneous data elements such as character, integer and floats. Ex: - @array = (“surendra”, 1, 12,”tux”); $# operator gives the size of the array. Ex: - $#array => 4 Accessing Arrays:- With dollar operator along with array name and index number inside square braces will give the array element. Ex: - @array = (“surendra”, 1, 10,”patil”);                 $arra [0] => Surendra                 $array[2] => 10 Program to display all the elements in an array:- use strict; use warnings; # # Print Array Elements # my @array = ("Surendra",10,111,"patil","tux"); my $i = undef; for ($i = 0; $i < $#array; $i++) {                 print "$array[$i...

Perl Script to Replace string in a file

#!/usr/bin/perl #Script:- This Script Replaces the string with given string #Author:- Surendra Patil #Date:- 04/04/2013 use strict; use warnings; my $line = undef; #open the file in reading and writing mode open(INPUT_FILE,”+<names.txt”); #store the contents of the file into memory my @data = <INPUT_FILE>; #set the file pointer to the start of the file seek INPUT_FILE,0,0; #loop and replace the string with ignoring case foreach $line (@data) { $line =~ s/belgaum/california/ig; print INPUT_FILE $line; } close INPUT_FILE; #display the modified file open(INPUT_FILE,”<names.txt”); while (my $line = <INPUT_FILE>) { print “$line”; } close INPUT_FILE; =================================================== Input file:- string.txt Surendra Patil From Nidasoshi  belgaum       Karnataka Bangalore India ==================================================== Output:- Surendra Patil From Nidasoshi  california     ...