TechiWarehouse.Com

Programming Languages


Computer Training Excuses - No ExcuseYou wouldn't go to a doctor who had never been to medical school, or hire a lawyer who never studied law. One side-effect of a world advancing as rapidly as ours is that fields are becoming more and more specialized and narrow. People can no longer get by on general knowledge in their careers, something I found out for myself not too long ago. I'd been out of high school for two years, scraping by on my own and picking up scraps of programming as I went. I saw all of the self-taught programmers breaking into the IT industry, and I hoped to do the same. After all, IT is one of the few industries out there where being creative and a quick learner is more important than a degree.
Divider Line

Which is the Easiest Programming Language? - Programming LanguagesIf you have some misconception in mind that the programming languages are easy or hard, I am afraid to say that you are mistaken. To tell you frankly about the programming languages, there is nothing such as easy or tough programming language. The only thing that you need to keep in mind while stepping into the world of programming languages is that which programming language would set your base by giving you in-depth knowledge of programming logics and the basics of programming techniques.
Divider Line

Perl Tutorial - Perl Tutorial A simple as it gets and no bull $#!t perl tutorial. Explains the low downs of the language involving some realy important basic concepts. Perl is short for Practical Extraction and Report Language. It's a language that is available free over the Web, and it's used for a variety of things, from writing CGI scripts to assisting administrators in maintaining their systems.
Divider Line

Entering into Programming World - Good Luck ProgrammingOnce you have learned some programming languages, you feel yourself completely prepared to enter into the programming world, and start working on the programming languages that you have learned. Many students also feel that they have learned extra things which might be helpful in their programming careers. The matter of fact is that the real world is entirely different from what you have been taught in the classrooms.
Divider Line

Basic Object-Oriented Concepts - Basic Object-Oriented Concepts There is an old story of how several blind men set out to understand what an elephant was by examining a live specimen. Each of them explored a different part of the elephant's body. One blind man, falling against the elephant's side, proclaimed that an elephant must be very much like a wall. Another, grasping the elephant's ear, decided that an elephant must closely resemble a leaf. One grabbed the elephant's tail and determined that elephants must resemble ropes.
Divider Line

Best of the Web Hosting Show Guides - Those Arent the Moving PantsWhen moving from house to house, you have to pack up all of your belongings in the old house before you can move, right? The same could be said when you are moving from web host to web host. The first and most important thing you can do is backup your web site and get it ready for the move. Remember to grab all of your static files. This would be all of your non-dynamic pages, images, templates and more. The exact files that you do backup might change depending on how your site is setup.
Divider Line

Ten Perl Myths - Ten Perl MythsOne of the things you might not realize when you're thinking about Perl and hearing about Perl is that there is an awful lot of disinformation out there, and it's really hard for someone who's not very familiar with Perl to separate the wheat from the chaff, and it's very easy to accept some of these things as gospel truth - sometimes without even realising it. What I'm going to do, then, is to pick out the top ten myths that you'll hear bandied around, and give a response to them. I'm not going to try to persuade you to use Perl - the only way for you to know if it's for you is to get on and try it - but hopefully I can let you see that not all of what you hear is true.
Divider Line

Why Programmers Love the Night - Programming at night One famous saying says that programmers are machines that turn caffeine from coffee and Coca Cola into a programming code. And if you ask a programmer when does he like working the most and when is he most productive - he is probably going to say late at night or early in the morning, that is when he has the greatest energy and concentration to work.
Divider Line

Paths in Perl - Paths in PerlDifferent Operating Systems use different characters as their path separator when specifying directory and file paths. Check out how and what is used in PERL on *nix.
Divider Line

How to be a Programmer - Programing training To be a good programmer is difficult and noble. The hardest part of making real a collective vision of a software project is dealing with one's coworkers and customers. Writing computer programs is important and takes great intelligence and skill. But it is really child's play compared to everything else that a good programmer must do to make a software system that succeeds for both the customer and myriad colleagues for whom she is partially responsible. In this essay I attempt to summarize as concisely as possible those things that I wish someone had explained to me when I was twenty-one.
Divider Line

TW Tech Glossary - Misplaced your bible? Well here it is - Tech Glosasary! This truly took a while to complete and should be used by all from beginners to advance techies.
Divider Line

