App-Cheats

 view release on metacpan or  search on metacpan

cheats.txt  view on Meta::CPAN

  -f, --filter value    Filter output based on conditions provided (default [])
                        - dangling=(true|false)
                        - label=<key> or label=<key>=<value>
                        - before=(<image-name>[:tag]|<image-id>|<image@digest>)
                        - since=(<image-name>[:tag]|<image-id>|<image@digest>)
                        - reference=(pattern of an image reference)
docker image ls -f reference=poti1/my-perl-server


#############################################################
## Docker Images/Containers - Cleanup
#############################################################

# Remove all stoped containers.
docker container prune

# Auto remove container on exit
docker run --rm <IMAGE_ID> perl -E 'say $^V'

# Remove all docker containers and images.
docker rm --force $(docker ps -aq)
docker rmi --force $(docker images -aq)

# Remove stuck containers
sudo systemctl stop docker.service
sudo su
rm -f /var/lib/docker/containers/*  # just "sudo ..." doesnt work for some reason.
exit
sudo systemctl start docker.service


#############################################################
## Docker Logs
#############################################################

# View messages from containers.
docker logs CONTAINER
docker logs -f CONTAINER


#############################################################
## Docker Attach/Detach
#############################################################

# Attach to a container.
docker run CONTAINER
docker start -a CONTAINER

# Detach from a container.
docker run -d CONTAINER


#############################################################
## DockerHub (push/pull)
#############################################################

# Need to first create an auth key on dockerhub
docker login -u poti1

# Upload to dockerhub.
# Make sure repo is public or you cant upload due to restrictions.
docker push poti1/my-perl-server:latest

# Fetch image.
docker pull poti1/my-perl   # Always fetches the latest.
docker run poti1/my-perl    # Fetches only if missing locally.

# Use an uploaded image.
docker run -it --rm poti1/my-perl -E 'say 123'


#############################################################
## Docker Data/Volumes
#############################################################

# 2 types of volumes/mount
# -v LOCAL:DOCKER:PERMISSIONS
docker run -v /app/data                 # Anynymous volume.
docker run -v data:/app/data            # Named volume.
docker run -v $(pwd)/data:/app/data     # Bind mount.
docker run -v $(pwd)/data:/app/data:ro  # Bind mount (read-only).
#
# anonymous - closed on shutdown
# named     - persistent.

# View docker volumes
# Does NOT show bind mounts.
# Only for development use.
docker volume ls

# Create anonymous docker volume
VOLUME [ "/app/path" ]
# Same as:
-v /app/path

# Create named docker volume
# Not deleted on container shutdown.
# NAME:/PATH_IN_CONTAINER:PERMISSIONS
# NAME should be absolute
docker run -v $(pwd)feedback:/app/feedback feedback-node
docker run -v $(pwd)feedback:/app/feedback:ro feedback-node

# Docker volume gotchas.
# Mount a folder will overwrite all content in container folder.
# If clash of volume paths, longer wins.
docker run -d --rm -p 3000:80 --name feedback-app -v $(pwd)/feedback:/app/feedback -v $(pwd):/app -v /app/node_modules feedback-node


#############################################################
## Docker ARG/ENV
#############################################################

# Using environmental variables in a Dockerfile.
ENV PORT 80
EXPOSE $PORT

# Build and run using an environmental variable
docker run --env DEBUG=1
docker run --rm --env PORT=8000 poti1/my-perl -E 'say "123 $ENV{PORT}"'

# Provide arguments when building a docker image.
ARG DEFAULT_PORT=8080
ENV PORT $DEFAULT_PORT
EXPOSE $PORT
docker build --build-arg DEFAULT_PORT=3000


#############################################################

cheats.txt  view on Meta::CPAN

# Create a basic loading hour glass (status bar) (what to monitor is taken from input)
perl -e '$|++; sub p{select undef,undef,undef,0.5} sub b{print "\b \b"} @a=qw( | / - \\ ); $c=shift; while(eval "`$c`"){ print $a[$i++ % @a]; p; b } print "\n"' 'ps -elf | grep watch | grep junk | grep -v $$'

# Function to print to the same line (loading glass).
sub _SingleLinePrint {
    my ( $Self, $LineRaw ) = @_;
    my $Line   = $Self->_ReplaceColorTags($LineRaw);
    my $Length = length($Line);
    print "\b" x $Length;
    print " "  x $Length;
    print "\b" x $Length;
    print $Line;
    return;
}


#############################################################
## Perl Pack
#############################################################

# Check perldoc perlpacktut

# Using pack tutorial
perl -le "print qq([$_]) for unpack 'x3 A2 x4 A3 x4 A2', 'To BE or not to be'"

# Convert a string to its ascii values
perl -le 'print "[$_]" for unpack "C*", "ABC"'

# Convert ascii values to string characters
perl -le 'print "[$_]" for pack "C*", 65,66,67'
perl -le 'print "[$_]" for pack "C*", hex(41),66,67'

# Convert hexidecimal to decimal
perl -le 'print "[$_]" for pack "H*", "41"'

# Pack/convert ordinals to a string of aschii values
perl -le '@mem=(65,66,67); print for pack "C*",@mem'

# Unpack a string into its ordinal values
perl -le '$mem="ABC"; print for unpack "C*",$mem'

# Data selection. alternative to substr. Use to column splitting when spaces do not necessary delimit
perl -le '($what,$where,$howmuch)=@ARGV; print unpack "x$where A$howmuch", $what' "[abc def][hij][klm]" 1 7

# sprintf versus pack, unpack.
# These are same.
perl -E 'say for unpack "H*", pack "C*", 100'
perl -E 'printf "%x\n", 100'
64


#############################################################
## Perl PAUSE Account
#############################################################

# Upload to pause server without a browser.
# 1. Need to file create a .pause file
# 2. Install required modules:
cpanm CPAN::Uploader
cpanm Config::Identity
cpan-upload my_module.tar.gz


#############################################################
## Perl Regular Expressions - General
#############################################################

# Match a number followed by an equal amount of another number
# Dynamic recursive regex
perl -le '$r=qr/0(??{$r})?1/; print "001" =~ $r ? "PASS" : "FAIL"'
#
# Just dynamic
perl -le '$r=qr/0(??{1 x (length $1)})/; print "000111" =~ /^$r$/ ? "PASS" : "FAIL"'
#
# Pattern group (?PARNO)
perl -le '$r=qr/(0(?1)?1)/; print "0011" =~ /^$r$/ ? "PASS" : "FAIL"'

# Perl regex optimization working
echo "line1 Liine2" | perl -nle '$r=qr[(?s-xim:line1(?{print"got line1\n"}).*?line2(?{print"got line2\n"}))]; /$r/'

# Perl regex optimization is supressed (with either character class or Dynamic Regex Construct)
echo "line1 Liine2" | perl -nle '$r=qr[(?s-xim:line1(?{print"got line1\n"}).*?[Ll]ine2(?{print"got line2\n"}))]; /$r/'
echo "line1 Liine2" | perl -nle '$r=qr[(?s-xim:line1(?{print"got line1\n"}).*?(??{line2})(?{print"got line2\n"}))]; /$r/'

# Global match in JavaScript, but all return all captures. /(.*?)/g
regex = /<script[^>]*>(?<s>.*?)<\/script>/g
str = "blah<script>111</script><script>222</script><script>333</script>blah2"
while(my = regex.exec(str)){ console.log(my[1]) }

# Remove control characters
perl -E '$c = "\e[31mHERE\e[0m"; $c =~ s&\p{PosixCntrl}&*&g; say $c'
# \p{PosixCntrl}  - ASCII-range Unicode.
# \p{XPosixCntrl} - Full-range Unicode.

# Named captures affect %+ and %+, but ALSO $1,$2
perl -Mojo -E '"abc" =~ /(?<first>.)(?<second>.)(?<third>.)/; say r \%-; say r \%+; say "1:$1"; say "2:$2"; say "3:$3"'

# Using /a flag to limit to ascii.
perl -C -E 'say "\N{BENGALI DIGIT FOUR}"' # ৪
perl -C -E 'say "\N{BENGALI DIGIT FOUR}" =~ /\d/' # 1
perl -C -E 'say "\N{BENGALI DIGIT FOUR}" =~ /\d/a' # ""
perl -C -E 'say 0+"\N{BENGALI DIGIT FOUR}"' # 0

# CAUTION: regex variables get reset on the next successful match.
perl -E '"abc" =~ /(.*)/; say "[$1]"; 123 =~ /1/; say "[$1]"'
[abc]
[]
#
# Also a function passes in its variables by reference.
# @_ is an alias to the arguments.
perl -E 'sub f { $_[0] = "new" } my $v = "old"; f($v); say $v'
new
#
# This can lead to input suddenly disappearing
# or changing when using unquoted regex variables.
perl -E 'sub f{ say "f(@_)" } "abc" =~ /(.*)/; f($1)'
F1(abc)  # Bad, but still works.
perl -E 'sub f{ 123 =~ /1/; say "F1(@_)" } "abc" =~ /(.*)/; f($1)'
F1()     # BAD, $1 gets changed.
perl -E 'sub f{ 123 =~ /1/; say "f(@_)" } "abc" =~ /(.*)/; f("$1")'
f(abc)   # Correct way: quote "$1"

cheats.txt  view on Meta::CPAN

x2hs -X Example      # Pure Perl
h2xs -A -n Example   # XS
perl Makefile.PL
make
perl -Mblib -MExample2 -E 'Example2::print_hello()'

# Using -Mblib is similar to using:
perl -Iblib/lib -Iblib/arch -MExample2 -e 'Example2b::print_hello'

# Create a new distribution in perl.
module-starter --module=My::Test --distro=my-test --author="AUTHOR" --email="EMAIL" --mb --verbose

# Additional folder preparation (module-starter)
mv App-Pod/* .
rmdir App-Pod
mv ignore.txt .gitignore
echo "*.swp" >> .gitignore
chmod +x Build.PL
# Remove MYMETA.* and META.*
#
# Prepend to Build.PL
"#!/bin/env perl
"

# Additional folder preparation (module-starter)
# Add to Build.PL
    meta_merge     => {
        resources => {
            bugtracker => 'https://github.com/poti1/data-trace/issues',
            repository => 'https://github.com/poti1/data-trace',
        },
    },

# Additional folder preparation (module-starter)
# Create: .github/workflows/ci.yml
---
name: build and test
on:
  push:
    branches:
      - "*"
  pull_request:
    branches:
      - "*"
  workflow_dispatch:
jobs:
  build-job:
    name: Build distribution
    runs-on: ubuntu-20.04
    container:
      image: perldocker/perl-tester:5.38
    steps:
      - uses: actions/checkout@v4
      - name: Run Tests
        env:
          AUTHOR_TESTING: 1
          AUTOMATED_TESTING: 1
          EXTENDED_TESTING: 1
          RELEASE_TESTING: 1
        run: auto-build-and-test-dist
      - uses: actions/upload-artifact@v4
        with:
          name: build_dir
          path: build_dir
        if: ${{ github.actor != 'nektos/act' }}
  coverage-job:
    needs: build-job
    runs-on: ubuntu-20.04
    container:
      image: perldocker/perl-tester:5.38
    steps:
      - uses: actions/checkout@v4 # codecov wants to be inside a Git repository
      - uses: actions/download-artifact@v4
        with:
          name: build_dir
          path: .
      - name: Install deps and test
        run: cpan-install-dist-deps && test-dist
        env:
          CODECOV_TOKEN: ${{secrets.CODECOV_TOKEN}}
  test-job:
    needs: build-job
    strategy:
      fail-fast: true
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        distribution: [default, strawberry]
        perl-version:
          - "5.24"
          - "5.26"
          - "5.28"
          - "5.30"
          - "5.32"
          - "5.34"
          - "5.36"
          - "5.38"
        exclude:
          - { os: windows-latest, distribution: default }
          - { os: macos-latest,   distribution: strawberry }
          - { os: ubuntu-latest,  distribution: strawberry }
          - { distribution: strawberry, perl-version: "5.8" }
          - { distribution: strawberry, perl-version: "5.10" }
          - { distribution: strawberry, perl-version: "5.12" }
          - { distribution: strawberry, perl-version: "5.34" }
          - { distribution: strawberry, perl-version: "5.36" }
    runs-on: ${{ matrix.os }}
    name:  on ${{ matrix.os }} perl ${{ matrix.perl-version }}
    steps:
      - name: set up perl
        uses: shogo82148/actions-setup-perl@v1.28.0
        with:
          perl-version: ${{ matrix.perl-version }}
          distribution: ${{ matrix.distribution }}
      - uses: actions/download-artifact@v4
        with:
          name: build_dir
          path: .
      - name: install deps using cpanm
        uses: perl-actions/install-with-cpanm@v1
        with:
          cpanfile: "cpanfile"



( run in 1.468 second using v1.01-cache-2.11-cpan-b16cb0d3907 )