Tag Archives: software

Examples of recursion, in Perl, Ruby, and Bash

This article is in response to the following question posted in the Perl community group on  LinkedIn:

I’m new to PERL and trying to understand recursive subroutines. Can someone please explain with an example (other than the factorial ;) ) step by step, how it works? Thanks in Advance.

Below, are some very simplified code examples in Perl, Ruby, and Bash.

A listing of the files used in these examples:

blopez@blopez-K56CM ~/hello_scripts 
$ tree .
 ├── hello.pl
 ├── hello.rb
 └── hello.sh
0 directories, 3 files
blopez@blopez-K56CM ~/hello_scripts $


Recursion example using Perl:

– How the Perl script is executed, and it’s output:

blopez@blopez-K56CM ~/hello_scripts $ perl hello.pl "How's it going!"
How's it going!
How's it going!
How's it going!
^C
blopez@blopez-K56CM ~/hello_scripts $

– The Perl recursion code:

#!/usr/bin/env perl
use Modern::Perl;
my $status_update = $ARGV[0]; # get script argument
 
sub hello_world
{
    my $status_update = shift; # get function argument
    say "$status_update";
    sleep 1; # sleep, or eventually crash your system
    &hello_world( $status_update ); # execute myself with argument
}
 
&hello_world( $status_update ); # execute function with argument


Recursion example using Ruby:

– How the Ruby script is executed:

