in reply to Is it possible to determine last executed command line?

How do you want to execute a command without calling system, backticks or the like?

Or are you referring to Perl statements?

If yes, you can often use something like that:

open($handle, '<', $file) or die "Can't open '$file' for reading: $!";

The or checks the return value of open, the last Perl statement.

Replies are listed 'Best First'.
Re^2: Is it possible to determine last executed command line?
by bdimych (Monk) on Dec 03, 2007 at 10:07 UTC
    In brief

    I have separate sub inside my project

    sub retcode_full_test { # code taken from perldoc -f system if ($? == -1) { G::abend("failed to execute: $!"); } elsif ($? & 127) { G::abend(sprintf("child died with signal %d, %s coredump", ($? + & 127), ($? & 128) ? 'with' : 'without')); } else { return $? >> 8; } }
    I've thought, that it would be better and it may be possible
    sub retcode_full_test { # code taken from perldoc -f system if ($? == -1) { G::abend("failed to execute $?????: $!"); } elsif ($? & 127) { G::abend(sprintf("$????? died with signal %d, %s coredump", ($ +? & 127), ($? & 128) ? 'with' : 'without')); } else { return $? >> 8; } }
      Rather than calling
      system( "....." ); retcode_full_text();
      you might consider modifying retcode_full_text to take the last command as an argument, and using

      my_system( "....." );

      with
      sub my_system { my ( $cmd ) = @_; system( $cmd ); retcode_full_text( $cmd ); }
        In case the caller wants to pass a list of command-line args to system(), you would want to do it like this (which seems simpler anyway):
        sub my_system { system( @_ ); retcode_full_text( "@_" ); }
        Or overriding the global system:
        BEGIN { *CORE::GLOBAL::system = \&my_system; }

        I'm not sure how to do this for backticks or pipe-open, though.