How to Install the Aptana Studio IDE on Windows - Aptana Studio 3IDE (Integrated Development Environment) represents an integrated development environment in which you can develop applications. This tutorial will be useful to all those that considering doing programing...
Divider Line

What Programming Language To Learn - Programming LanguagesOne of the most common questions we hear from individuals hoping to enter the IT industry is, "What programming languages do I need to know?" Obviously this is a complex question, and the answer will depend on what field the questioner is going into. Many programming languages are taught during courses used to obtain a computer information science degree. However, those already in IT know that the greatest skill you can have is to be a jack-of-all-trades. A well-prepared worker can switch between computer programming jobs with only minimal training, thanks to a wide knowledge of multiple programming languages.
Divider Line

What is Perl?

PERL stands for for Practical Extraction and Report Language. Perl is a programming language developed by Larry Wall, especially designed for processing text. Because of its strong text processing abilities, Perl has become one of the most popular languages for writing CGI scripts. Perl is an interpretive language which makes it easy to build and test simple programs.


Tips

File I/O and Permissions

The usual way of writing to a file in Perl works in CGI scripts as well. First open the file on a file handle for writing (or die if you can't; better yet, send an error message), then print to the file and close it:
 

$text = "whatever";
open (FILE, ">$file_name")
|| die "Can't open $file_name for writing.";
print FILE "$text";
close FILE;

Remember, though, that the server is executing the script, not you, so the server must have permission to write a file in your chosen directory. Commonly, the server runs as user "nobody" or someone else with minimal permissions, so this requires world read and write access. You should set the directory permissions as chmod 766 . If you want to append to an existing file (using >> instead of > in the open statement above) make sure the file the script is appending to also has 766 permissions.

Of course, this means that anyone with an account on your system can edit or delete these files. Most web servers are configured this way for security reasons -- security for the server and the system, not the web pages. New CGI developers are appalled when they first hear of this problem, but there is no perfect solution on a public system.

Testing File Names

When your script writes to a new file, you probably want it to create a new and unique name for the new file, one that doesn't conflict with any existing files, which would be overwritten. One way to create a new file name that's unique is to incorporate the process id and the time into the name. Perl's special variable, $$ returns the current pid and $^T returns the time (in seconds since 1970). So you could use something like $filename = "$$" . "$^T" . ".html"; Neither alone will guarantee uniqueness since there are only a finite number of process id's, which are recycled, and your script could have been accessed twice within the same second.

This results in ugly filenames, something like "83498127310497.html". If you have prettier names that you insist on, you can test for the existence of a file with the proposed new name, using Perl's -e operator. -e $file_name is true if a file already exists with that name. In the example below, the variable $text holds some key text taken from the contents of the file that we want to use in the name. We're also assuming that the script is constructing a web page, so we add ".html" to the end of the name.
 

$file_name = $your_chosen_dir . $text . ".html";
if ( -e $file_name ) {
## do something to make it different, like
## substitute pidtime.html for html at the end
$file_name =~ s/html$/$$$^T\.html/;
}

Presumably you'll get mixtures of pretty and ugly filenames, and then only occasionally.

Locking a File

A different issue arises when you want your CGI script to write or append to an existing file. There is a (small) chance that two different processes (users submitting your form) will want to write to the file almost simultaneously. If one opens the file before the other closes it, the last to close will overwrite any changes the first tried to make.

One approach to this problem is to keep files open for appending or writing as briefly as possible. Try to prepare as much of your text as possible in advance of opening the file, so that there are as few operations as possible while the file is open. For example, to append the contents of $text to the file, you might write
 

... a lot of code to prepare ...
... a complicated $text ..
open (FILE, ">>$file_name")
|| die "Couldn't open $file_name.";
print FILE "$text\n";
close FILE;

This is not a single, atomic operation however, so it's no guarantee that another process couldn't open the file before this process closed it. As well, this idea is only sensible for appending to the end of a file. If you want to open the file, read some of it, insert something in the middle, then read the rest, you can spend a long time inside the file. Another approach is to "lock" the file, so that only one process can open it for writing at a time. Unix provides the flock(2) and fcntl(2) system calls (and Perl has its own versions of these by the same names).

However, these don't provide the ability to lock the file for writing, but still allow other processes to access it for reading, If you don't mind locking the file against read access, you can consider these system calls. But if the file is a web page, or something the web server may otherwise want to access, you may want to permit read access, but lock out write access.

How to Install a Perl Script in Unix

The correct path to perl should appear after '#!' on the first line of the script. Typically it will be
 

'#!/usr/local/bin/perl' or '#!/usr/bin/perl'

If necessary, configure the script as instructed in the documentation that comes with it.
Using FTP, move your script to your server. Be sure to use ASCII mode.

Set the permissions on the script. If using telnet type chmod 755. If using a graphical FTP program, follow its instructions. Only the owner (you) should have write access, but anyone should be able to read and execute it. Test the script by typing in the URL to the script in your browser.

Sending email f rom a Perl script is easy with Net::SMTP.
 

Include the Net::SMTP module in your script. Example: use Net::SMTP;
Create a new object with 'new'. Example: $smtp = Net::SMTP->new("smtp.your_isp.com");
Send the MAIL command to the server. Example: $smtp->mail("someone\@sender.com");
Send the server the 'Mail To' address. Example: $smtp->to("someone_else\@recipient.com");
Start the message. Example: $smtp->data();
Send the message. Example: $smtp->datasend("Hello World!\n\n");
End the message. Example: $smtp->dataend();
Close the connection to your server. Example: $smtp->quit();

 

How to Write Data Files in Perl

Saving data to files is a critical step in any programming language. Learn how to do it in Perl.

To open a file, use the open(FILE_HANDLE, filename) function as detailed in steps 2 and 3.
Use write mode when you want to save data, overwriting any data currently in the file. Example: p
 

en(FD, "> test.dat") or die("Couldn't open test.dat\n");

Use append mode when you want to open a file for writing without destroying the current contents. Example:

open(FD, ">> test.dat") or die("Couldn't open test.dat\n");

Use print() or printf() with the filehandle to send output to the file rather than the screen. Example:

print FD "Hello World!\n";

Close the file with close( FILEHANDLE); Example:

close(FD);

Why do I keep getting 'Unable to Open data.txt"?

there are a few reasons for this...

1) your $boarddir doesn't point to the right directory where data.txt is kept.

