Last active
July 23, 2017 03:58
-
-
Save psycho23/2c68086213017f743a7f3986ceeb6912 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#what's not here: continue{} block, OOP (mostly), threads, POD, | |
# exporting subroutines (mostly), packages, debugging, | |
# special variables (mostly), regular expressions (mostly). | |
String fxns (chomp,chop,chr,index,lc,length,oct,ord,rindex,split,sprintf,substr,uc,ucfirst) | |
Array/List fxns (@ARGV,grep,join,map,pop,push,reverse,shift,sort,splice,qw//,unshift,List::Util=shuffle) | |
Hash fxns (delete,each,exists,keys,values) | |
Variable fxns (defined,ref,scalar) | |
All fxns(printf,undef,Data::Dumper) | |
Exception fxns ($@[eval error]) | |
Math fxns (**,abs,hex,int,oct,rand,sqrt,Math::Trig=:pi[pi]) | |
Misc fxns (``,__FILE__,__LINE__,__PACKAGE__,caller,die,eval,sleep,system,warn) | |
Filesystem fxns (<>,$/[$RS],chdir,chmod,Cwd,eof,File::Copy,glob,link,open,read,readlink,rename,rmdir,seek,select,stat,symlink,sysread,syswrite,sysseek,tell,umask,unlink) | |
Directory fxns (mkdir,closedir,opendir,readdir,rewinddir,seekdir,telldir) | |
Library fxns (Exporter) | |
OOP fxns (bless) | |
#--------------------------------------------------------------------------------- | |
# file_is_readable?(); file_is_writeable?(); is_dir?(); is_folder?(); | |
# is_empty_file?(); is_nonempty_file?(); is_text_file?(); | |
# is_binary_file?(); is_regular_file?() [ie. not directory, tty] | |
# file_exists?() | |
perldoc -f -X | |
# system() backtick `` operator | |
perldoc perlop #look for 'qx' | |
# <<EOF; | |
#EOF | |
perldoc perlop #look for 'here-doc' | |
#unicode stuff-that-I-find-confusing | |
perldoc perluniintro | |
#--------------------------------------------------------------------------------- | |
#on windows 8.0 using powershell 3.0 | |
echo 'how are you today?' | \ | |
perl -Mstrict -Mwarnings -ne '$_=join(\" \", unpack(\"U*\",$_));print' | |
perldoc whatever > ~/whatever.txt #& then open it with notepad. | |
#on windows 8.0 with Strawberry perl | |
perl Makefile.PL; dmake; dmake test; dmake install | |
#doing individual tests | |
prove -vbl t/whatever.t | |
#on GNU/Linux | |
echo 'how are you today?' | \ | |
perl -Mstrict -Mwarnings -ne '$_=join(" ", unpack("U*",$_));print' | |
#--------------------------------------------------------------------------------- | |
#convert binary scalar string to scalar integer value | |
bin(): sub bin{return oct('0b' . $_[0])} | |
See: | |
sprintf('%b', 100) #1100100 | |
%o #144 (octal) | |
%x #64 (hex) | |
unpack('C', chr(ord('r'))) #114 | |
bin(unpack('b*',chr(ord('r'))) #(01001110) -> 78 | |
ret_new_List_with_altered_returns{}: map{} | |
filter_valid{}: grep{} | |
redo next (ie. continue) last (ie. break) | |
my $sz = 'A crazy horse named Joe.'; | |
if(my ($name) = $sz =~ m{A .* horse named ([^.]+)\.}){ | |
# m() m'' m!! m## m// // | |
say "Name: $1"; | |
say "Name: $name"; | |
} | |
#perldoc perlretut | perlreref | perlre | perlrequick | perlcheat | |
#Precedence | |
=pod | |
or | |
and | |
LIST ops | |
= | |
?: | |
.. | |
|| // | |
&& | |
| ^ | |
& | |
== | |
<< >> | |
+ - . | |
* / % | |
=~ | |
~ | |
** | |
=cut | |
#perldoc perlcheat, perldoc perlop | |
sub callme1($){ | |
print Dumper([@_]); | |
return ''; | |
} | |
sub callme2{ | |
print Dumper([@_]); | |
return ''; | |
} | |
say callme1 1, 2, 3, 4, 5; #callme1 absorbs '1' | |
say callme2 1, 2, 3, 4, 5; #callme2 absorbs all | |
#-| is post. |- is pre. | |
#:raw prevents '\n' being treated as '\r\n' on Windows/Win32/Win64 | |
use IO::Handle; | |
open(my ${input}, '<', q!mydata.txt!) or die $!; | |
open(my ${output}, '|-:raw', 'tail -10 | sort') or die $!; | |
=pod | |
my $all; | |
{ | |
local $/; | |
$all = <$input>; | |
} | |
=cut | |
my $one_byte; | |
{ | |
local $/ = \1; #`perldoc perlvar` & look for INPUT_RECORD_SEPARATOR or $RS | |
#newline by default. | |
$one_byte = <$input>; | |
} | |
seek ${input}, 0, 0; #bring it back to the beginning. | |
${output}->autoflush(1); | |
print ${output} @input_lines; | |
exit; #same as 'exit 0' | |
#get all files in PWD | |
opendir(my $dirh, './'); | |
say for(readdir($dirh)); | |
# changes all values of @array to 'whatever'!!! | |
my @array = (1,2,3); | |
for my $i(@array){ | |
$i = 'whatever'; | |
} | |
my @array = qw{1 2 3 4 5 6 7 8 9 10 11 12 13 14 15}; | |
$_ *= 8 for (@array); | |
say shift(@array) while(@array); | |
chomp(@output = `command`); | |
sub name_of_sub{ | |
my $argument = shift // 'default'; #meaning: execute if undef | |
return $argument . ' is OK.'; | |
} | |
print name_of_sub(); | |
use feature qw/say/; | |
use feature 'say'; | |
use feature qw!say!; | |
use feature qw'say'; | |
use feature qw{say}; | |
use feature qw(say); | |
use feature qw#say#; | |
my $n = 0b0100_1011_1001_0100_1110; #309582 | |
say $n; | |
CORE::say $n; | |
my @array = qw{1 2 3 4 5 6 7}; | |
m![23]! && say for(@array); | |
#both of these are the same: | |
my $s = undef; | |
my $s; | |
my $nTotalCounted = () = get_a_list_of_whatever(); | |
my %hash = ( | |
hello=>'world', | |
world=>'cyman' | |
); | |
foreach my $key (keys %hash){ | |
delete $hash{$key}; | |
} | |
#is the same as... | |
delete @hash{keys %hash}; | |
#is the same as... | |
%hash = (); | |
#is the same as... | |
undef %hash; | |
my ($hello, $again); | |
my @world = (1); | |
sub hello{ | |
($hello,$again) = @world; | |
print "undefined\n" if(!defined($again)); #true. | |
} | |
hello(); | |
my @array = (1,2,3); | |
my $val; | |
while(defined($val = shift(@array))){ | |
print $val; | |
} | |
for(my $i=0; $i<3; $i++){ | |
print $i, "\n"; #0, 1, 2 | |
my $i = 8; | |
} | |
#works the same with hashes | |
my @has_values = (1,2,3); | |
my @no_values = (); | |
if(@has_values){ | |
print "Yes I know\n"; | |
} | |
if(! @no_values){ | |
print "Yes I know\n"; | |
} | |
while(my ($key,$value) = each(%ENV)){ | |
print "$key:$value\n"; | |
} | |
local $@; #reset to undef | |
eval{ die 'msg' }; | |
if(my $exception = $@){ | |
chomp $exception; | |
say "exception caught: $exception"; #msg at test.pl line 7. | |
} | |
#$exception does not exist out here. | |
#named locators! | |
if( 'whatever' =~ /^(?<_found>[hatw]+)(?<_cool>[erv]+)/ ){ | |
say $+{_found}, "\n", $+{_cool}; #what\never | |
} | |
Read | |
v Write | |
v v Append only | |
v v Create non-existing | |
v v Clobber existing | |
< Y N N N N * | |
> N Y N Y Y * | |
>> N Y Y Y N * | |
+< Y Y N N N | |
+> Y Y N Y Y | |
+>> Y Y Y Y N | |
#convert all characters to a list of numbers: | |
unpack('U*', $mystring); | |
#file test operators | |
-r File is readable by effective uid/gid. | |
-w File is writable by effective uid/gid. | |
-x File is executable by effective uid/gid. | |
-o File is owned by effective uid. | |
-e File exists. | |
-z File has zero size (is empty). | |
-s File has nonzero size (returns size in bytes). | |
-f File is a plain file. | |
-d File is a directory. | |
-l File is a symbolic link. | |
-T File is a textfile. | |
-B File is a binary file. | |
#get a file list from STDIN :))) | |
while (<>) { | |
chomp; | |
next unless -f $_; # ignore specials | |
#... | |
} | |
#an array with spaces for when qw// just isn't enough. | |
my @array = split(/, /, q{My string which has multiples, And here another, and another, end}); | |
local $" = ', '; #is initially a space. Cannot/should-not be undef. | |
my @array = qw/Berkeley OSU UofO Titanium-Oxide/; | |
say "@array"; #Berkeley, OSU, UofO, Titanium-Oxide | |
my $pos=-1; | |
while( ($pos = index($string, $lookfor, $pos)) > -1){ | |
print "Found @ $pos\n"; | |
$pos++; | |
} | |
delete @{ $dungeon[$x][$y] }{'OCCUPIED','DAMP','CLOTH'}; | |
use IPC::Run 'run'; | |
my $stdin = "Input"; | |
my $exitstatus = run [ $command, @args ], \$stdin, \my $stdout, \my $stderr; | |
my @options = ( \&name_of_this_sub1, \&name_of_this_sub2, | |
\&name_of_this_sub3 ); | |
print $options[rand @options]->(); #the float auto-converts to int | |
sub name_of_this_sub1{ 'yes!' } | |
sub name_of_this_sub2{ 'no!' } | |
sub name_of_this_sub3{ 'maybe!' } | |
my $i_am_done = 0; | |
$_ = 0; | |
until($i_am_done){ #this is cool | |
$_++; | |
$i_am_done = 1 if($_ > 5) | |
} | |
say; | |
(?:) means don't capture. | |
EG. (?:hello|world|end of days) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment