Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Printing POD info to terminal window without exiting

by cr8josh (Sexton)
on Aug 04, 2021 at 22:18 UTC ( [id://11135611]=perlquestion: print w/replies, xml ) Need Help??

cr8josh has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm new to POD documentation, and am using GetOpt::Long and "HelpMessage" to give usage info to a user. My problem is that HelpMessage exits my script. I want to be able to print my POD info to the terminal window, and not exit my script. I looked into Pod2Usage as well but it also exits.

For example, if the user enters --help, the script shows help/usage info but then continues running.

Many thanks in advance.

EDIT: Answered my own question. It turns out that Pod2Usage won't exit if you call it with

pod2usage(-exitval => 'NOEXIT');

And HelpMessage works with the same invocation:

HelpMessage(-exitval => 'NOEXIT');

My reading of the docs ("pod2usage($exitstatus))" was that the following should work as well, but it doesn't, which caused my confusion:

pod2usage('NOEXIT')

  • Comment on Printing POD info to terminal window without exiting

Replies are listed 'Best First'.
Re: Printing POD info to terminal window without exiting
by GrandFather (Saint) on Aug 05, 2021 at 02:44 UTC

    Note that for a command line script/app expected behavior is that the app/script exits after dumping help given --help on the command line. Maybe you would be better to use something like --hints instead where you want to dump the help and continue?

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re: Printing POD info to terminal window without exiting
by davido (Cardinal) on Aug 05, 2021 at 15:04 UTC

    You already answered your own question by following up to state that -exitval => 'NOEXIT' works. I had found this to work just fine, as well. But it wouldn't be Perl if it didn't arouse curiosity. I was curious if I could override CORE::GLOBAL::exit effectively. Turns out I can (though this isn't to say I should):

    #!/usr/bin/env perl use strict; use warnings; our $override_exit = 0; BEGIN { no warnings 'redefine'; *CORE::GLOBAL::exit = sub :prototype(;$) { if ($override_exit) { warn "EXIT_OVERRIDE\n"; } else { CORE::exit($_[0] // 0); } }; } use Pod::Usage; { local $override_exit = 1; print "Calling pod2usage(1)\n"; pod2usage(1); print "We are still here after overriding exit.\n"; } print "calling pod2usage with -exitval => 'NOEXIT'\n"; pod2usage(-exitval => 'NOEXIT'); print "We are still here after using -exitval => 'NOEXIT'.\n";

    This produces the following output:

    Calling pod2usage(1) EXIT_OVERRIDE We are still here after overriding exit. calling pod2usage with -exitval => 'NOEXIT' We are still here after using -exitval => 'NOEXIT'.

    So this demonstrates that NOEXIT works, and that overriding CORE::GLOBAL::exit can work.


    Dave

Re: Printing POD info to terminal window without exiting
by perlfan (Vicar) on Aug 08, 2021 at 02:20 UTC
    Given that you should exit when this occurs, I tend do use effective, system(qq{perldoc $0});. Of course on debian based systems perldoc is not installed by default (unfortunately); so a check for perldoc may be prudent.
      system(qq{perldoc $0});

      Sorry, but no. I have much respect for TIMTOWTDI, but given that Pod::Usage has been in the Perl core for over 20 years and the question was already solved in the OP, then a solution that calls an external program that may not be installed, is brittle in other ways (PATH and $0), doesn't do the same thing as the OP is asking, and introduces a potential security vulnerability ($0 can be assigned to), is the Wrong Way.

        Explain to me your objection. Pod::Usage seems to rely on underlying "formatters", including perldoc. Does your object rely simply on the hypothetical that $0 can be reassigned or that there exist linux distros that include perl but not perldoc by default, like Debian?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11135611]
Approved by GrandFather
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-25 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found