2) You haven't chmod'd it correctly (777).

3) You need to up load all .cgi - txt files in ASCII... not binary.

4) make sure there is a data.txt there.

How do I change the time zones to reflect the times where I live??

You need to go to '&get-date' in both index.cgi and Robboard.cgi

Look for this line...
 

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);

and change it to...

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time() + 3600);

change the 3600 to whatever time zone offset your in..

How do I add the name of the post in email messages?

elsif (//) {
Under..
if ($set_email) { ## line added for email on/off mod
Add..
print "$mname[$idnum]\n";

And there you have it.. The message title will appear in the subject of your email :)

How do I find my full home pathway?

You have a few choices here...

1) Go to the help/support pages of your host provider. (recommended)

2) If ALL files are in the same folder as your cgi's then you can use the following line for your pathway..
 

$directory = '$ENV{'DOCUMENT_ROOT'}';

OR

$directory = 'directory';

3) You can go to the scripts section of this site to download a little script I wrote to find your pathway.

How can I use the members login with multiple millsieboards?

1) Make 3 directories in your cgi-bin. Call them "boards1" "boards2" and "board_members".

2) Make sure you have all the files of MillsieBoard in the "boards1" and "boards2" directories except for "pwd.txt" and the "portfolio" directory. Put those to files only in "board_members" in your cgi-bin.

3) In your cgi-bin you should have these files:

index.cgi
index2.cgi
members.cgi
members2.cgi
millsieboard.cgi
millsieboard2.cgi
millsieboard.cfg
millsieboard2.cfg
cookie.lib

4) Open up "millsieboard.cfg" and "millsieboard2.cfg". Change the paths of "$pw_file" and "$profiledir" (lines 77 & 78) to something like the following:
 

$pw_file = "/home/username/public_html/cgi-bin/board_members/pwd.txt"; $profiledir = "/home/username/public_html/cgi-bin/board_members/profile";

5) Open all of the CGI files with the number 2 in them and change:
 

this...
require 'boards.cfg';
to this...
require 'boards2.cfg';

6) [same step taken in regular multi-board mod to make a ssi page]

****NOTE: If you want more than 2 boards do the same steps, just add more files accordingly.***

How to convert date to UNIX time in perl (or vice versa)

