Difference between exec and system in perl?

scmuser created the topic: Difference between exec and system in perl?

What are the Difference between exec and system in perl? please explain….

rajeshkumar replied the topic: Re: Difference between exec and system in perl?

exec function in perl

The exec function executes a system command and never returns; use system instead of exec if you want it to return. It fails and returns false only if the command does not exist and it is executed directly instead of via your system’s command shell

Since it’s a common mistake to use exec instead of system, Perl warns you if there is a following statement that isn’t die, warn, or exit (if -w is set–but you always do that, right?). If you really want to follow an exec with some other statement, you can use one of these styles to avoid the warning:
exec (‘foo’) or print STDERR “couldn’t exec foo: $!”;
{ exec (‘foo’) }; print STDERR “couldn’t exec foo: $!”;

Example:
$shell = ‘/bin/csh’;
exec $shell ‘-sh’; # pretend it’s a login shell

or
exec {‘/bin/csh’} ‘-sh’; # pretend it’s a login shell

More..

Executes a system command (directly, not within a shell) and never returns to the calling script, except if the command specified does not exist and has been called directly, instead of indirectly through a shell. The operation works as follows:

* If there is only one scalar argument that contains no shell metacharacters, then the argument is converted into a list and the command is executed directly, without a shell.
* If there is only one scalar argument that contains shell metacharacters, then the argument is executed through the standard shell, usually /bin/sh on Unix.
* If LIST is more than one argument, or an array with more than one value, then the command is executed directly without the use of a shell.

Return Value: 0 only if the command specified cannot be executed

Example

Following are the usage…
exec ‘/bin/echo’, ‘Your arguments are: ‘, @ARGV;
exec “sort $outfile | uniq”;

Another example:
exec {‘/bin/csh’} ‘-sh’; # pretend it’s a login shell

System function in perl

Does exactly the same thing as exec LIST , except that a fork is done first, and the parent process waits for the child process to exit. Note that argument processing varies depending on the number of arguments. If there is more than one argument in LIST, or if LIST is an array with more than one value, starts the program given by the first element of the list with arguments given by the rest of the list.

Executes the command specified by PROGRAM, passing LIST as arguments to the command.The return value is the exit status of the program as returned by the wait function. To obtain the actual exit value, divide by 256.

Return Value : Exit status of program as returned by wait

Example

Try out following example:
#!/usr/bin/perl -w
system(“ls -F /var > /tmp/t.tmp”);

It will produce following result:

A file in /tmp direcory, check it out.

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :

How to read properties file using perl

scmuser created the topic: How to read properties file using perl

Hi,

i would like to read properties file using perl…Any sample code or any example?

My properties file is as such …

variable1 = value
variable2 = value

tpatil replied the topic: Re: How to read properties file using perl

Hope this helps….

{code}
#! /usr/bin/perl -w
use Getopt::Long;
use strict;

my $inputFile = "test.properties";

#############################################
# Get the variables passed from property file
#############################################
my @parmList = &read_parameters;

##############################################################
# populate hash map
##############################################################
%parameters = @parmList;

#print the property1 value from test.properties
print "$parameters{"property1"}"\n"

##############################
#Subroutine to read the values
##############################
sub read_parameters {

# my $importFile = $filePath . $inputFile;
my $importFile = $inputFile;
my $parameterName = '';
my $parameterValue = '';

# open the file and read the contents into an array
open(DATA, "<$importFile") or die "\n\nUnable to open input file: $!\n\n"; my @parmListTemp = ;
close(DATA);

my @parmList = ();

foreach my $row (@parmListTemp){
chop $row;

# ignore blanks and lines that start with #
if (($row ne '') && ($row !~ /^#/)) {

# get parameters and their values and put into an array
($parameterName, $parameterValue) = split(/=/, $row);

if($debugLevel >= 3) {print "\n\n Parameter $parameterName value is $parameterValue"; };

push(@parmList, $parameterName);
push(@parmList, $parameterValue);
};
};

return @parmList;
};
{/code}

rajeshkumar replied the topic: Re: How to read properties file using perl

Hi Tushar,

Can you please help me on this problem

www.scmgalaxy.com/forum/perl-script/crea…to-mysql-db.html#679

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :

To test mysql remote server access using perl

rajeshkumar created the topic: To test mysql remote server access using perl

perl -MDBI -e ‘DBI->connect(“DBI:mysql:rajesh:rajeshk-W7″,”rajesh”,”rajesh123″) or die DBI->errstr;’

perl -MDBI -e ‘DBI->connect(“DBI:mysql:myDB:server2.whatever.co.uk”,”myuser”,”mypassword”) or die DBI->errstr;’

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :

How to Open Directory and read and display all the file using perl

rajeshkumar created the topic: How to Open Directory and read and display all the file using perl

Method 1

$DIR = "/home/rajesh";
$DIRHANDLE = "HANDLE";

opendir ($DIRHANDLE, "$DIR") || die "Error Opening $DIR : $!";
@dirlist = readdir ($DIRHANDLE);
foreach (@dirlist)
{
print "$_ \n";
}

Method 2

@dirlist = `ls -A $DIR`;
foreach (@dirlist)
{
print "$_ \n";
}

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :

How to identify which perl module has been installed.

scmuser created the topic: How to identify which perl module has been installed.

How to identify which perl module has been installed.

rajeshkumar replied the topic: Re: How to identify which perl module has been installed.

rmp -qa | grep perl

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

puneetbhatia77 replied the topic: Re: How to identify which perl module has been installed.

I tired this command at ubuntu but got error.

rmp -qa | grep perl
No command ‘rmp’ found, but there are 20 similar ones
rmp: command not found

DO we need to install rmp package?

Regards,
Puneet

rajeshkumar replied the topic: Re: How to identify which perl module has been installed.

Hi Puneet,

I guess ubuntu support .deb package manager by default..

you need to have RPM installed. May be following URL should help you out?
embraceubuntu.com/2005/09/23/installing-using-an-rpm-file/

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

amavila replied the topic: Re: How to identify which perl module has been installed.

Hi,
rpm or dpkg commands not to be used for listing perl modules as there are possibilities that the Perl modules are installed by downloading from CPAN web or using cpan commands.

To list perl modules –
On windows/*nix use the command: “instmodsh” (check the documentation : perldoc -i instmodsh)

If you are using Active Perl distribution you can try the command “ppm list”

Regards,
Shiva

Tagged :

Find out which perl module installed?

scmuser created the topic: find out which perl module installed?

Hi,

what is the way to find out perl module which has been installed in unix / linux system?

rajeshkumar replied the topic: Re: find out which perl module installed?

try out this?
find `perl -e ‘print “@INC”‘` -name ‘*.pm’ -print | tee ~/Perl-modules-installed.txt

Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn

Tagged :