Newbie queries

P

P.R.Brady

I just can't find answers in the FAQ. It tells you how to do clever
things but I just can't find the simple things.

Q1: Is there an index to the FAQ? If not , how do you find your way
round it?

Q2: How do I exit gracefully from a program? Die is a crash close, but
is there a 'clean' finish, other than dribbling down to the end of the
source file?

Q3: I start my scripts typically with 'perl -w myprog.pl'.
How do I supply run time parameters to my program? What puts them in
@ARGV? A possibly related question - why am I entreated to put the
first line of my script as:
#!c:\perl\perl.exe -w (I'm running under windoze)?

Q4: How do I declare global variables so that 'use strict;' is satisfied?

Regards
Phil
 
J

John Bokma

P.R.Brady said:
I just can't find answers in the FAQ. It tells you how to do clever
things but I just can't find the simple things.

Q1: Is there an index to the FAQ? If not , how do you find your way
round it?

perldoc perldoc (on the command line)
Q2: How do I exit gracefully from a program? Die is a crash close, but
is there a 'clean' finish, other than dribbling down to the end of the
source file?

perldoc -f exit
Q3: I start my scripts typically with 'perl -w myprog.pl'.
How do I supply run time parameters to my program? What puts them in
@ARGV? A possibly related question - why am I entreated to put the
first line of my script as:
#!c:\perl\perl.exe -w (I'm running under windoze)?

Not needed on Windows since .pl is probably "connected" with perl.exe,
ie. script.pl in cmd.exe should perl.exe the script.

use warnings;

instead of the -w switch

So on windows your first lines could look like:

use strict;
use warnings;

print "Hello, world!\n";
exit;

running it:
Hello, world!

parameters, just add them:

script.pl par1 par2 par3 ... parn
Q4: How do I declare global variables so that 'use strict;' is satisfied?

perldoc -f our
perldoc -f my
 
C

Chris Cole

I just can't find answers in the FAQ. It tells you how to do clever
things but I just can't find the simple things.

Q1: Is there an index to the FAQ? If not , how do you find your way
round it?

Have you tried searching? Either the /term/ construct in perldoc or
whatever browser/reader's specific search tool.
Q2: How do I exit gracefully from a program? Die is a crash close, but
is there a 'clean' finish, other than dribbling down to the end of the
source file?
exit;

Q3: I start my scripts typically with 'perl -w myprog.pl'.
How do I supply run time parameters to my program? What puts them in
@ARGV? A possibly related question - why am I entreated to put the
first line of my script as:
#!c:\perl\perl.exe -w (I'm running under windoze)?

Run-time parameters are passed as a space-delimited list and place in
@ARGV (not sure by what). What you do with them in your script is up to
you. See Getopt::Long module for some ideas.

I don't do perl on windoze, so I can't answer the rest as in linux you can
do either 'perl -w myprog.pl' without a shebang (#!) line or just run
'./myprog.pl' with.
Q4: How do I declare global variables so that 'use strict;' is satisfied?

Declare 'my $var;' outside of any block and it will be available globally
e.g.:

#! perl

use strict;
use warnings;

my $var = 1;

while (<>) {

if ($var) {
do stuff...;
exit;
}
}
exit;
Regards
Phil

HTH
Chris.
 
J

John Bokma

John said:
perldoc -f our
perldoc -f my

Just a side note, global *variables* are often a bad idea. You can use
global constants with for example:

use constant PI => 3.14;

read the warnings that come with this pragma.

Personally I use either a var I pass around, or better, I use a class.
 
J

John Bokma

John said:
Just a side note, global *variables* are often a bad idea. You can use
global constants with for example:

use constant PI => 3.14;

read the warnings that come with this pragma.

Personally I use either a var I pass around, or better, I use a class.

Instead of a global var that is.

Uhm... I try to quit on the Purl Gurl :-D
 
J

Jürgen Exner

P.R.Brady said:
I just can't find answers in the FAQ. It tells you how to do clever
things but I just can't find the simple things.

Q1: Is there an index to the FAQ? If not , how do you find your way
round it?

perldoc perlfaq
Q2: How do I exit gracefully from a program? Die is a crash close,
but is there a 'clean' finish, other than dribbling down to the end
of the source file?

perldoc -f exit
Q3: I start my scripts typically with 'perl -w myprog.pl'.

It is easier to use
use warnings;
instead of "-w"
How do I supply run time parameters to my program?

Just type them .....
What puts them in @ARGV?

Your command shell (even if it's just DOS) and the Perl interpreter
A possibly related question - why am I entreated to put the
first line of my script as:
#!c:\perl\perl.exe -w (I'm running under windoze)?

Will simply be ignored by DOS but doesn't cause any harm either
Q4: How do I declare global variables so that 'use strict;' is
satisfied?

You use "my $foo;" do declare the scalar $foo. Don't remember in which Fine
Manual this is described, though.

jue
 
T

Tad McClellan

Q1: Is there an index to the FAQ?


perldoc perlfaq

(You didn't think to try that already?)

If not , how do you find your way
round it?


I use the "-q" switch to "perldoc", or:

1) find out where the raw POD files got installed:

perldoc -l perlfaq

2) make my own index:

cd <directory from step #1>

grep ^= perlfaq[1-9].pod >faq.headlines

3) search the index

