Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

PIPE less

by Anonymous Monk
on Apr 16, 2010 at 09:13 UTC ( [id://835050]=perlquestion: print w/replies, xml ) Need Help??

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

HI,

How can i use the unix "less" command as in the below example in perl
echo "hi"|less
Actually i want to use this for --help in my script, for example perl myScript --help
prints the usage in the "less" mode
Thanks in advance!!!

Replies are listed 'Best First'.
Re: PIPE less
by choroba (Cardinal) on Apr 16, 2010 at 09:19 UTC
Re: PIPE less
by cdarke (Prior) on Apr 16, 2010 at 10:14 UTC
    With the minimum of changes to existing code:
    open (my $less, "|less") or die "No less: $!\n"; my $OldHandle = select $less; print "help text\n"; # will go to less close ($less); # Don't forget this! select $OldHandle;
Re: PIPE less
by jethro (Monsignor) on Apr 16, 2010 at 09:37 UTC

    Open to a pipe:

    open($f,'|-','less') or die "Error: Could not help"; print $f "hi"; close $f;

    More information about this you can find with "perldoc -f open"

Re: PIPE less
by ungalnanban (Pilgrim) on Apr 16, 2010 at 10:15 UTC

    using system function we can achieve this.
    See the following example code.
    use strict; use warnings; use Getopt::Std; . . . my %option=(); getopts("hwmyc:",\%option); . . . if(defined $option{h}) { system("less help_file"); #system("cat help_file | less"); #system("echo "this is help file"|less"); } . . .
    --$ugum@r--
Re: PIPE less
by BrowserUk (Patriarch) on Apr 16, 2010 at 16:36 UTC

    My take on this is that if your "usage" info is more than fits on a single screen, then it is too much and should be documented separately.

    Usage text should be an on-screen prompt of the syntax of the command that the user can put on the screen to remind him while he is typing the command.

    If the text scrolls off the top of his screen, he can't do that. In which case he's better off loading the help info--man; info; html; whatever--in a separate screen from where he is typing the command, so that he can refer between them.

    My real bottom line is that if the command syntax is so complicated that it cannot be outlined in less than one screenful of text, it would be much better to allow the user to supply the command parameters and switches through a file. Eg: command -p job.parms

    And perhaps supply a separate interactive utility or gui to construct those parameters files.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: PIPE less
by ambrus (Abbot) on Apr 16, 2010 at 16:52 UTC

    Also, for customizability, I recommend running "${PAGER-pager} || less" instead of just plain "less".

Re: PIPE less
by Crackers2 (Parson) on Apr 16, 2010 at 14:14 UTC

    I'd say don't do this.

    If your user wants to view the help in less, it's easy enough for them to pipe your script to less themselves. With an implicit less you also make it harder to just dump out the whole help.

      My less (version 436 by Mark Nudelman) doesn't page unless its STDOUT is connected to a terminal

        For the reason outlined by BrowserUK below, I often like to just dump the help to stdout so I can use (in most terminals) Shift-PgUp and Shift-PgDown to look through it while actually typing the command.

        Of course that's still possible either way. Doing something like "program --help | cat" could probably trick less into thinking it's not connected to a terminal.

      Which leads back to my answer, Re: PIPE less, giving the user more options (usually usage, help and man).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 17:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found