App-Cheats

 view release on metacpan or  search on metacpan

cheats.txt  view on Meta::CPAN


# Perl Symbol Table
# Type glob magic - only the type gets modified.
*foo = \$scalar; 
*foo = \@array;


#############################################################
## Perl Unicode - General
#############################################################

# Create invalid Malformed UTF-8 character (unicode)
perl -C -Me -MEncode -E 'my $v = "a\372z"; dd $v; Encode::_utf8_on($v); say ""; dd $v; say $v'

# Check if valid utf8 (unicode)
Encode::is_utf8( $Param{Text}, 1 )
utf8::valid( $Param{Text} )

# Pick a unicode character at a time.
perl -Mutf8 -C -E 'say for "äö" =~ /(\X)/g'
ä
ö
perl -Mutf8 -C -E 'say for "äö" =~ /(.)/g'
ä
ö


#############################################################
## Perl Unicode - Codes
#############################################################

# Unicode salute/saluting.
perl -C -E 'say "\x{1FAE1}"'
🫡
# Draw a box with unicode in perl.
perl -C -E 'say "\N{BOX DRAWINGS LIGHT ARC DOWN AND RIGHT}" . ("\N{BOX DRAWINGS LIGHT HORIZONTAL}" x 5) . "\N{BOX DRAWINGS LIGHT ARC DOWN AND LEFT}"; say "\N{BOX DRAWINGS LIGHT VERTICAL}     \N{BOX DRAWINGS LIGHT VERTICAL}" for 1..2; say "\N{BOX DRAW...

# Unicode error codes:
➜ HEAVY ROUND-TIPPED RIGHTWARDS ARROW U+279c 0x279c 10140 023634
✖ HEAVY MULTIPLICATION X              U+2716 0x2716 10006 023426

# Unicode star
★ BLACK STAR U+2605 0x2605 9733 023005


#############################################################
## Perl Unicode - Mojibake
#############################################################

# Perl mojibake guide.
https://dev.to/drhyde/a-brief-guide-to-perl-character-encoding-if7

# Perl mojibake examples. (wrong length)
perl -E '$s = "é"; say $s . " contains " . length($s) . " chars"'
é contains 2 chars

# Perl mojibake examples. (utf8 is not enough)
perl -Mutf8 -E '$s = "é"; say $s . " contains " . length($s) . " chars"'
� contains 1 chars

# Perl mojibake examples. (-C or binmode to get correct encoding and therefore length)
perl -Mutf8 -E 'binmode(STDOUT, ":encoding(UTF-8)"); $s = "é"; say $s . " contains " . length($s) . " chars"'
perl -Mutf8 -C -E '$s = "é"; say $s . " contains " . length($s) . " chars"'
perl -Mutf8 -C -E 'binmode(STDOUT, ":encoding(UTF-8)"); $s = "é"; say $s . " contains " . length($s) . " chars"'
é contains 1 chars

# Perl mojibake examples. (Simulate malformed UTF-8 character warnings)
echo '"key": "é"' > my.out
iconv -f utf-8 -t latin1 my.out > my2.out
file my*.out
cat my*
    "key": "�"
    "key": "é"
