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

Hi,

Is there a perl solution to copy lines from a terminal? Let say I execute the following set of commands:

]$ ls ]$ pwd /home/baxy/zz ]$ whoami baxy ]$ which perl /usr/bin/perl
then I decide to save the outputs of those commonds (including commands themselves) is there a perl solution for that?

thnx

Replies are listed 'Best First'.
Re: Copy terminal lines-unix
by Corion (Patriarch) on Oct 06, 2015 at 08:51 UTC

    See the backticks:

    my $output = `ls`;

    You cannot read terminal output after the fact from Perl, but you can paste the terminal output into Perl:

    while( <> ) { chomp; print "You pasted: [$_]\n"; };
      agree. but what about the cases when I need to pass and execute something like this:
      ./SaveCMD "ls | perl -lne 'print $_ if /-/'" --SaveCMD-- #!/usr/bin/perl use strict; open(OUT, ">>", "cmd-out") || die "$!"; print OUT "cmd: $ARGV[0] \n"; my $cmd = $ARGV[0]; print STDERR $cmd; my $out = qx($cmd); print OUT $out; close OUT;
      in such situations I am not free to use $_ variable. any way to bypass this??

        What makes you think that you are not free to use $_ in this situation?

        Maybe now it's a good time to learn about Perl, its syntax, especially about loops?

        If you need to escape the dollar sign to prevent its expansion by the shell, use a backslash:
        ./SaveCMD "ls | perl -lne 'print \$_ if /-/'"
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Copy terminal lines-unix
by nikosv (Deacon) on Oct 06, 2015 at 17:24 UTC
    use the built in linux script command

    script makes a typescript of everything displayed on your terminal. It is useful for students who need a hardcopy record of an
    interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1).