Perl 2 – basic input/output

Posted on July 8, 2013

Standard inputs like C are also used here in perl. Whether you use these inputs for web based application or standalone command line application, the standard input is “stdin” and the standout output is “stdout”. To simplify the use, “stdout” is not necessary as used with a “print” statement.

For the command line usage:

print “What is your name?”;
The above statement will display the text  “What is your name?” on the screen.

For the web application usage:

print “Content-type:text/html”;
print “Hello there!”;

The above statement will display the text  “Hello there!” on the webpage.

The first line is needed to tell the brower what kind of input is coming after so the browser can handle it properly. Here the line tells the browser what comes after is text in html format. You can specify the following data will be an image, video or others. The line is necessary for web based application or the display will fail.

For the standard input, you use “stdin” as a handle and in command line mode, it is like:

$name=<stdin>;

The variable with name “name” is used to capture the input from the keyboard and you can then display the name after user enter the name:

print “Hello $name”;

For the web based “stdin”, you need a form to capture input from the user, once the user filled out the form, the data will be send to the server and to the script or perl program to execute:

<form action=”/cgi-bin/perlscript.pl” method=”post”> <input type=”text” name=”input” />
<input type=”submit” /> </form>
If there are more inputs in the form to send to the script through the standard input, all input data will be serialized (concatenated) with ‘&’ symbol and keeping the input data a single line going through the “stdin” and the script will be like:

$data=<stdin>;

The data received and stored in the variable $data need to be processed to make sense out of a single line of characters. We will talk more about processing data with the 3 kinds of variables later.

» Filed Under Education, perl | Comments Off on Perl 2 – basic input/output

Perl 1 – the basics

Posted on July 3, 2013

Most people will know about the basic of perl but for the newcomers, there they are, other than that you can search the web for these basics, in summary are simply:

1. The scalar variable such as $var, using a ‘$’ symbol in front of the name of the variable.

2. The vector variable such as @var, using an ‘@’ symbol in front of the name of the variable.

There will not be a conflict if the scalar variable and vector variable is using the same name as show in this example: ‘var’.  These two variables are using different memory space.

3. The hash variable, or called a table variable, or even a look-up variable. This variable behaves like a vector variable but with an order pair called ‘key’ and ‘value’.  The purpose is to map one quantity to another such as the month ‘january’ is 1, ‘february’ is 2 etc. This variable is constructed using a variable such as $month:

For example: %month=(‘january’=>1,’february’=>2,…);

Each of these variable has their purposes in any applications or programs. We will talk about the usage later on.

The most common errors that people have are forgetting the symbols ‘$’, ‘@’, and ‘%’ and sometimes forgetting how to use them properly.

More examples

$a_number=2.3;

$a_text=”a line or a few alphabets”;

$a_long_line=qq~

this is for mulitple

lines

~;

@temperatures=(2.3, 4,5, 5.7);

@names(‘john’,’jack’,’mary’);

%urls(‘microsoft’=>’www.microsoft.com’, ‘ibm’,’www.ibm.com’,’news’, ‘www.calgaryherald.com’);

» Filed Under Education, perl | Comments Off on Perl 1 – the basics

A different DPST switch

Posted on November 25, 2011

Today the students use a dip switch which is one that I have not seen before. It is modified from a traditional dip switch which has 8 SPST switches:

A modified dipswitch

When I tested the modified dip switch using the first set of switch which has 4 contacts or 4 pins, I found out the schematic of the switch which is a DPST switch but it is not a normal one. The normal DPST schematic looks like this:

Double Pole Single Throw switch

The modified one looks like this:

New Dip Switch schematic DPST, weird version

To implement a single SPDT switch using this kind of DPST switch, you can wire in the following way:

Turning DPST to SPDT

The specification of the dip switch can be found at:
http://www.ctscorp.com/components/Datasheets/206-208.pdf

» Filed Under Education, electronics | Comments Off on A different DPST switch

Active HIGH or Active LOW

Posted on November 23, 2011

These two terms confused a lot of people. What is it?

Any logical point has a level HIGH or LOW. Adding the word “active” implies some meaning. From my experience is that if we use it for positive logic, that is, use the HIGH, we called this point or line is active HIGH, and if we use it for negative logic, that is, using the LOW, we called this active LOW.

Let’s say, an input or output line called A has a level HIGH or LOW. If we use this line to enable other digital system, then we called this active HIGH and labelled it as EN, which stands for ENABLE logic.

If A is inverted and it is the LOW level that is used to do enabling, then we called the line active LOW and we labelled the line EN.

» Filed Under Education, electronics | Comments Off on Active HIGH or Active LOW

Some tricks I learned when designing digital circuit

Posted on November 23, 2011

I learned in the past that you can turn a NAND gate into an inverter:

Say NAND gate is (A*B)’ and when you make or force B to be 1 or HIGH, then we have (A*1)’ which is simply A’ so A is inverted.

Same idea is for NOR gate. NOR is (A+B)’. When B is forced to be grounded or 0, then (A+B)’=(A+0)’=A’. Similarly A is inverted.

So when you do not have inverter or you do not want to use another inverter, you can use this tricks to turn NAND gate or NOR gate to an inverter.

It is funny that in one of the test bank that the question ask how do you say about input B for an NAND gate such that the output follows the input A. It shocked me because you can make output the inversion of the input A but not following it. Well if the gate is AND gate, it is all right.

A*B=A*1=A, so the ouput will be identical to A, if A is high, output is high, if A is low, output is low. Change AND gate to NAND gate is absolutely ridiculous.

Similar problem happened to another question using NOR gate. I said, use OR gate is correct.

There is another way to make NAND gate an inverter or AND gate a follower:

(A*B)’=(A*A)’=A’ or
(A*B)=(A*A)=A

This is the trick to make A the same as B, that is, tie the pin A and pin B together, you will get inversion of A using NAND gate or a follower of A using AND gate.

» Filed Under Education, electronics | Comments Off on Some tricks I learned when designing digital circuit

keep looking »

  • Recent Comments