Gossamer Forum
Home : General : Perl Programming :

I'm learning perl and I have few question

Quote Reply
I'm learning perl and I have few question
Hi ! I'm learning perl (in a book) and I have some points that I don't understand.

- what is the differance btw

$Myvar = 'X';
and
my $Myvar = 'X';
or
Local $Myvar = 'X';
(What exactly does the my and local keyword ?)

I've tryed to write a little program. Can you please tell me why this program do not work when I call it from my browser? (I have chmod it 755 on my net server)

#!/usr/bin/perl -w
#The first script of my life !

my $Marie = 'Québec';
my $Maren = 'Germany';

# this is what I want to be print on screen

$Pays = "Marie live in $Marie and Maren live in $Maren\n";
print "$pays";


Thanks a lot for your help !
Quote Reply
Re: I'm learning perl and I have few question In reply to
my = private value within your script
local = similar, but different in the sense that local only operates on global variables versus unique variables.

The reason your script is not working is that you have two different cases for the variable: $pays and $Pays. They must be the same case setting to work, like the following:

Code:
#!/usr/bin/perl -w
#The first script of my life !

my $Marie = "Québec";
my $Maren = "Germany";

# this is what I want to be print on screen

$Pays = "Marie live in $Marie and Maren live in $Maren\n";
print "$Pays";

BTW: A better book for learning advanced codes of Perl is Advanced Perl Programming by O'Reilly.

Regards,



------------------
Eliot Lee
Anthro TECH,L.L.C
www.anthrotech.com
----------------------




Quote Reply
Re: I'm learning perl and I have few question In reply to
You also require:

print "Content-type: text/html\n\n";

before your first print statement.

Dan Smile


Quote Reply
Re: I'm learning perl and I have few question In reply to
Yep...Dan is correct...

So, the complete codes should look like the following:

Code:
#!/usr/bin/perl -w
#The first script of my life !

my $Marie = "Québec";
my $Maren = "Germany";

# this is what I want to be print on screen
$Pays = "Marie live in $Marie and Maren live in $Maren\n";
print "Content-type: text/html\n\n";
print "$Pays";

Hope this helps.

Regards,

------------------
Eliot Lee
Anthro TECH,L.L.C
www.anthrotech.com
----------------------




Quote Reply
Re: I'm learning perl and I have few question In reply to
You almost never need to use "local" . So us always "my" .
local is used to change a global variable temporarily.

------------------
Turk Scripts
http://turkiyem.com/turkscripts
Quote Reply
Re: I'm learning perl and I have few question In reply to
Thanks a lot guys !

:-)
I really appreciate your help !
Quote Reply
Re: I'm learning perl and I have few question In reply to
I've learned alot by just reading from this forum. I now understand what the code you guys are talking about. I want to know something more what if I want the value for

my $Marie = "Québec";
my $Maren = "Germany";

is coming from user input in a html form. and I also want to save it into a database(flat file)? do we need to use | (delimeter) to seperate these variable input?

forgive me I dont know really the concept on how to call cgi/pl program from server or inputs from html form.

thanks
Quote Reply
Re: I'm learning perl and I have few question In reply to
No...my is something that you assign to variables and arrays in your script. The values do not come from a form.

If you want to print fields into a database, you have to use one of the following variables:

Code:
$in{'field'}

OR

Code:
$fields{'field'}

depending on what type of parse_form codes you use.

Regards,

------------------
Eliot Lee
Anthro TECH,L.L.C
www.anthrotech.com
* Be sure to visit the Resource Center for FAQ's, Modifications and Extra Goodies!!
* Search Forums!
* Say NO to Duplicate Threads. :)
----------------------








Quote Reply
Re: I'm learning perl and I have few question In reply to
I tell you what (not trying to be rude here), but you'll learn a heck of a lot by reading what others have already written, we went to cgi-resources.com about oh I don't know maybe 2 years ago, and started to download scripts to use on our websites, and over the two years, we have learned quite a lot, the book learning is great, but real world cgi scripts are also a great way to learn, I look at the different scripts that we run (and now modify them too), and I try and figure out what something in them does, I still have problems figuring out what everything means, but I methodically read through them and corrolate what is happeing on my screen compared to what is written in the script, in this way I can see what something means in a script and see where to midify scripts and how to rewrite them.

So, delve on into some scripts, and see what they do and how they do it. it's a great way to learn.

Cheers
Vis
Quote Reply
Re: I'm learning perl and I have few question In reply to
Thanks for the telling me Visionary! This forum is just another source of what I actually do in trying to learn cgi/perl stuff.

Regards!