Closed Thread Icon

Preserved Topic: Learning to program (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=17929" title="Pages that link to Preserved Topic: Learning to program (Page 1 of 1)" rel="nofollow" >Preserved Topic: Learning to program <span class="small">(Page 1 of 1)</span>\

 
Boudga
Maniac (V) Mad Scientist

From: Jacks raging bile duct....
Insane since: Mar 2000

posted posted 04-10-2001 22:47

I really would like to learn how to program in Perl but I have no idea where to start. All the books I've bought on it assume you already know how to program in some other language. Do I need to learn another language before I try to learn Perl? Has anyone here learned Perl as their first programming language and if so how did you learn it?

Please help!

jiblet
Paranoid (IV) Inmate

From: Minneapolis, MN, USA
Insane since: May 2000

posted posted 04-10-2001 23:36

When you try to read these books what do you get hung up on? I have never done a lot of programming, but then, I learned little bits from the age of 14 on up, so it all seems fairly straightforward to me. If you are totally lost, just search the net for a primer of some sort, I don't imagine it would take that much to get into an average Perl book. If you are just having trouble with some specific concept, post here! I'd love to be able to answer a question instead of just asking all the time

Boudga
Maniac (V) Mad Scientist

From: Jacks raging bile duct....
Insane since: Mar 2000

posted posted 04-10-2001 23:42

I've learned what certain things do in Perl but I have no understanding of how I could put these things together to make something functional. Like for instance I would like to be able to write to a text file from a web page submission and be able to query the text file. I have no idea where to start on something like that.

jiblet
Paranoid (IV) Inmate

From: Minneapolis, MN, USA
Insane since: May 2000

posted posted 04-10-2001 23:59

Unfortunately I have no experience writing perl code, I've done some installations of scripts and edited it a bit. The first thing is to have a place to test out your stuff. I'm assuming you have some sort of shell account with cgi access. Your script needs to go in the cgi-bin directory and have the file permissions set appropriately , where it can then be accessed via a web browser by
http://www.yourdomain.com/cgi-bin/filename.pl

You would have that URL be the action for your form. Then in your perl script, the values will be passed into variables that have the same names as the names of the respective fields (At least that's how it works in PHP, I guess it could be different in Perl). To output a file there should be a description of how to work with files in your book. Generally there is some command to open up a file for either reading, writing or appending, and several file manipulation commands to go with it. This is supposed to be Perl's forte, so I'm sure your book goes into detail.

I know this isn't super helpful in terms of accurate details, but my favorite way to figure out the minutae is to learn by example. Go to Matt's Script Archive at http://www.worldwidemart.com/scripts/ and install some of those scripts so you can see exactly how they work.

Boudga
Maniac (V) Mad Scientist

From: Jacks raging bile duct....
Insane since: Mar 2000

posted posted 04-11-2001 00:03

I think I might be as far along as you....

I have a IIS server running Active State Perl and have hacked a few Perl scripts to make them work for myself. It's the creation from scratch of anything that boggles me.

jiblet
Paranoid (IV) Inmate

From: Minneapolis, MN, USA
Insane since: May 2000

posted posted 04-11-2001 00:11

What about gutting an existing perl script and filling in your own commands?

if u know how to:

read input from a form
write output to a file
read input from a file
& display output to a browser

then what else is there? Is the syntax confusing?

Boudga
Maniac (V) Mad Scientist

From: Jacks raging bile duct....
Insane since: Mar 2000

posted posted 04-11-2001 00:13

yeah I don't understand a lot of the syntax....like when to use this versus that etc...

Boudga
Maniac (V) Mad Scientist

From: Jacks raging bile duct....
Insane since: Mar 2000

posted posted 04-11-2001 00:16

one thing in particular that I would love to create right now is a database based on a web page I run at work that lists the DVD movies owned by about a dozen people.

Then I would like to be able to write to the DB and query the DB using Perl. Where to start...hmmm?!?!?!?!

hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana, USA
Insane since: Aug 2000

posted posted 04-11-2001 03:48

Boudga: First: What platform are you running on? Second: What database are you using? Third: What version of Perl are you using?

We need to answer these questions before I can give you any specific advice on how to connect Perl to a batabase.

If your on Linux or some other UNIX look-a-like use CPAN to download the Perl driver for the database you want to use.

If your on Windows it's a bit harder.

I have been programming databases using Perl for an ISP for the last year and a half so I may be able to help you once we figure out some specific questions.

Note: I have been programming in various languages for the last thirty years and personally don't consider Perl a good place to start learning to program.




[This message has been edited by hyperbole (edited 04-11-2001).]

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 04-11-2001 06:20

Perl was my first language and regretably I have cease using it. But it does serve a great purpose. I learned of off a website, that was back in the days where I had no money, my parents didn't know what programming was, and I thought I could learn anything off of the web, I learned HTML didn't I... God, have to laugh at yourself for the being so naive, thinking HTML was a programming language.

But I learned from a nice site.

_:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::_
http://www.wdvl.com/Authoring/Languages/Perl/PerlfortheWeb/toc.html

_:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::_

That site will always have a place in my heart, it brought me into the world of programming. I learned well from that site, a lot of the information will be too much to handle, but just go through it, you may not even understand a lot of it, but if you jump in with two feet (metal chains attached to you), and just keep plowing through you will hit the bottom and begin to crawl out, your chains with then quickly fall appart and you will reach the other side, being born again with new found knowledge.

Programming will be hard until you learn it. It is not as awful as you think it is, just keep telling yourself that the entire time you do it. Don't think that oh "Hello World" that is so useless could I simple type <h1>Hello World</h1> and be done with it. Well of course you can, but that is just the first step. Take you time with it, but make sure to jump in, don't just put your toe in and say "Damn the water is cold." The water is fucking nice, you just have to get out to the first enbankment, once you are there you will notice that the water is 86 degrees farenheit, and there are hundreds of really cool people out there that are willing to help you every step of the way.

I wish you good luck! Have fun! It will be the best and the worst, you will curse yourself sometimes, but you will never be able to give it up once you are there.

-mage-

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 04-11-2001 08:17

Boudga, since you're using ActivePerl, you can use PPM to install necessary perl modules for connecting to MySQL db server. Just open up command prompt and type "C:>ppm" [enter]. Now, type "PPM>install DBI" [enter] and "PPM>install DBD-Mysql" (you must be online when doing this). If everything went well (you can check this by typing "PPM>query" [enter], and you should see "DBI" and "DBD-Mysql" on the list). Now you have everything that is needed for connecting to MySQL db server. A small intro how to use this can be found here: http://www.ozoneasylum.com/Forum12/HTML/000091.html

Boudga
Maniac (V) Mad Scientist

From: Jacks raging bile duct....
Insane since: Mar 2000

posted posted 04-11-2001 15:20

hyperbole: Windows NT 4.0, no DB created yet, Active State Perl 522

Let's say I wanted to connect to an access database....or maybe a text file database

linear
Paranoid (IV) Inmate

From: other places
Insane since: Mar 2001

posted posted 04-11-2001 16:33

I recommend the O'Reilly books: _Learning Perl_, called the llama book, and _Programming Perl_, called the camel book. When you think you're comfortable with those, _Effective Perl Programming_ (Addison-Wesley) is a must. If "see one do one" is more your learning style, the _Perl Cookbook_ is a winner. Chapter 14 of the cookbook is all about database access.

Text files are the easiest. Perl gives you the <> operator to make all the work disappear:

open (MYFILE, 'c:\data\mydata.txt') or die $!;
while (<MYFILE> ) { ## opens your file and reads one line at a time into $_
## do something here: $_ contains the current line of the file
}

dbm files are next easiest, and *fast* if your system supports them. If you have more than 10k lines in your file, consider dbm for performance rasons.

dbmopen (%myhash, ''c:\data\mydata') or die $!;
while (($key, $val) = each %myhash) {
## do something here: $key is the current key and $val the current value
}

If you need the features of a relational database, I echo the suggestion to use DBI. (Except possibly for Sybase, but that's another story....)

hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana, USA
Insane since: Aug 2000

posted posted 04-11-2001 17:24

Boudga: There are two modules that may be of use to you: DBD-ODBC and DBI.

DBI is a general purpose package for accessing databases and therefore is harder to use than DBD-ODBC. I have not used DBD-ODBC and don't know how complete it is, but it probably is the module you want to install. While I have used DBI on Linux, I use sybperl most of the time to access our databases since they are all Sybase.

The second part of your question was about creating a "text database". By that I assume you mean a database in a flat file. That is probably the easiest way to start creating a datbase, but then you end up doing all the database operations in your perl program.

You can create a flat file using a text editor like notepad. When you create the file make each line a record and separate the fields with some delimiter. There are several ways to do this. Each field could be separated by a comma, or you can make each field a fixed width (probably harder to enter). If you create the information as columns in Excel you can then have Excel create a comma separtated file which you can read into perl.

Why don't I turn the conversation back over to you at this point so you can ask some more questions about what I just said.

Boudga
Maniac (V) Mad Scientist

From: Jacks raging bile duct....
Insane since: Mar 2000

posted posted 04-11-2001 17:34

I knew about how to create a comma delimited file...

lets say i create this kind of file. I've got a dozen names of people who own a combined total of 300 DVD's. Would I create the file like this:

1 boudga, movie name, movie name, movie name, movie name,
2 sarah, movie name, movie name, movie name, movie name, movie name, movie name,
3 jimmy, movie name, movie name, movie name, movie name, movie name,
4 roger, movie name, movie name, movie name, movie name,
etc.,

or like this:

1 boudga, movie name
2 boudga, movie name
3 sarah, movie name
4 roger, movie name
etc.,

so more that 10k will cause slow downs in db queries?

hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana, USA
Insane since: Aug 2000

posted posted 04-11-2001 20:41

Either way would be appropriate. It depends on which is easier for you to create and to a lesser extend how you will use the data once you get it into the program.

For example: with the second format you could read the file into a perl script as follows:

code:
open(INPUT_FILE, "< filename");
@list = <INPUT_FILE>;
close(INPUT_FILE);



This puts the file into an array called @list. It will contain one line of the file in each element of the array.

now you can create a hash of the database using the following:

code:
foreach $line (@list)
{
($name, $movie) = split(/,/, $line);

$DVDs{$name} .= ", " if (length($DVDs{$name}) == 0);
$DVDs{$name} .= $movie;
}



This loop will place each line from @list into $line, one at a time. It checks to see if the hash $DVDs already has a movei listed for the person and if so concatenates a "," to the end of the list then it adds the movie to the list for that person.

When you are finished with this loop, you will have a list (called a hash) with the movies owned by each person in your database.

You can print this list by saying

code:
foreach $name (sort keys %DVDs)
{
printf("%15s: %s\n", $name, $DVDs{$name};
}



One of the most confusing things about Perl is that a variable changes its prefix depending on how you are accessing the data it contains. For example, to refer to the entire has in DVDs we call it %DVDs (as in the for loop above), but to refer to the individual elemnts of the hash we refer to $DVDs{$name}. This will also happen with arrays.

Boudga
Maniac (V) Mad Scientist

From: Jacks raging bile duct....
Insane since: Mar 2000

posted posted 04-11-2001 20:45

whoa, right over my head...I think I might have understood 25% of that post Hyperbole

linear
Paranoid (IV) Inmate

From: other places
Insane since: Mar 2001

posted posted 04-11-2001 20:47

hyperbole >>One of the most confusing things about Perl is that a variable changes its prefix depending on how you are accessing the data it contains.

One of the most confusing things about other languages is that they *don't*.

hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana, USA
Insane since: Aug 2000

posted posted 04-11-2001 20:56

I agree liner.

OK Bougda. Tell me what you do understand and ask me some questions. This is a place to start learning.

Boudga
Maniac (V) Mad Scientist

From: Jacks raging bile duct....
Insane since: Mar 2000

posted posted 04-11-2001 21:23

on the print part of your directions...do you mean print to the DB or print out the results of a query?

Boudga
Maniac (V) Mad Scientist

From: Jacks raging bile duct....
Insane since: Mar 2000

posted posted 04-11-2001 21:24

I'm going to attempt to put this into action...bare with me on this ok?

Boudga
Maniac (V) Mad Scientist

From: Jacks raging bile duct....
Insane since: Mar 2000

posted posted 04-11-2001 21:26

hyperbole...what's your ICQ uin? I'd like to talk to you privately about something that is OT..

hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana, USA
Insane since: Aug 2000

posted posted 04-11-2001 21:29

ICQ #95202104

hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana, USA
Insane since: Aug 2000

posted posted 04-11-2001 21:50

The unadorned printf in the third code segment will print to the screen (standard out).

We would need to do something like

code:
open(OUTPUT, "> filename")
printf(OUTPUT, ....



to print to a file.

We are not really dealing with a Data Base in the Access, Sybase, MySQL, Oracle sense of database. We are just calling our flat file a database.

« BackwardsOnwards »

Show Forum Drop Down Menu