App-Cheats

 view release on metacpan or  search on metacpan

cheats.txt  view on Meta::CPAN

perl -E 'eval { exit 1 }; say $@; say "here"'         # Blank
perl -E 'eval { exit 0 }; say $@; say "here"'         # Same
perl -E 'eval { return 1 }; say $@; say "here"'       # Return early from an eval. ürints "here"
perl -E 'eval { die }; say $@; say "here"'            # caught die,                prints "here"
perl -E 'eval { exit 1 }; say $@; END {say "here"}'   # Run before final exit.     prints "here"
perl -E 'eval { die }; say $@; END {say "here"}'      # Same.
perl -E 'open FH, ">", "file"; say FH "123"; exit 1'  # File closed and contains "123"

# Perl signal handling (eval,die,exit)
# Avoid using $SIG{__DIE__}
https://www.perlmonks.org/?node_id=1173708
perl -E '$SIG{__DIE__} = sub { say "caught die!" }; die; say $@; say "here"'

# Perl signal handling (eval,die,exit)
# Catch exit command.
perl -E 'BEGIN{ *CORE::GLOBAL::exit = sub(;$){die "EXIT_OVERRIDE: @_\n"} } eval { exit 1 }; print "caught error: $@" if $@; say "here"; exit 0'

# Perl signal handling (eval,die,exit)
# exit overrite snippet. Plus capture all signals.
# exit overrite snippet. Plus capture all signals.
our $ExitOverride = 1;
BEGIN {
    *CORE::GLOBAL::exit = sub {
        die "EXIT_OVERRIDE:Caught: @_\n" if $ExitOverride;
        CORE::exit(@_);
    };
}
local %SIG = %SIG;
KEY:
for my $Key ( sort keys %SIG ) {
    next KEY if $Key eq 'CHLD';
    next KEY if $Key eq 'CLD';
    next KEY if $Key eq '__DIE__';
    next KEY if $Key eq '__WARN__';
    $SIG{$Key} = sub { die $Key };    ## no critic
}
#
# RUN CODE HERE
#
$ExitOverride = 0;

# Perl signal handling (eval,die,__DIE__)
# Capture STDOUT and STDERR.
# Catch die and throw to STDOUT.
perl -MApp::Pod -E '{ local *STDOUT; open STDOUT, ">", \$out or die $!; local *STDERR; open STDERR, ">>", \$err or die $!; print "print-out"; print STDERR "print-err"; local $SIG{__DIE__} = sub{ my $m = shift; chomp $m; print STDERR "<$m>" }; eval{di...
#
# Use $@ to capture eval error.
# Better than SIG{__DIE__} since sub calls may except an die
# to stop something, like Pod::Simple, which is used by Pod::LOL).
perl -Ilib -MApp::Pod -E '{ local *STDOUT; open STDOUT, ">", \$out or die $!; local *STDERR; open STDERR, ">>", \$err or die $!; print "print-out"; print STDERR "print-err"; eval{die "die\n"}; print STDERR "<$@>" if $@; print "print-out2" } say "\n[$...

# Redirect to terminal even when STDOUT and/STDERR are sent somewhere else.
perl -E 'open my $fh, ">", "/dev/tty" or die $!; close *STDOUT; say $fh "111"; say "HERE"; say $fh "123";'
111
123
pod e say

# Perl Signal Handling
# Another  interesting  signal  is  signal  number  0.
# This  doesn’t  actually  affect  the target  process,
# but  instead  checks  that  it’s  alive  and  hasn’t
# changed  its  UIDs. That  is,  it  checks  whether
# it’s  legal  to  send  a  signal,  without  actually
# sending  one. 
unless (kill 0 => $kid_pid) {     
  warn "something wicked happened to $kid_pid"; 
}


#############################################################
## Perl Symbol Table
#############################################################

# Remove a subroutine from the symbol table (perl)
# defined &abs_path will still return 1 since it still exists
# but we removed a reference to it.
delete $Cwd::{'abs_path'}

# Remove the contents of a subroutine (perl)
# defined &abs_path will return 0
# Still found in symbol table
undef $Cwd::{'abs_path'}

# Snippet to capture output in perl.
# Capture output.
my $output = "";
{
    local *STDOUT;
    local *STDERR;
    open STDOUT, ">",  \$output or die $!;
    open STDERR, ">>", \$output or die $!;
    eval { App::Pod->run };
    if ( $@ ) {
        $output = $@;
        chomp $output;
    }
}

# Backup and restore STDOUT in perl.
# Backup current STDOUT
open(my $backup_stdout, '>&', STDOUT) or die "Can't duplicate STDOUT: $!";
# Redirect STDOUT to a file
open(STDOUT, '>', 'output.txt') or die "Can't redirect STDOUT: $!";
# Write to the redirected STDOUT
print "This goes to the output.txt file\n";
# Restore original STDOUT
open(STDOUT, '>&', $backup_stdout) or die "Can't restore STDOUT: $!";

# Give a name to an anonymous sub/function.
# Inside the sub.
# Single global variable.
my $code = sub {
    local *__ANON__ = 'code_name';
    ...
};
$code->();

# Give a name to an anonymous sub/function.
# Outside the sub.
use Sub::Util;
*{"${class}::$_"} = set_subname("${class}::$_", $patch{$_}) for keys %patch;



( run in 2.374 seconds using v1.01-cache-2.11-cpan-df04353d9ac )