Newbie queries

Discussion in 'Perl Misc' started by P.R.Brady, Jul 5, 2004.

  1. P.R.Brady

    P.R.Brady Guest

    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
     
    P.R.Brady, Jul 5, 2004
    #1
    1. Advertising

  2. P.R.Brady

    John Bokma Guest

    P.R.Brady wrote:

    > 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.pl

    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

    --
    John MexIT: http://johnbokma.com/mexit/
    personal page: http://johnbokma.com/
    Experienced Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
     
    John Bokma, Jul 5, 2004
    #2
    1. Advertising

  3. P.R.Brady

    Chris Cole Guest

    On Mon, 05 Jul 2004 15:58:11 +0100, P.R.Brady wrote:

    > 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.
     
    Chris Cole, Jul 5, 2004
    #3
  4. P.R.Brady

    John Bokma Guest

    John Bokma wrote:

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

    >
    > 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.

    --
    John MexIT: http://johnbokma.com/mexit/
    personal page: http://johnbokma.com/
    Experienced Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
     
    John Bokma, Jul 5, 2004
    #4
  5. P.R.Brady

    John Bokma Guest

    John Bokma wrote:

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

    >>
    >>
    >> 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.


    Instead of a global var that is.

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

    --
    John MexIT: http://johnbokma.com/mexit/
    personal page: http://johnbokma.com/
    Experienced Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
     
    John Bokma, Jul 5, 2004
    #5
  6. P.R.Brady wrote:
    > 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
     
    Jürgen Exner, Jul 5, 2004
    #6
  7. P.R.Brady <> wrote:


    > 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

    grep <something> faq.headlines


    > 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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jul 5, 2004
    #7
  8. P.R.Brady

    John Bokma Guest

    John Bokma, Jul 5, 2004
    #8
  9. P.R.Brady

    Paul Lalli Guest

    On Mon, 5 Jul 2004, Jürgen Exner wrote:

    > P.R.Brady wrote:
    >
    > 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


    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
     
    Paul Lalli, Jul 5, 2004
    #9
  10. John Bokma <> wrote:
    > Jürgen Exner wrote:
    >
    >>>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

    >
    > 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...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jul 5, 2004
    #10
  11. P.R.Brady

    John Bokma Guest

    Tad McClellan wrote:

    > John Bokma <> wrote:
    >
    >>Jürgen Exner wrote:
    >>
    >>>>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

    >>
    >>But needed if you are using Apache webserver

    >
    > 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.

    --
    John MexIT: http://johnbokma.com/mexit/
    personal page: http://johnbokma.com/
    Experienced Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
     
    John Bokma, Jul 5, 2004
    #11
  12. P.R.Brady

    John Bokma Guest

    Tad McClellan wrote:

    > John Bokma <> wrote:
    >
    >>Jürgen Exner wrote:
    >>
    >>
    >>>>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

    >>
    >>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...


    "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.

    --
    John MexIT: http://johnbokma.com/mexit/
    personal page: http://johnbokma.com/
    Experienced Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
     
    John Bokma, Jul 5, 2004
    #12
  13. P.R.Brady

    Matt Garrish Guest

    "John Bokma" <> wrote in message
    news:40e9bebb$0$202$...
    > Tad McClellan wrote:
    >
    > > John Bokma <> wrote:
    > >
    > >>Jürgen Exner wrote:
    > >>
    > >>>>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
    > >>
    > >>But needed if you are using Apache webserver

    > >
    > > 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.
    >


    So change the default:

    http://httpd.apache.org/docs-2.0/mod/core.html#scriptinterpretersource

    Matt
     
    Matt Garrish, Jul 6, 2004
    #13
  14. P.R.Brady

    John Bokma Guest

    Matt Garrish wrote:

    > "John Bokma" <> wrote in message
    > news:40e9bebb$0$202$...
    >
    >>Tad McClellan wrote:
    >>
    >>>John Bokma <> wrote:
    >>>
    >>>
    >>>>Jürgen Exner wrote:
    >>>>
    >>>>
    >>>>>>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
    >>>>
    >>>>But needed if you are using Apache webserver
    >>>
    >>>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.

    >
    > So change the default:
    >
    > http://httpd.apache.org/docs-2.0/mod/core.html#scriptinterpretersource


    Thanks, although the security warning sounds like fun :-(

    --
    John MexIT: http://johnbokma.com/mexit/
    personal page: http://johnbokma.com/
    Experienced Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
     
    John Bokma, Jul 6, 2004
    #14
  15. P.R.Brady

    Matt Garrish Guest

    "John Bokma" <> wrote in message
    news:40e9f69d$0$863$...
    > Matt Garrish wrote:
    >
    > > "John Bokma" <> wrote in message
    > > news:40e9bebb$0$202$...
    > >
    > >>
    > >>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.

    > >
    > > So change the default:
    > >
    > > http://httpd.apache.org/docs-2.0/mod/core.html#scriptinterpretersource

    >
    > Thanks, although the security warning sounds like fun :-(
    >


    If anyone can miss that warning, Purl Gurl can... : )

    Matt
     
    Matt Garrish, Jul 6, 2004
    #15
  16. P.R.Brady

    P.R.Brady Guest

    P.R.Brady wrote:
    > 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
     
    P.R.Brady, Jul 6, 2004
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    2,344
  2. laksh
    Replies:
    1
    Views:
    460
    Dennis Lee Bieber
    Jul 20, 2005
  3. Ian Roddis

    xslt queries in xml to SQL queries

    Ian Roddis, Feb 26, 2006, in forum: Python
    Replies:
    3
    Views:
    1,514
    Crutcher
    Feb 26, 2006
  4. Mark Rae
    Replies:
    6
    Views:
    340
    sloan
    Feb 15, 2007
  5. Abby Lee

    so many queries within queries I'm confused

    Abby Lee, Aug 4, 2004, in forum: ASP General
    Replies:
    11
    Views:
    356
    Aaron [SQL Server MVP]
    Aug 6, 2004
Loading...

Share This Page