Wednesday, January 23, 2013

How could you comment out block of code in Ruby?

Multiple lines of text or code can be marked as comments using the Ruby "=begin" and "=end"  markers. These are known as the comment block markers.

=begin
This is a comment line.
It shows that the next line of code is
marked as a comment.
=end

How could you comment out block of code in Python?

Use triple-quoted strings.

#!/usr/bin/python
   
'''
print "This is a \
multiline \
comment.";
'''

Monday, January 21, 2013

What are the various ways you can run UNIX commands in Ruby?

Backticks (``) - Backticks (aka backquotes) runs the command in a sub-shell and returns the standard output from that command.

#!/usr/local/bin/ruby -w
today = `date`
puts today

System (Kernel.system) - This command runs in a subshell and returns true if the command ran successfully and false otherwise.

system "ls -1"

Exec (Kernel.exec) - It runs the given command by replacing the current process with the one created by the external command.

exec "ls -1"

spawn (available in Ruby v1.9. aka Kernel.spawn also available as Process.spawn) - It doesn‘t wait for end of the command but returns the pid of the sub-process. Therefore, you'll have to use Process.wait or Process.detach on the resulting pid.

pid = kernel.spawn ("ls -lA", {:chdir => "/home/ravi"})
Process.wait pid

%x operator - The operator makes it easy and readable to use quotes in a command.

#!/usr/bin/env ruby
list=%x[ls -1]
puts list

IO.popen - It is another way to run a command in a sub-process. It helps you control the connection to IO object between standard input and standard output in that the sub-process.

Open3.popen3 - The Ruby standard library includes the class Open3. It is easy to use and returns stdin, stdout and stderr.

Open4.popen4 - It is a Ruby Gem put together by Ara Howard. It operates similarly to open3 except that we can get the exit status from the program. It also returns a process id for the subshell and we can get the exit status from that waiting on that process.

How could you run UNIX commands in Python?

#!/usr/bin/python
import os
os.system("ls -l")

or

import os
buffer=os.popen("ls -l")
for i in buffer.readlines():
     print "Result:",i,

or

from subprocess import call
call(["ls", "-l"])

What are the modules available in Perl to run external commands or 3rd party programs?

On UNIX:
  •     IPC::Open2
  •     IPC::Open3
  •     IPC::Run
On Windows:
  •     Win32::Process::Create

Friday, January 18, 2013

How could you add or remove elements in a Hash in Perl?

%FM = ( 'Radio City' => 91.1,
              'Big FM' => 92.7,
              'Red FM' => 93.5,
              'Radio One' => 94.3,
              'Radio Mirchi' => 98.3,
              'Fever' => 104.0,
              'Oye FM' => 104.8,
              'FM Rainbow' => 107.1,
              'FM Gold' => 100.7);

# Add one more element to the hash
$FM{'FM Berhampur'} = 100.6;

# Remove one element from the hash
delete ( $FM{'FM Gold'} );

foreach ( keys %FM )
{
    print "$_ => $FM{$_}\n";
}

How could you find out size of a Hash in Perl?

%FM = ( 'Radio City' => 91.1,
              'Big FM' => 92.7,
              'Red FM' => 93.5,
              'Radio One' => 94.3,
              'Radio Mirchi' => 98.3,
              'Fever' => 104.0,
              'Oye FM' => 104.8,
              'FM Rainbow' => 107.1,
              'FM Gold' => 100.7);

print "size of Hash: ",scalar keys %FM,"\n";
# or
print "size of Hash:  " . keys ( %FM ) . ".\n";

How could you check the existance of a key in a Hash in Perl?

%FM = ( 'Radio City' => 91.1,
              'Big FM' => 92.7,
              'Red FM' => 93.5,
              'Radio One' => 94.3,
              'Radio Mirchi' => 98.3,
              'Fever' => 104.0,
              'Oye FM' => 104.8,
              'FM Rainbow' => 107.1,
              'FM Gold' => 100.7);

if (exists ( $FM { "Fever" } ) )
{
    print "Frequency of FM Fever is $FM{Fever}MHz.\n";
}

Thursday, January 10, 2013

What does bless() do?

The process of turning a reference into an object is called 'blessing' and the bless() function
just turns the reference into an object of the required class.

Thursday, January 3, 2013

What is the difference between pipe and xargs?

xargs converts the input into command line arguments and is useful when the parameter list is too long for the shell. It splits the parameter list and iterates over the smaller list.

The pipe takes the standard output of the first command and copies it to the standard input. Roughly, pipe is an option to concatenate two/more commands.