Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

learn perl 1

Status
Not open for further replies.

WebGoat

MIS
Jul 16, 2005
85
US
i want to learn perl.9

what compiler is needed to compile and run perl ?

if i want to run and compile java proram i need jdk. so how do i compile and run perl in linux redhat
 
Perl is an interpreted language, so the compilation is done at run-time (i.e. you don't compile a perl file into an executable and then run the executable - you pass your Perl script to the perl interpreter and it compiles and runs it in one step).

The perl interpreter is probably already installed on your Red Hat system. Type ``which perl'' on the command line, and you should get ``/usr/bin/perl'' in return (the path to the perl interpreter).

As an example of writing and running a perl program - open a text editor and create the following file (call it `test.pl'):
Code:
#!/usr/bin/perl -w
use strict;
print "Hello World!\n";

Now, on the command line, type ``perl test.pl'', and it should print ``Hello World'' to the screen.
 
just learn about how to run perl. and i tested a program to run .it worksin my system.

but what is

use strict;
you have usued ?
 
You need to read some books on perl and/or look at some online material about perl.
The use strict; directive is not necessary but is recommended for all perl scripts as a facility to reduce common errors and mistakes that you might create in your scripts.
You can run perl scripts without it but there are very few reasons for not using it and since you appear to be starting to learn perl I would strongly suggest you use strict; in all your scripts.
You will also notice the -w on the end of the #!/usr/bin/perl line. That instructs perl to offer you warnings about things that would actually compile but might not be what you intended or wanted. I would recommend you always run with -w for the same reasons as I recommend you use strict;.



Trojan.
 
Exactly - that's why I included them, as it's a great habit to get into from the start, even though many beginners' tutorials don't use them.

[offtopic]
Congrats on the TipMaster thing too Trojan.
[/offtopic]
 
LOL!
Thanks Isnid.
Always nice to see your posts which are, without exception, accurate, informative and reliable.
:)


Trojan.
 
but they are suppresingg errors.

using strict and -w i got error
Code:
Global symbol "$d" requires explicit package name at test line 9.
Global symbol "$e" requires explicit package name at test line 10.
Global symbol "$f" requires explicit package name at test line 11.
Global symbol "$d" requires explicit package name at test line 11.
Global symbol "$e" requires explicit package name at test line 11.
Global symbol "$f" requires explicit package name at test line 13.
Execution of test aborted due to compilation errors.



without use strict but with -w i got error

Code:
Argument "string" isn't numeric in addition (+) at test line 10.


did you see, they are differeing very much . the error messages are different . in fact, without use strict and -w was a good choice.
 
>without use strict and -w was a good choice.
because of its readibility of error messages. it directly specifies the error in a simpler way.
 
ohh , forgot to post the code.

i did intentonally wrong code to test -w and use strict

here is the code

Code:
#!/usr/local/bin/perl -w
use strict;
#@food  = ("apples", "pears", "eels");
#$l=push(@food, "eggs", "lard");
#($a, $b) = @food;
#$f ="@food";
#print $b;
#($a, $b) = ($c, $d);
$d='009';
$e='string';
$f=$d+$e;
#print @food."";
print $f;
 
Try this:
Code:
#!/usr/local/bin/perl -w
use strict;
use diagnostics;
#@food  = ("apples", "pears", "eels");
#$l=push(@food, "eggs", "lard");
#($a, $b) = @food;
#$f ="@food";
#print $b;
#($a, $b) = ($c, $d);
my $d='009';
my $e='string';
my $f=$d+$e;
#print @food."";
print $f;

You'll see that I have added my in the appropriate places and I've also added use diagnostics; which is great for beginners because it explains any errors in great detail.



Trojan.
 
Argument "string" isn't numeric in addition (+) at test line 12 (#1)
(W numeric) The indicated string was fed as an argument to an operator
that expected a numeric value instead. If you're fortunate the message
will identify which operator was so unfortunate.

9

excellent !! and excellent !

this error is really self-explanarotory. any body can understand this error message and find out the place where this error has occured.

so, i have decided to use -w, use strict, use diagonstics and ALL MY VARIABLES would be prefixed by the my keyword.

its a very powerful thing.

thanks
 
but the program outputs 9.
tha was a unfortunate thing [mad]
 
I'm glad that you're a convert! :)
What did you expect your program to output?
You're using a math operator to add strings. To join strings you should use the dot operator like this:
Code:
my $f=$d . $e;


Trojan.
 
>What did you expect your program to output?
i want the program should not print the output at all. you know C,C++,JAVA all programs behaves dont show output once there is some compile erorr.

this language is somewhat exceptional

 
ok, NO problem . let it output whatever it wants. i must stop this post . i am happy whatever i have learnt . thanks
 
Yep, it is.
These issues are not fatal exceptions in perl. This is why you should always test your scripts with "perl -c" and clean up any warnings and errors it suggests.
"perl -c script.pl" will test compile the code but not run it so it's a useful tool to see if you have clean code.


Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top