Time return number of seconds since 0 in your computer (1 jan 1970) on UNIX localtime converts unixtime to normal dates

To convert a specific date to its corresponding unixtime, you use Time::Local, the function looks like this:
 

$time = timelocal($sec,$min,$hours,$mday,$mon,$year);
so to convert 3rd of Sept 2000 to unixtime, (note that jan=0,feb=1,...):
use Time::Local;
$time = timelocal(0,0,0,'3','08','2000');


Installation

After installing ActivePerl and SynEdit on Windows 98, you will obtain a SynEdit option in your Programs menu. Use this for starting SynEdit. You will use this program both for editing and running Perl. In order to do the latter, you must create a file perlbat.bat in the directory Perl with the following content:
 

@echo off
perl -w %1
pause

and set the "Close on exit" property of this file. Then you need to define the default compiler under the Advanced menu of SynEdit as
 

perlbat.bat %f

As soon as you have done that, you will be able to run your Perl programs with the "Run Default Compiler" under the same menu or by pressing F8.

Programming tips

?Use chomps after reading lines

?Don't program the same task twice
?Avoid using many temporary variables

Report format tips

  • ?Don't forget answering the questions
  • Include only program modifications
  • Always give a test example
  • Submit one text file
  • Mention test results, answers and comments first and big programs later (in appendices)

Reading the whole file to a variable at one step.

Instead of reading a file line by line, you might want to read the whole file to a variable at one step. This is useful especially if you are reading html files. If you open a file and try reading from that file, perl reads only until the first [enter] character and stops. The reason of this behavior is that the default "input record separator" in perl is the [enter] character. This separator is defined in the special variable $/. By default $/ is equal to "\n". If you undefine this variable using undef, you can read the file at one step,
Example:
 

undef $/;
open(FILE, "data.htm");
$html = ;
close(FILE);

 

Regular expressions to search a variable:

I am assuming from now on that you are familiar with substitution operator in perl: s///. A basic example:
s/apple/orange/;

would replace the word "apple" with the word "orange". The separator "/" we used in this example can be replaced with any other non alpha-numeric character. The catch is; you have to escape the separator character inside your regular expression. So it is a better idea to use a less common character as a separator than "/". I prefer using "#" as a separator, because it is less common in strings and visually it is a good separator. So same regular expression could be written as:
 

s#apple#orange#;

A common mistake people do when using regular expressions is to try to match a variable in your regular expressions.

Example:
 

$data =~ s#$url#http://yahoo.com#;

This is going to work properly most of the time. But sometime it won't behave as expected or you will be experiencing occasional run time errors. For example, if your $url is equal to http://yahoo.com/do.cgi?action=go++&tell=poetry, the substitution operator is going to fail and exit with an error message.
 

"/http://yahoo.com/do.cgi?action=go++&tell=poetry/: nested *?+ in regex..."

The reason for the failure is that you can't use "++" inside your regular expression. You have to escape them. The variable might include several special variables, which have to be escaped properly. To correct way to implement this substitution is:
 

$temp = quotemeta($url);
$data =~ s#$temp#http://yahoo.com#;

 

quotemeta() is a standard perl function and it escapes all non-alphanumeric characters in your variable.

Using eval for clever substitutions:

If you used regular expressions in perl, you should have used substitution operator frequently. Most of the time a simple substitution is satisfactory.
Example:
 

$html =~ s#\bdogs\b#cats#ig;

In this example all the occurrences of the word "dogs" are replaced by the word "cats". What if we want to replace "dogs" with variable we calculated in our program rather than a fixed text.

Example:
 

$html =~ s#\bdogs\b#join(', ' , @animals)#ige;

In this example we used "e" switch, which enables us to use a result of an expression as a replacement.

"e" means: evaluate right side as an expression.

If you want to do more complicated replacement using a chunk of code, you might want to use eval function with curly brackets.

Example: In this example if the target of a link in an html page is "_top", then we replace that link with a link to http://yahoo.com.
 

$html =~ s#eval{
if($2 eq "_top"){
$string = qq{
};
}
else {
$string = qq{
};
}
$string
}
#iges;


Did You Know?

  • Perl is considered to be an adopted child of CGI since almost 85% of all CGI is done through Perl.




Premium Tutorials & Screencasts



Partners Links
Gabor Szabo Perl Trainer - Here you will find blog where author write about Test automation and also a Perl tutorials.