blopez@blopez-K56CM ~/hello_scripts 
$ ruby hello.rb "Doing great!"
Doing great!
Doing great!
Doing great!
^Chello.rb:7:in `sleep': Interrupt
    from hello.rb:7:in `hello_world'
    from hello.rb:8:in `hello_world'
    from hello.rb:8:in `hello_world'
    from hello.rb:11:in `'
blopez@blopez-K56CM ~/hello_scripts $

Note: In Ruby’s case, stopping the script with CTRL-C returns a bit more debugging information.

– The Ruby recursion code:

#!/usr/bin/env ruby
status = ARGV[0] # get script argument
 
def hello_world( status ) # define function, and get script argument
    puts status
    sleep 1 # sleep, or potentially crash your system
    return hello_world status # execute myself with argument
end
 
hello_world status # execute function with argument

Recursion example using Bash:

– How the Bash script is executed:

blopez@blopez-K56CM ~/hello_scripts $ bash hello.sh "..nice talking to you."
..nice talking to you.
..nice talking to you.
..nice talking to you.
^C
blopez@blopez-K56CM ~/hello_scripts $

– The Bash recursion code:

#!/usr/bin/env bash
 
mystatus=$1 # get script argument
 
hello_world() {
    mystatus=$1 # get function argument
    echo "${mystatus}"
    sleep 1 # breath between executions, or crash your system
    hello_world "${mystatus}" # execute myself with argument
}
 
hello_world "${mystatus}" # execute function with argument

The Apache Software Foundation Celebrates the 17th Anniversary of the Apache HTTP Server with the release of v2.4

World’s most popular Web Server powers nearly 400 million Websites across the globe

Numerous enhancements make Apache HTTP Server v2.4 ideally suited for Cloud environments. They include:
•    Improved performance (lower resource utilization and better concurrency)
•    Reduced memory usage
•    Asyncronous I/O support
•    Dynamic reverse proxy configuration
•    Performance on par, or better, than pure event-driven Web servers
•    More granular timeout and rate/resource limiting capability
•    More finely-tuned caching support, tailored for high traffic servers and proxies.

Read the full press release at The Apache Foundation’s blog.

Understanding the Concepts of Classes, Objects, and Data-types in Computer Programming

book-car

Every once in a while I get into discussions with various people about computer programming concepts such as objects, classes, data-types, methods, and functions.

Sometimes these discussions result in questions about the definition of these terms from those who have some programming experience, and sometimes these questions come from those who have no background in computer science or familiarity with programming terminology whatsoever.

I usually attempt to answer these questions using various metaphors and concepts that I feel the individual or group may be able to relate to.

It is quite possible that even my own understanding of these concepts may be incorrect or incomplete in some way. For the sake of reference and consistency, I am writing this brief article to explore these concepts in the hope that it will provide clarity into their meaning and purpose.

 

So, what is a data-type?

Some languages, like C, have a strict set of data-types. Other languages, like C++, and Java offer the developer the ability to create their own data-types that have the same privilages as the data-types that are built into the language itself.

A data-type is a strict set of rules that govern how a variable can be used.

A variable with a specific data-type can be thought of in the same way as material things in the real world. Things have attributes that make them what they are. For example, a book has pages made of paper that can be read. A car is generally understood to be an automobile that has four wheels and can be used for transport. You cannot drive a book to the grocery store, in the same sense that you cannot turn the pages of a car.

A data-type is a specific set of rules for how a variable can be used.

 

Data-types in computer programming may include examples such as:
_____________________ 
Object Type
==== ========
Int = number, no decimal places
Float = large number with decimal places
Char = a plain-text character
_____________________  

 

More familiar, real-world examples may include:
_____________________
Object Type
==== ========
Bucket = Strong, can hold water, has handle
Balloon = Fragile, can hold a variable amount of air, elastic, portable
Wheel = Round, metal, rubber, rolls
_____________________ 

 

In languages like C++, there are core data-types such as the ones found in C. However, C++ also offers developers the ability to create their own data-types.  Providing developers the ability to create their own data-types makes the language much more flexible. We more commonly refer to a user-defined data-type by the more popular term, class.

In C++, a class is a user-defined data-type [1]. That’s all it is. It provides the developer the ability to create a variable (or object) with specific attributes and restrictions, in the same way that doing “int dollars = 5;” creates an object called “dollars” who’s attribute is to have a value which is strictly an integer. In the real world, a five-dollar bill cannot be eaten (technically), and it cannot be driven like a car to a grocery store (even though that’s where it will likely end up).

An object is a variable that has been defined with a specific data-type. A variable is an object when it is used as an intance of a class, or when it contains more than just data. An object in computer programming is like an object in the real world such as a car, or a book. There are specific rules that govern how an object can be used which are inferred by the very nature of the object itself.

The nature of computer programming means that developers have the ability to redefine objects, for example making the object “book” something that can be driven. In the real world however, we know that you can call a car a book, but it’s still a car. The core understanding of what a car is has been ingrained within us. Although “car” is simply a three letter word (a symbol, or label), there are too many people and things in the world that depend on the word “car” having a specific definition. Therefore objects in the real world cannot be as easily redefined as their counter-parts in computer programming (however, it is still possible [2]).


So what is a method?

In computer programming, we have things called “functions”. A function is an enclosed set of instructions which are executed in order to generate (or “return”) a specific result or set of results. You can think of a function as a mini program. Computer programs are often created by piecing together multiple functions in interesting and creative ways.

Functions have many names, and can also be referred to as subroutines, blocks and methods. A method is a function which is specifically part of a class, or a user-defined data-type, which makes a method an attribute of an object – something that the object is capable of doing.  Just like in the real world, methods can be manipulated and redefined for an object, but not for that object’s base class.  A book can be used to prop-up a coffee table, but that does not mean that books are by definition meant to be used in this way.


Enlightenment achieved!

I’m not really sure where I was going with all of this, but the above should be sufficiently lucid.   I was motivated to write this after recently referencing Bjarne Stroustrup’s “The C++ Programming Language”.  If you’ve ever asked yourself the question “what is an object?” or “what is a class?”, then the above descriptions should serve as a useful reference.

[1] “The C++ Programming Language – Special Edition”, page 224.

[2] For example, the definition of “phone” has been redefined several times in recent history, from the concept of a dial-based phone, to cell phones, to modern smart-phones.