Q2: How do I exit gracefully from a program?
^^^^

That is a SAQ (Self Answering Question):

perldoc -f exit

Die is a crash close, but
is there a 'clean' finish,


Did you read the documentation for the function you are using?

perldoc -f die

"See also exit(), warn(), and the Carp module."

Q3: I start my scripts typically with 'perl -w myprog.pl'.
How do I supply run time parameters to my program?


Just type them in at the end of the command line that you already have.

perl -w myprog.pl arg0 arg1 ...

What puts them in
@ARGV?


perl's start-up code does that for you before running the code
that you have given it.

A possibly related question - why am I entreated to put the
first line of my script as:
#!c:\perl\perl.exe -w (I'm running under windoze)?


To enable warnings (the old way).

A more modern way is to put this near the top instead:

use warnings;

You can read all about it with:

perldoc warnings

Q4: How do I declare global variables so that 'use strict;' is satisfied?

"Coping with Scoping":

http://perl.plover.com/FAQs/Namespaces.html
 
P

Paul Lalli

P.R.Brady wrote:

Your command shell (even if it's just DOS) and the Perl interpreter



Will simply be ignored by DOS but doesn't cause any harm either

Not quite. The line isn't ignored entirely. The Perl interpreter will
obey any switches, such as -w in this example, that are listed after the
shebang, even though the shebang isn't especially necessary when running
under Windows.

Paul Lalli
 
T

Tad McClellan

John Bokma said:
But needed if you are using Apache webserver


But nobody (other than you) said anything about the web.

In fact, the OP was asking about the command line, implying that
there is no CGI involved here...
 
J

John Bokma

Tad said:
But nobody (other than you) said anything about the web.

I added it as a reminder, one day that command line tool will be turned
into a neat CGI thingy ;-)
In fact, the OP was asking about the command line, implying that
there is no CGI involved here...

I am not here for the OP alone. I stumbled into this quite recently, I
thought that even Apache would use the binding between perl.exe and the
..pl extension. Not so.

Also the "ignored" statement was incorrect, but someone else explained that.
 
J

John Bokma

Tad said:
But nobody (other than you) said anything about the web.

In fact, the OP was asking about the command line, implying that
there is no CGI involved here...

"Q3: I start my scripts *typically* with 'perl -w myprog.pl'.
How do I supply run time parameters to my program? What puts them in
@ARGV? A possibly *related* question - why am I entreated to put
the first line of my script as:
#!c:\perl\perl.exe -w (I'm running under *windoze*)?
"

So, when you run it under *windoze*, using Apache, you need the she-bang.
 
P

P.R.Brady

P.R.Brady said:
I just can't find answers in the FAQ. It tells you how to do clever
things but I just can't find the simple things.

Q1: Is there an index to the FAQ? If not , how do you find your way
round it?

Q2: How do I exit gracefully from a program? Die is a crash close, but
is there a 'clean' finish, other than dribbling down to the end of the
source file?

Q3: I start my scripts typically with 'perl -w myprog.pl'.
How do I supply run time parameters to my program? What puts them in
@ARGV? A possibly related question - why am I entreated to put the
first line of my script as:
#!c:\perl\perl.exe -w (I'm running under windoze)?

Q4: How do I declare global variables so that 'use strict;' is satisfied?

Regards
Phil

Thanks for the helpful replies everyone.
Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top