cat my2.out | perl -Mutf8 -C -lne '/\d/'
cat my2.out | perl -C -lne '/\d/'
    Malformed UTF-8 character: \xe9\x22 (too short; 2 bytes available, need 3) in pattern match (m//) at -e line 1, <> line 1.
    Malformed UTF-8 character: \xe9\x22 (unexpected non-continuation byte 0x22, immediately after start byte 0xe9; need 3 bytes, got 1) in pattern match (m//) at -e line 1, <> line 1.
cat my2.out | perl -Mutf8 -C -ne '/\d/'
cat my2.out | perl -C -ne '/\d/'
perl -C -ne '/\d/' < my2.out
perl -CI -ne '/\d/' < my2.out
perl -ne 'INIT{binmode STDIN, ":utf8"} /\d/; print' < my2.out
    Malformed UTF-8 character: \xe9\x22\x0a (unexpected non-continuation byte 0x22, immediately after start byte 0xe9; need 3 bytes, got 1) in pattern match (m//) at -e line 1, <> line 1.
perl -ne 'INIT{binmode STDIN, ":encoding(UTF-8)"} /\d/; print' < my2.out
    "key": "\xE9"
perl -C -lne 'print utf8::valid($_) ? "valid" : "invalid"' < my.out
    valid
perl -C -lne 'print utf8::valid($_) ? "valid" : "invalid"' < my2.out
    invalid
#
# Summary:
    - A file/string may be declared as utf8, but it really is not.
    - "-CI" is the same as 'binmode STDIN, ":utf8"'
    - ":encoding(UTF-8)" should be preferred over ":utf8"
    - Use "utf8::valid" to check for malformed strings.

# iconv using perl (piconv)
# Saves a file using wrong encoding (mojibake)
perl -CA -le 'open OUT, ">:encoding(latin1)", "my3.out" or die $!; print OUT shift' '"key": "é",'

# Find non ascii characters.
perl -C -lne 'print $1 if /([^[:ascii:]])/' my.yml
uni_convert --string "$(perl -C -lne 'print $1 if /([^[:ascii:]])/' my.csv)"
echo 'aböc' | perl -nE 'say "[$1]" if /(\P{ASCII}+)/'


#############################################################
## Perl Unicode - Encode/Decode
#############################################################

# Compare use of encode/decode.
# Start with non unicode.
perl -C -MEncode -Mutf8 -C -Me -e '$_ = "\xef\xac\xa1"; my $en = eval{encode("UTF-8", $_)} // ""; my $de = eval{decode("UTF-8", $_)} // ""; say; say $en; say $de; dd $_; dd $en, dd $de'
ﬡ
ﬡ
ﬡ
SV = PV(0xb40000740302de60) at 0xb4000074030a9be8
  REFCNT = 1
  FLAGS = (POK,IsCOW,pPOK)
  PV = 0xb400007382ce28a0 "\xEF\xAC\xA1"\0
  CUR = 3
  LEN = 16
  COW_REFCNT = 1
SV = PV(0xb40000740302e0d0) at 0xb4000074030a93a8
  REFCNT = 1
  FLAGS = (POK,pPOK,UTF8)
  PV = 0xb4000074031623d0 "\xEF\xAC\xA1"\0 [UTF8 "\x{fb21}"]
  CUR = 3
  LEN = 16
SV = PV(0xb40000740302dea0) at 0xb40000740301f930
  REFCNT = 1
  FLAGS = (POK,pPOK)
  PV = 0xb400007382ce2d70 "\xC3\xAF\xC2\xAC\xC2\xA1"\0
  CUR = 6
  LEN = 16

# Compare use of encode/decode.
# Start with unicode.
perl -C -MEncode -Mutf8 -C -Me -e '$_ = "\x{fb21}"; my $en = eval{encode("UTF-8", $_)} // ""; my $de = eval{decode("UTF-8", $_)} // ""; say; say $en; say $de; dd $_; dd $en, dd $de'
ﬡ
ﬡ
SV = PV(0xb40000721e02de60) at 0xb40000721e0a2be8
  REFCNT = 1
  FLAGS = (POK,IsCOW,pPOK,UTF8)
  PV = 0xb40000719dce28a0 "\xEF\xAC\xA1"\0 [UTF8 "\x{fb21}"]
  CUR = 3
  LEN = 16
  COW_REFCNT = 1
SV = PV(0xb40000721e034590) at 0xb40000721e0a23a8
  REFCNT = 1
  FLAGS = (POK,IsCOW,pPOK)
  PV = 0xb40000721e02c0c0 ""\0
  CUR = 0

cheats.txt  view on Meta::CPAN

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

# Simple exmplae of parallel processing
# (Perl Modules - AnyEvent)
# NOT WORKING!
perl -MAnyEvent -E 'my @files = (1..30); my $cv = AnyEvent->condvar; foreach my $file (@files) { $cv->begin; AnyEvent->timer(after => 0, cb => sub { say "Processing file $file"; sleep(1); $cv->end; }); } $cv->recv;'


#############################################################
## Perl Modules - Automake::Config
#############################################################

# Install Automake::Config (termux)
git clone git@github.com:poti1/arm-none-eabi.git
cd arm-none-eabi
cpanm --look automake-1.15.gz
$ ./configure
$ make
$ make install


#############################################################
## Perl Modules - autovivification
#############################################################

# autovivification Example:
perl -Me -E 'my $h = { k => 11  }; no autovivification; say defined $h->{k2}{k3}{k5}; p $h'
{
    k   11
}


#############################################################
## Perl Modules - B::Concise
#############################################################

# Perl Modules - B::Concise
# explain what a perl program is doing (very concise).
perl -MO=Concise -e 'print 111'


#############################################################
## Perl Modules - B::Deparse
#############################################################

# Perl Modules - B::Deparse
# explain what a perl program is doing (simply)
perl -MO=Deparse -e 'print 111'


#############################################################
## Perl Modules - bignum
#############################################################

# Convert big numbers into full form
# from scientific notation to expanded form
echo "$b" | perl -Mbignum -lpe '$_ += 0'


#############################################################
## Perl Modules - binmode
#############################################################

# Using unicode in perl STDOUT
perl -CO   script
perl -C    script # Which is same as
perl -CDSL script # S includes I/O
perl -e 'binmode STDOUT, "encoding(UTF-8)"'
perl -e 'binmode STDOUT, ":utf8"'
perl -E 'use open qw/:std :utf8/; say "\N{SNOWFLAKE}"'

# Mixed up encoding.
perl -E '$s = "é"; say length($s) . " $s"'
2 é
perl -C -E '$s = "é"; say length($s) . " $s"'
2 é
perl -Mutf8 -E '$s = "é"; say length($s) . " $s"'
1 �
perl -C -Mutf8 -E '$s = "é"; say length($s) . " $s"'
1 é


#############################################################
## Perl Modules - Business::CreditCard
#############################################################

# Validate a credit card number.
perl -MBusiness::CreditCard -E 'say validate("5276 4400 6542 1319")'
1
#
perl -MBusiness::CreditCard -E 'say cardtype("5276 4400 6542 1319")'
MasterCard


#############################################################
## Perl Modules - charnames
#############################################################

# Convert between a Unicode character, hexidecimal number and the name
perl -CDAS -E 'use charnames(); printf "%s %#x %s\n", $_, ord, charnames::viacode(ord) for @ARGV' ❄ ☃
# ❄ 0x2744 SNOWFLAKE
# ☃ 0x2603 SNOWMAN

# Converting between a Unicode name, code, and string
# Name: SNOWFLAKE
# Code: 0x2744, 10052
# String: \N{SNOWFLAKE}, \N{U+2744}, \x{2744}, ❄
#
perl -C -E 'say "\N{SNOWFLAKE}"'                                                 # \N{SNOWFLAKE} -> ❄
perl -C -E 'say "\N{U+2744}"'                                                    # \N{U+2744}    -> ❄
perl -C -E 'say "\x{2744}"'                                                      # \x{2744}      -> ❄
perl -C -Mutf8 -E 'say "❄"'                                                      # ❄             -> ❄
perl -E 'say "❄"'                                                                # ❄             -> ❄
perl -E 'use open qw/:std :utf8/; say "\N{SNOWFLAKE}"'                           # \N{SNOWFLAKE} -> ❄
#
perl -Mutf8 -E 'printf "%#x\n", ord "❄"'                                         # ❄             -> 0x2744
perl -Mutf8 -E 'say ord "❄"'                                                     # ❄             -> 10052
perl -Mutf8 -Mcharnames=:full -E 'say charnames::viacode ord "❄"'                # ❄             -> SNOWFLAKE
#
perl -C -Mcharnames=:full -E 'say charnames::vianame("SNOWFLAKE")'               # SNOWFLAKE     -> 2744
perl -C -Mcharnames=:full -E 'printf "%#x\n", charnames::vianame("SNOWFLAKE")'   # SNOWFLAKE     -> 0x2744
perl -C -Mcharnames=:full -E 'say charnames::string_vianame("SNOWFLAKE")'        # SNOWFLAKE     -> ❄
#
perl -C -Mcharnames=:full -E 'say charnames::viacode("U+2744")'                  # U+2744        -> SNOWFLAKE
perl -C -Mcharnames=:full -E 'say charnames::viacode(0x2744)'                    # 0x2744        -> SNOWFLAKE
perl -C -Mcharnames=:full -E 'say charnames::viacode("10052")'                   # 10052         -> SNOWFLAKE

# Difference between the different whitespace regex characters
perl -Mcharnames=:full -E 'my @qr = (qr/\s/, qr/\h/, qr/\v/, qr/[[:space:]]/, qr/\p{Space}/); my $fmt = "%#06x" . ("%2s" x @qr) . " %s\n"; printf "\nVersion: $^V\n$fmt\n", qw/- s h v p u Name/; for my $ord (0..0x10ffff){ my $chr = chr $ord; next unle...



( run in 1.901 second using v1.01-cache-2.11-cpan-d8267643d1d )