| PERLRUN(1) | Perl Programmers Reference Guide | PERLRUN(1) |
With methods 2 and 3, Perl starts parsing the input file from the beginning, unless you've specified a -x switch, in which case it scans for the first line starting with "#!" and containing the word "perl", and starts there instead. This is useful for running a program embedded in a larger message. (In this case you would indicate the end of the program using the "__END__" token.)
The "#!" line is always examined for switches as the line is being parsed. Thus, if you're on a machine that allows only one argument with the "#!" line, or worse, doesn't even recognize the "#!" line, you still can get consistent switch behaviour regardless of how Perl was invoked, even if -x was used to find the beginning of the program.
Because historically some operating systems silently chopped off kernel interpretation of the "#!" line after 32 characters, some switches may be passed in on the command line, and some may not; you could even get a "-" without its letter, if you're not careful. You probably want to make sure that all your switches fall either before or after that 32-character boundary. Most switches don't actually care if they're processed redundantly, but getting a "-" instead of a complete switch could cause Perl to try to execute standard input instead of your program. And a partial -I switch could also cause odd results.
Some switches do care if they are processed twice, for instance combinations of -l and -0. Either put all the switches after the 32-character boundary (if applicable), or replace the use of -0digits by "BEGIN{ $/ = "\0digits"; }".
Parsing of the "#!" switches starts wherever "perl" is mentioned in the line. The sequences "-*" and "- " are specifically ignored so that you could, if you were so inclined, say
#!/bin/sh #! -*-perl-*- eval 'exec perl -x -wS $0 ${1+"$@"}' if 0;
to let Perl see the -p switch.
A similar trick involves the env program, if you have it.
#!/usr/bin/env perl
The examples above use a relative path to the perl interpreter, getting whatever version is first in the user's path. If you want a specific version of Perl, say, perl5.14.1, you should place that directly in the "#!" line's path.
If the "#!" line does not contain the word "perl" nor the word "indir", the program named after the "#!" is executed instead of the Perl interpreter. This is slightly bizarre, but it helps people on machines that don't do "#!", because they can tell a program that their SHELL is /usr/bin/perl, and Perl will then dispatch the program to the correct interpreter for them.
After locating your program, Perl compiles the entire program to an internal form. If there are any compilation errors, execution of the program is not attempted. (This is unlike the typical shell script, which might run part-way through before finding a syntax error.)
If the program is syntactically correct, it is executed. If the program runs off the end without hitting an exit() or die() operator, an implicit exit(0) is provided to indicate successful completion.
extproc perl -S -your_switches
as the first line in "*.cmd" file (-S due to a bug in cmd.exe's `extproc' handling).
$ perl -mysw 'f$env("procedure")' 'p1' 'p2' 'p3' 'p4' 'p5' 'p6' 'p7' 'p8' ! $ exit++ + ++$status != 0 and $exit = $status = undef;
at the top of your program, where -mysw are any command line switches you want to pass to Perl. You can now invoke the program directly, by saying "perl program", or as a DCL procedure, by saying @program (or implicitly via DCL$PATH by just using the name of the program).
This incantation is a bit much to remember, but Perl will display it for you if you say "perl "-V:startperl"".
Command-interpreters on non-Unix systems have rather different ideas on quoting than Unix shells. You'll need to learn the special characters in your command-interpreter ("*", "\" and """ are common) and how to protect whitespace and these characters to run one-liners (see -e below).
On some systems, you may have to change single-quotes to double ones, which you must not do on Unix or Plan 9 systems. You might also have to change a single % to a %%.
For example:
# Unix perl -e 'print "Hello world\n"' # MS-DOS, etc. perl -e "print \"Hello world\n\"" # VMS perl -e "print ""Hello world\n"""
The problem is that none of this is reliable: it depends on the command and it is entirely possible neither works. If 4DOS were the command shell, this would probably work better:
perl -e "print <Ctrl-x>"Hello world\n<Ctrl-x>""
CMD.EXE in Windows NT slipped a lot of standard Unix functionality in when nobody was looking, but just try to find documentation for its quoting rules.
There is no general solution to all of this. It's just a mess.
In this documentation, "#!/usr/bin/perl" on the first line of the program will stand in for whatever method works on your system. You are advised to use a specific path if you care about a specific version.
#!/usr/local/bin/perl5.14
or if you just want to be running at least version, place a statement like this at the top of your program:
use 5.014;
#!/usr/bin/perl -spi.orig # same as -s -p -i.orig
A "--" signals the end of options and disables further option processing. Any arguments after the "--" are treated as filenames and arguments.
Switches include:
find . -name '*.orig' -print0 | perl -n0e unlink
The special value 00 will cause Perl to slurp files in paragraph mode. Any value 0400 or above will cause Perl to slurp files whole, but by convention the value 0777 is the one normally used for this purpose.
You can also specify the separator character using hexadecimal notation: -0xHHH..., where the "H" are valid hexadecimal digits. Unlike the octal form, this one may be used to specify any Unicode character, even those beyond 0xFF. So if you really want a record separator of 0777, specify it as -0x1FF. (This means that you cannot use the -x option with a directory name that consists of hexadecimal digits, or else Perl will think you have specified a hex number to -0.)
perl -ane 'print pop(@F), "\n";'
is equivalent to
while (<>) { @F = split(' '); print pop(@F), "\n"; }
An alternate delimiter may be specified using -F.
-a implicitly sets -n.
As of 5.8.1, the -C can be followed either by a number or a list of option letters. The letters, their numeric values, and effects are as follows; listing the letters is equal to summing the numbers.
I 1 STDIN is assumed to be in UTF-8 O 2 STDOUT will be in UTF-8 E 4 STDERR will be in UTF-8 S 7 I + O + E i 8 UTF-8 is the default PerlIO layer for input streams o 16 UTF-8 is the default PerlIO layer for output streams D 24 i + o A 32 the @ARGV elements are expected to be strings encoded in UTF-8 L 64 normally the "IOEioA" are unconditional, the L makes them conditional on the locale environment variables (the LC_ALL, LC_CTYPE, and LANG, in the order of decreasing precedence) -- if the variables indicate UTF-8, then the selected "IOEioA" are in effect a 256 Set ${^UTF8CACHE} to -1, to run the UTF-8 caching code in debugging mode.
For example, -COE and -C6 will both turn on UTF-8-ness on both STDOUT and STDERR. Repeating letters is just redundant, not cumulative nor toggling.
The "io" options mean that any subsequent open() (or similar I/O operations) in the current file scope will have the ":utf8" PerlIO layer implicitly applied to them, in other words, UTF-8 is expected from any input stream, and UTF-8 is produced to any output stream. This is just the default, with explicit layers in open() and with binmode() one can manipulate streams as usual.
-C on its own (not followed by any number or option list), or the empty string "" for the "PERL_UNICODE" environment variable, has the same effect as -CSDL. In other words, the standard I/O handles and the default "open()" layer are UTF-8-fied but only if the locale environment variables indicate a UTF-8 locale. This behaviour follows the implicit (and problematic) UTF-8 behaviour of Perl 5.8.0. (See "UTF-8 no longer default under UTF-8 locales" in perl581delta.)
You can use -C0 (or "0" for "PERL_UNICODE") to explicitly disable all the above Unicode features.
The read-only magic variable "${^UNICODE}" reflects the numeric value of this setting. This variable is set during Perl startup and is thereafter read-only. If you want runtime effects, use the three-arg open() (see "open" in perlfunc), the two-arg binmode() (see "binmode" in perlfunc), and the "open" pragma (see open).
(In Perls earlier than 5.8.1 the -C switch was a Win32-only switch that enabled the use of Unicode-aware "wide system call" Win32 APIs. This feature was practically unused, however, and the command line switch was therefore "recycled".)
Note: Since perl 5.10.1, if the -C option is used on the "#!" line, it must be specified on the command line as well, since the standard streams are already set up at this point in the execution of the perl interpreter. You can also use binmode() to set the encoding of an I/O stream.
For example, to watch how perl executes your program, use -Dtls. Another nice value is -Dx, which lists your compiled syntax tree, and -Dr displays compiled regular expressions; the format of the output is explained in perldebguts.
As an alternative, specify a number instead of list of letters (e.g., -D14 is equivalent to -Dtls):
1 p Tokenizing and parsing (with v, displays parse stack) 2 s Stack snapshots (with v, displays all stacks) 4 l Context (loop) stack processing 8 t Trace execution 16 o Method and overloading resolution 32 c String/numeric conversions 64 P Print profiling info, source file input state 128 m Memory and SV allocation 256 f Format processing 512 r Regular expression parsing and execution 1024 x Syntax tree dump 2048 u Tainting checks 4096 U Unofficial, User hacking (reserved for private, unreleased use) 16384 X Scratchpad allocation 32768 D Cleaning up 65536 S Op slab allocation 131072 T Tokenizing 262144 R Include reference counts of dumped variables (eg when using -Ds) 524288 J show s,t,P-debug (don't Jump over) on opcodes within package DB 1048576 v Verbose: use in conjunction with other flags to increase the verbosity of the output. Is a no-op on many of the other flags 2097152 C Copy On Write 4194304 A Consistency checks on internal structures 8388608 q quiet - currently only suppresses the "EXECUTING" message 16777216 M trace smart match resolution 33554432 B dump suBroutine definitions, including special Blocks like BEGIN 67108864 L trace Locale-related info; what gets output is very subject to change 134217728 i trace PerlIO layer processing. Set PERLIO_DEBUG to the filename to trace to.
All these flags require -DDEBUGGING when you compile the Perl executable (but see ":opd" in Devel::Peek or "'debug' mode" in re which may change this). See the INSTALL file in the Perl source distribution for how to do this.
If you're just trying to get a print out of each line of Perl code as it executes, the way that "sh -x" provides for shell scripts, you can't use Perl's -D switch. Instead do this
# If you have "env" utility env PERLDB_OPTS="NonStop=1 AutoTrace=1 frame=2" perl -dS program # Bourne shell syntax $ PERLDB_OPTS="NonStop=1 AutoTrace=1 frame=2" perl -dS program # csh syntax % (setenv PERLDB_OPTS "NonStop=1 AutoTrace=1 frame=2"; perl -dS program)
See perldebug for details and variations.
Perl can be built so that it by default will try to execute $Config{sitelib}/sitecustomize.pl at startup (in a BEGIN block). This is a hook that allows the sysadmin to customize how Perl behaves. It can for instance be used to add entries to the @INC array to make Perl find modules in non-standard locations.
Perl actually inserts the following code:
BEGIN { do { local $!; -f "$Config{sitelib}/sitecustomize.pl"; } && do "$Config{sitelib}/sitecustomize.pl"; }
Since it is an actual "do" (not a "require"), sitecustomize.pl doesn't need to return a true value. The code is run in package "main", in its own lexical scope. However, if the script dies, $@ will not be set.
The value of $Config{sitelib} is also determined in C code and not read from "Config.pm", which is not loaded.
The code is executed very early. For example, any changes made to @INC will show up in the output of `perl -V`. Of course, "END" blocks will be likewise executed very late.
To determine at runtime if this capability has been compiled in your perl, you can check the value of $Config{usesitecustomize}.
-F implicitly sets both -a and -n.
If no extension is supplied, and your system supports it, the original file is kept open without a name while the output is redirected to a new file with the original filename. When perl exits, cleanly or not, the original file is unlinked.
If the extension doesn't contain a "*", then it is appended to the end of the current filename as a suffix. If the extension does contain one or more "*" characters, then each "*" is replaced with the current filename. In Perl terms, you could think of this as:
($backup = $extension) =~ s/\*/$file_name/g;
This allows you to add a prefix to the backup file, instead of (or in addition to) a suffix:
$ perl -pi'orig_*' -e 's/bar/baz/' fileA # backup to # 'orig_fileA'
Or even to place backup copies of the original files into another directory (provided the directory already exists):
$ perl -pi'old/*.orig' -e 's/bar/baz/' fileA # backup to # 'old/fileA.orig'
These sets of one-liners are equivalent:
$ perl -pi -e 's/bar/baz/' fileA # overwrite current file $ perl -pi'*' -e 's/bar/baz/' fileA # overwrite current file $ perl -pi'.orig' -e 's/bar/baz/' fileA # backup to 'fileA.orig' $ perl -pi'*.orig' -e 's/bar/baz/' fileA # backup to 'fileA.orig'
From the shell, saying
$ perl -p -i.orig -e "s/foo/bar/; ... "
is the same as using the program:
#!/usr/bin/perl -pi.orig s/foo/bar/;
which is equivalent to
#!/usr/bin/perl $extension = '.orig'; LINE: while (<>) { if ($ARGV ne $oldargv) { if ($extension !~ /\*/) { $backup = $ARGV . $extension; } else { ($backup = $extension) =~ s/\*/$ARGV/g; } rename($ARGV, $backup); open(ARGVOUT, ">$ARGV"); select(ARGVOUT); $oldargv = $ARGV; } s/foo/bar/; } continue { print; # this prints to original filename } select(STDOUT);
except that the -i form doesn't need to compare $ARGV to $oldargv to know when the filename has changed. It does, however, use ARGVOUT for the selected filehandle. Note that STDOUT is restored as the default output filehandle after the loop.
As shown above, Perl creates the backup file whether or not any output is actually changed. So this is just a fancy way to copy files:
$ perl -p -i'/some/file/path/*' -e 1 file1 file2 file3... or $ perl -p -i'.orig' -e 1 file1 file2 file3...
You can use "eof" without parentheses to locate the end of each input file, in case you want to append to each file, or reset line numbering (see example in "eof" in perlfunc).
If, for a given file, Perl is unable to create the backup file as specified in the extension then it will skip that file and continue on with the next one (if it exists).
For a discussion of issues surrounding file permissions and -i, see "Why does Perl let me delete read-only files? Why does -i clobber protected files? Isn't this a bug in Perl?" in perlfaq5.
You cannot use -i to create directories or to strip extensions from files.
Perl does not expand "~" in filenames, which is good, since some folks use it for their backup files:
$ perl -pi~ -e 's/foo/bar/' file1 file2 file3...
Note that because -i renames or deletes the original file before creating a new file of the same name, Unix-style soft and hard links will not be preserved.
Finally, the -i switch does not impede execution when no files are given on the command line. In this case, no backup is made (the original file cannot, of course, be determined) and processing proceeds from STDIN to STDOUT as might be expected.
perl -lpe 'substr($_, 80) = ""'
Note that the assignment "$\ = $/" is done when the switch is processed, so the input record separator can be different than the output record separator if the -l switch is followed by a -0 switch:
gnufind / -print0 | perl -ln0e 'print "found $_" if -p'
This sets "$\" to newline and then sets $/ to the null character.
-Mmodule executes "use" module ";" before executing your program. This loads the module and calls its "import" method, causing the module to have its default effect, typically importing subroutines or giving effect to a pragma. You can use quotes to add extra code after the module name, e.g., '-MMODULE qw(foo bar)'.
If the first character after the -M or -m is a dash (-) then the 'use' is replaced with 'no'. This makes no difference for -m.
A little builtin syntactic sugar means you can also say -m MODULE=foo,bar or -MMODULE =foo,bar as a shortcut for '-M MODULE qw(foo bar)'. This avoids the need to use quotes when importing symbols. The actual code generated by -MMODULE=foo,bar is "use module split(/,/,q{foo,bar})". Note that the "=" form removes the distinction between -m and -M; that is, -mMODULE=foo,bar is the same as -MMODULE=foo,bar.
A consequence of the "split" formulation is that -MMODULE=number never does a version check, unless "MODULE::import()" itself is set up to do a version check, which could happen for example if MODULE inherits from Exporter.
LINE: while (<>) { ... # your program goes here }
Note that the lines are not printed by default. See "-p" to have lines printed. If a file named by an argument cannot be opened for some reason, Perl warns you about it and moves on to the next file.
Also note that "<>" passes command line arguments to "open" in perlfunc, which doesn't necessarily interpret them as file names. See perlop for possible security implications.
Here is an efficient way to delete all files that haven't been modified for at least a week:
find . -mtime +7 -print | perl -nle unlink
This is faster than using the -exec switch of find because you don't have to start a process on every filename found (but it's not faster than using the -delete switch available in newer versions of find. It does suffer from the bug of mishandling newlines in pathnames, which you can fix if you follow the example under -0.
"BEGIN" and "END" blocks may be used to capture control before or after the implicit program loop, just as in awk.
LINE: while (<>) { ... # your program goes here } continue { print or die "-p destination: $!\n"; }
If a file named by an argument cannot be opened for some reason, Perl warns you about it, and moves on to the next file. Note that the lines are printed automatically. An error occurring during printing is treated as fatal. To suppress printing use the -n switch. A -p overrides a -n switch.
"BEGIN" and "END" blocks may be used to capture control before or after the implicit loop, just as in awk.
#!/usr/bin/perl -s if ($xyz) { print "$xyz\n" }
Do note that a switch like --help creates the variable "${-help}", which is not compliant with "use strict "refs"". Also, when using this option on a script with warnings enabled you may get a lot of spurious "used only once" warnings.
On some platforms, this also makes Perl append suffixes to the filename while searching for it. For example, on Win32 platforms, the ".bat" and ".cmd" suffixes are appended if a lookup for the original name fails, and if the name does not already end in one of those suffixes. If your Perl was compiled with "DEBUGGING" turned on, using the -Dp switch to Perl shows how the search progresses.
Typically this is used to emulate "#!" startup on platforms that don't support "#!". It's also convenient when debugging a script that uses "#!", and is thus normally found by the shell's $PATH search mechanism.
This example works on many platforms that have a shell compatible with Bourne shell:
#!/usr/bin/perl eval 'exec /usr/bin/perl -wS $0 ${1+"$@"}' if $running_under_some_shell;
The system ignores the first line and feeds the program to /bin/sh, which proceeds to try to execute the Perl program as a shell script. The shell executes the second line as a normal shell command, and thus starts up the Perl interpreter. On some systems $0 doesn't always contain the full pathname, so the -S tells Perl to search for the program if necessary. After Perl locates the program, it parses the lines and ignores them because the variable $running_under_some_shell is never true. If the program will be interpreted by csh, you will need to replace "${1+"$@"}" with $*, even though that doesn't understand embedded spaces (and such) in the argument list. To start up sh rather than csh, some systems may have to replace the "#!" line with a line containing just a colon, which will be politely ignored by Perl. Other systems can't control that, and need a totally devious construct that will work under any of csh, sh, or Perl, such as the following:
eval '(exit $?0)' && eval 'exec perl -wS $0 ${1+"$@"}' & eval 'exec /usr/bin/perl -wS $0 $argv:q' if $running_under_some_shell;
If the filename supplied contains directory separators (and so is an absolute or relative pathname), and if that file is not found, platforms that append file extensions will do so and try to look for the file with those extensions added, one by one.
On DOS-like platforms, if the program does not contain directory separators, it will first be searched for in the current directory before being searched for on the PATH. On Unix platforms, the program will be searched for strictly on the PATH.
Note: This is not a substitute for "-T" ! This is meant to be used only as a temporary development aid while securing legacy code: for real production code and for new secure code written from scratch, always use the real -T.
$ perl -V:libc libc='/lib/libc-2.2.4.so'; $ perl -V:lib. libs='-lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc'; libc='/lib/libc-2.2.4.so'; $ perl -V:lib.* libpth='/usr/local/lib /lib /usr/lib'; libs='-lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc'; lib_ext='.a'; libc='/lib/libc-2.2.4.so'; libperl='libperl.a'; ....
Additionally, extra colons can be used to control formatting. A trailing colon suppresses the linefeed and terminator ";", allowing you to embed queries into shell commands. (mnemonic: PATH separator ":".)
$ echo "compression-vars: " `perl -V:z.*: ` " are here !" compression-vars: zcat='' zip='zip' are here !
A leading colon removes the "name=" part of the response, this allows you to map to the name you need. (mnemonic: empty label)
$ echo "goodvfork="`./perl -Ilib -V::usevfork` goodvfork=false;
Leading and trailing colons can be used together if you need positional parameter values without the names. Note that in the case below, the "PERL_API" params are returned in alphabetical order.
$ echo building_on `perl -V::osname: -V::PERL_API_.*:` now building_on 'linux' '5' '1' '9' now
This switch really just enables the global $^W variable; normally, the lexically scoped "use warnings" pragma is preferred. You can disable or promote into fatal errors specific warnings using "__WARN__" hooks, as described in perlvar and "warn" in perlfunc. See also perldiag and perltrap. A fine-grained warning facility is also available if you want to manipulate entire classes of warnings; see warnings.
All references to line numbers by the program (warnings, errors, ...) will treat the "#!" line as the first line. Thus a warning on the 2nd line of the program, which is on the 100th line in the file will be reported as line 2, not as line 100. This can be overridden by using the "#line" directive. (See "Plain Old Comments (Not!)" in perlsyn)
If a directory name is specified, Perl will switch to that directory before running the program. The -x switch controls only the disposal of leading garbage. The program must be terminated with "__END__" if there is trailing garbage to be ignored; the program can process any or all of the trailing garbage via the "DATA" filehandle if desired.
The directory, if specified, must appear immediately following the -x with no intervening whitespace.
If PERL5LIB is not defined, PERLLIB is used. Directories are separated (like in PATH) by a colon on Unixish platforms and by a semicolon on Windows (the proper path separator being given by the command "perl -V:path_sep").
When running taint checks, either because the program was running setuid or setgid, or the -T or -t switch was specified, neither PERL5LIB nor PERLLIB is consulted. The program should instead say:
use lib "/my/directory";
It is conventional to start layer names with a colon (for example, ":perlio") to emphasize their similarity to variable "attributes". But the code that parses layer specification strings, which is also used to decode the PERLIO environment variable, treats the colon as a separator.
An unset or empty PERLIO is equivalent to the default set of layers for your platform; for example, ":unix:perlio" on Unix-like systems and ":unix:crlf" on Windows and other DOS-like systems.
The list becomes the default for all Perl's IO. Consequently only built-in layers can appear in this list, as external layers (such as ":encoding()") need IO in order to load them! See "open pragma" for how to add external encodings as defaults.
Layers it makes sense to include in the PERLIO environment variable are briefly summarized below. For more details see PerlIO.
Unlike in earlier versions of Perl, ":raw" is not just the inverse of ":crlf": other layers which would affect the binary nature of the stream are also removed or disabled.
The default set of layers should give acceptable results on all platforms
For Unix platforms that will be the equivalent of "unix perlio" or "stdio". Configure is set up to prefer the "stdio" implementation if the system's library provides for fast access to the buffer; otherwise, it uses the "unix perlio" implementation.
On Win32 the default in this release (5.14) is "unix crlf". Win32's "stdio" has a number of bugs/mis-features for Perl IO which are somewhat depending on the version and vendor of the C compiler. Using our own "crlf" layer as the buffer avoids those issues and makes things more uniform. The "crlf" layer provides CRLF conversion as well as buffering.
This release (5.14) uses "unix" as the bottom layer on Win32, and so still uses the C compiler's numeric file descriptor routines. There is an experimental native "win32" layer, which is expected to be enhanced and should eventually become the default under Win32.
The PERLIO environment variable is completely ignored when Perl is run in taint mode.
% env PERLIO_DEBUG=/tmp/perlio.log perl -Di script ...
and under Win32, the approximately equivalent:
> set PERLIO_DEBUG=CON perl -Di script ...
This functionality is disabled for setuid scripts, for scripts run with -T, and for scripts run on a Perl built without "-DDEBUGGING" support.
The PERLLIB environment variable is completely ignored when Perl is run in taint mode.
BEGIN { require "perl5db.pl" }
The PERL5DB environment variable is only used when Perl is started with a bare -d switch.
Note that Perl doesn't use COMSPEC for this purpose because COMSPEC has a high degree of variability among users, leading to portability concerns. Besides, Perl can use a shell that may not be fit for interactive use, and setting COMSPEC to such a shell may interfere with the proper functioning of other programs (which usually look in COMSPEC to find a shell fit for interactive use).
Before Perl 5.10.0 and 5.8.8, PERL5SHELL was not taint checked when running external commands. It is recommended that you explicitly set (or delete) $ENV{PERL5SHELL} when running in taint mode under Windows.
Setting this environment variable to 1 means that Perl will simply use the first suitable LSP enumerated in the catalog, which keeps McAfee Guardian happy--and in that particular case Perl still works too because McAfee Guardian's LSP actually plays other games which allow applications requiring IFS compatibility to work.
If set, this dumps out memory statistics after execution. If set to an integer greater than one, also dumps out memory statistics after compilation.
If the option is provided, and "PERL_PERTURB_KEYS" is NOT set, then a value of '0' implies "PERL_PERTURB_KEYS=0" and any other value implies "PERL_PERTURB_KEYS=2".
PLEASE NOTE: The hash seed is sensitive information. Hashes are randomized to protect against local and remote attacks against Perl code. By manually setting a seed, this protection may be partially or completely lost.
See "Algorithmic Complexity Attacks" in perlsec, "PERL_PERTURB_KEYS", and "PERL_HASH_SEED_DEBUG" for more information.
When set to "1" or "RANDOM" then traversing keys will be randomized. Every time a hash is inserted into the key order will change in a random fashion. The order may not be repeatable in a following program run even if the PERL_HASH_SEED has been specified. This is the default mode for perl.
When set to "2" or "DETERMINISTIC" then inserting keys into a hash will cause the key order to change, but in a way that is repeatable from program run to program run.
NOTE: Use of this option is considered insecure, and is intended only for debugging non-deterministic behavior in Perl's hash function. Do not use it in production.
See "Algorithmic Complexity Attacks" in perlsec and "PERL_HASH_SEED" and "PERL_HASH_SEED_DEBUG" for more information. You can get and set the key traversal mask for a specific hash by using the "hash_traversal_mask()" function from Hash::Util.
Note that any information about the hash function, especially the hash seed is sensitive information: by knowing it, one can craft a denial-of-service attack against Perl code, even remotely; see "Algorithmic Complexity Attacks" in perlsec for more information. Do not disclose the hash seed to people who don't need to know it. See also "hash_seed()" and "key_traversal_mask()" in Hash::Util.
An example output might be:
HASH_FUNCTION = ONE_AT_A_TIME_HARD HASH_SEED = 0x652e9b9349a7a032 PERTURB_KEYS = 1 (RANDOM)
$ 3>foo3 PERL_MEM_LOG=3m perl ...
Ignored if perl is run setuid or setgid. Used only for some limited startup randomization (hash keys) if "-T" or "-t" perl is started with tainting enabled.
Perl may be built to ignore this variable.
Perl also has environment variables that control how Perl handles data specific to particular natural languages; see perllocale.
Perl and its various modules and components, including its test frameworks, may sometimes make use of certain other environment variables. Some of these are specific to a particular platform. Please consult the appropriate module documentation and any documentation for your platform (like perlsolaris, perllinux, perlmacosx, perlwin32, etc) for variables peculiar to those specific situations.
Perl makes all environment variables available to the program being executed, and passes these along to any child processes it starts. However, programs running setuid would do well to execute the following lines before doing anything else, just to keep people honest:
$ENV{PATH} = "/bin:/usr/bin"; # or whatever you need $ENV{SHELL} = "/bin/sh" if exists $ENV{SHELL}; delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
| 2019-10-21 | perl v5.30.3 |


Class: Net_Socket
Source Location: /library/pear/Net/Socket.php
Class Overview
PEAR | --Net_Socket
Author(s):
- Stig Bakken<EMAIL_ADDRESS>- Chuck Hagenbuch<EMAIL_ADDRESS>Version:
- 1.1
Copyright:
- 1997-2005 The PHP Group
Inherited Variables
Inherited Methods
Class: PEAR
- PEAR::PEAR()
- Constructor. Registers this object in $_PEAR_destructor_object_list for destructor emulation if a destructor object exists.
- PEAR::delExpect()
- This method deletes all occurences of the specified element from the expected error codes stack.
- PEAR::expectError()
- This method is used to tell which errors you expect to get.
- PEAR::getStaticProperty()
- If you have a class that's mostly/entirely static, and you need static
- PEAR::isError()
- Tell whether a value is a PEAR error.
- PEAR::loadExtension()
- OS independant PHP extension load. Remember to take care on the correct extension name for case sensitive OSes.
- PEAR::popErrorHandling()
- Pop the last error handler used
- PEAR::popExpect()
- This method pops one element off the expected error codes stack.
- PEAR::pushErrorHandling()
- Push a new error handler on top of the error handler options stack. With this you can easily override the actual error handler for some code and restore it later with popErrorHandling.
- PEAR::raiseError()
- This method is a wrapper that returns an instance of the configured error class with this object's default error handling applied. If the $mode and $options parameters are not specified, the object's defaults are used.
- PEAR::registerShutdownFunc()
- Use this function to register a shutdown method for static classes.
- PEAR::setErrorHandling()
- Sets how errors generated by this object should be handled.
- PEAR::staticPopErrorHandling()
- PEAR::staticPushErrorHandling()
- PEAR::throwError()
- Simpler form of raiseError with fewer options. In most cases message, code and userinfo are enough.
- PEAR::_checkDelExpect()
- This method checks unsets an error code if available
- PEAR::_PEAR()
- Destructor (the emulated type of...). Does nothing right now, but is included for forward compatibility, so subclass destructors should always call it.
Class Details
Generalized Socket class.
Tags:
| author: | Stig Bakken<EMAIL_ADDRESS>|
| author: | Chuck Hagenbuch<EMAIL_ADDRESS>|
| version: | 1.1 |
| copyright: | 1997-2005 The PHP Group |
[ Top ]
Class Variables
$addr = ''
[line 59]
The IP address to connect to.
| Type: | string |
$blocking = true
[line 47]
Whether the socket is blocking. Defaults to true.
| Type: | boolean |
$fp = null
[line 41]
Socket file pointer.
| Type: | resource |
$lineLength = 2048
[line 79]
Number of bytes to read at a time in readLine() and readAll(). Defaults to 2048.
| Type: | integer |
$persistent = false
[line 53]
Whether the socket is persistent. Defaults to false.
| Type: | boolean |
$port = 0
[line 65]
The port number to connect to.
| Type: | integer |
$timeout = false
[line 72]
Number of seconds to wait on socket connections before assuming there's no more data. Defaults to no timeout.
| Type: | integer |
Class Methods
method connect [line 97]
boolean connect(
string
$addr, [integer
$port = 0], [boolean
$persistent = null], [integer
$timeout = null], [array
$options = null])
Connect to the specified port. If called when the socket is already connected, it disconnects and connects again.
Tags:
| return: | | PEAR_Error True on success or a PEAR_Error on failure. |
| access: | public |
Parameters:
| string | $addr | IP address or host name. |
| integer | $port | TCP port number. |
| boolean | $persistent | (optional) Whether the connection is persistent (kept open between requests by the web server). |
| integer | $timeout | (optional) How long to wait for data. |
| array | $options | See options for stream_context_create. |
[ Top ]
method disconnect [line 157]
mixed disconnect(
)
Disconnects from the peer, closes the socket.
Tags:
| return: | true on success or an error object otherwise |
| access: | public |
[ Top ]
method eof [line 335]
bool eof(
)
Tests for end-of-file on a socket descriptor.
Tags:
| access: | public |
[ Top ]
method gets [line 248]
$size gets(
$size)
Get a specified line of data
Tags:
| return: | bytes of data from the socket, or a PEAR_Error if not connected. |
| access: | public |
Parameters:
| $size |
[ Top ]
method getStatus [line 232]
mixed getStatus(
)
Returns information about an existing socket resource.
Currently returns four entries in the result array:<p> timed_out (bool) - The socket timed out waiting for data blocked (bool) - The socket was blocked eof (bool) - Indicates EOF event unread_bytes (int) - Number of bytes left in the socket buffer </p>Tags:
| return: | Array containing information about existing socket resource or an error object otherwise |
| access: | public |
[ Top ]
method isBlocking [line 174]
boolean isBlocking(
)
Find out if the socket is in blocking mode.
Tags:
| return: | The current blocking mode. |
| access: | public |
[ Top ]
method read [line 268]
$size read(
integer
$size)
Read a specified amount of data. This is guaranteed to return, and has the added benefit of getting everything in one fread() chunk; if you know the size of the data you're getting beforehand, this is definitely the way to go.
Tags:
| return: | bytes of data from the socket, or a PEAR_Error if not connected. |
| access: | public |
Parameters:
| integer | $size | The number of bytes to read from the socket. |
[ Top ]
method readAll [line 468]
string readAll(
)
Read until the socket closes, or until there is no more data in
the inner PHP buffer. If the inner buffer is empty, in blocking mode we wait for at least 1 byte of data. Therefore, in blocking mode, if there is no data at all to be read, this function will never exit (unless the socket is closed on the remote end).Tags:
| return: | All data until the socket closes, or a PEAR_Error if not connected. |
| access: | public |
[ Top ]
method readByte [line 347]
1 readByte(
)
Reads a byte of data
Tags:
| return: | byte of data from the socket, or a PEAR_Error if not connected. |
| access: | public |
[ Top ]
method readInt [line 380]
integer readInt(
)
Reads an int of data
Tags:
| return: | 1 int of data from the socket, or a PEAR_Error if not connected. |
| access: | public |
[ Top ]
method readIPAddress [line 418]
Dot readIPAddress(
)
Reads an IP Address and returns it in a dot formated string
Tags:
| return: | formated string, or a PEAR_Error if not connected. |
| access: | public |
[ Top ]
method readLine [line 438]
All readLine(
)
Read until either the end of the socket or a newline, whichever comes first. Strips the trailing newline from the returned data.
Tags:
| return: | available data up to a newline, without that newline, or until the end of the socket, or a PEAR_Error if not connected. |
| access: | public |
[ Top ]
method readString [line 398]
string, readString(
)
Reads a zero-terminated string of data
Tags:
| return: | or a PEAR_Error if not connected. |
| access: | public |
[ Top ]
method readWord [line 363]
1 readWord(
)
Reads a word of data
Tags:
| return: | word of data from the socket, or a PEAR_Error if not connected. |
| access: | public |
[ Top ]
method select [line 493]
False select(
integer
$state, integer
$tv_sec, [integer
$tv_usec = 0])
Runs the equivalent of the select() system call on the socket with a timeout specified by tv_sec and tv_usec.
Tags:
| return: | if select fails, integer describing which of read/write/error are ready, or PEAR_Error if not connected. |
| access: | public |
Parameters:
| integer | $state | Which of read/write/error to check for. |
| integer | $tv_sec | Number of seconds for timeout. |
| integer | $tv_usec | Number of microseconds for timeout. |
[ Top ]
method setBlocking [line 189]
mixed setBlocking(
boolean
$mode)
Sets whether the socket connection should be blocking or not. A read call to a non-blocking socket will return immediately if there is no data available, whereas it will block until there is data for blocking sockets.
Tags:
| return: | true on success or an error object otherwise |
| access: | public |
Parameters:
| boolean | $mode | True for blocking sockets, false for nonblocking. |
[ Top ]
method setTimeout [line 209]
mixed setTimeout(
integer
$seconds, integer
$microseconds)
Sets the timeout value on socket descriptor, expressed in the sum of seconds and microseconds
Tags:
| return: | true on success or an error object otherwise |
| access: | public |
Parameters:
| integer | $seconds | Seconds. |
| integer | $microseconds | Microseconds. |
[ Top ]
method write [line 287]
mixed write(
string
$data, [integer
$blocksize = null])
Write a specified amount of data.
Tags:
| return: | true on success or an error object otherwise |
| access: | public |
Parameters:
| string | $data | Data to write. |
| integer | $blocksize | Amount of data to write at once. NULL means all at once. |
[ Top ]
method writeLine [line 320]
mixed writeLine(
$data)
Write a line of data to the socket, followed by a trailing "\r\n".
Tags:
| return: | fputs result, or an error |
| access: | public |
Parameters:
| $data |
[ Top ]


中国
独秀群芳 传承百年
摁一下全屏
空格键，方向键或Tab键控制幻灯片。
In China most of the world
1 The world's longest city wall, the Great Wall of China.
2 The youngest of the world's highest plateau, the qinghai-tibet plateau.
3 The world's largest loess landform, the loess plateau in China
4 The largest desert in the world mobile dune area percentage, the taklamakan desert
Chinese opera facial makeup is exclusive to the han nationality traditional opera, is different from any other countries theatrical makeup modelling art. Opera face each historical figure or a certain type of person has an approximate spectral type, like singing, playing music should be according to the score, the so called "facebook".
The glorious history of China
On December 20, 1999, Macao's return to the motherland
On July 13, 2001, Beijing's bid to host the 2008 Olympic Games a success
On July 1, 1997, Hong Kong's return to the motherland
On April 24, 1970, the first man-made satellite (dongfanghong 1) successful launch
On October 16, 1964, the first atomic bomb explosion success
On November 20, 1999, shenzhou spacecraft assumption
Tea, is the national drink of the Chinese nation. It sincerely shennong, the smell of the duke of zhou lu, xing in the tang dynasty, sheng in the song dynasty, tea tree native to southwest in our country, our country is the earliest discovered tea and the tea in the world, China is the birthplace of tea, known as "tea of the motherland". Tea is the pride of the Chinese nation!
The Vatican City State（梵蒂冈）
The Catholic center in the world
beautiful scenery
beautiful scenery
The Vatican is a country, also can saying is just a city, it is surrounded by Italy. The Vatican is the seat of the Roman Catholic church, the former is the shrine of catholics, since 1929,
was established as a sovereign state, countries have adopted the theocracy system.
The Vatican Italy can be said to be China, for all sides surrounded by Italy, so the people of the Vatican in more for Italian. And because the Vatican integration of church and state,
the national church workers. The Vatican is the smallest country in the world, with an area of only 0.44 square kilometers, is equal to the width of more than 660 meters,
ordinary people walks from head to tail is about 15 minutes.
结 束


Name:
Username:
Password:
Mentor:
Grade:
Favorite Subject:
Favorite Sport:
Favorite TV Show:
Word that describes you:
Picture:


OCaml-TLS: the protocol implementation and mitigations to known attacks
This is the fifth in a series of posts that introduce new libraries for a pure OCaml implementation of TLS. You might like to begin with the introduction.
ocaml-tls is the new, clean-slate implementation of TLS in OCaml that we’ve been working on for the past six months. In this post we try to document some of its internal design, the reasons for the decisions we made, and the current security status of that work. Try our live interactive demonstration server which visualises TLS sessions.
The OCaml-TLS architecture
The OCaml ecosystem has several distinct ways of interacting with the outside world (and the network in particular): straightforward unix interfaces and the asynchronous programming libraries lwt and async. One of the early considerations was not to restrict ourselves to any of those – we wanted to support them all.
There were also two distinct basic “platforms” we wanted to target from the
outset: the case of a simple executable, and the case of Mirage
unikernels.
So one of the first questions we faced was deciding how to represent interactions with the network in a portable way. This can be done by systematically abstracting out the API boundary which gives access to network operations, but we had a third thing in mind as well: we wanted to exploit the functional nature of OCaml to its fullest extent!
Our various prior experiences with Haskell and Idris convinced us to adopt what is called “purely functional” technique. We believe it to be an approach which first forces the programmer to give principled answers to all the difficult design questions (errors and global data-flow) in advance, and then leads to far cleaner and composable code later on. A purely functional system has all the data paths made completely explicit in the form of function arguments and results. There are no unaccounted-for interactions between components mediated by shared state, and all the activity of the parts of the system is exposed through types since, after all, it’s only about computing values from values.
For these reasons, the library is split into two parts: the directory /lib
(and the corresponding findlib package tls
) contains the core TLS logic, and
/mirage
and /lwt
(packaged as tls.mirage
and tls.lwt
respectively)
contain front-ends that tie the core to Mirage
and Lwt_unix
.
Core
The core library is purely functional. A TLS session is represented by the
abstract type Tls.Engine.state
, and various functions consume this session
type together with raw bytes (Cstruct.t
– which is by itself mutable, but
ocaml-tls
eschews this) and produce new session values and resulting buffers.
The central entry point is handle_tls, which transforms an input state and a buffer to an output state, a (possibly empty) buffer to send to the communication partner, and an optional buffer of data intended to be received by the application:
type state
type ret = [
| `Ok of [ `Ok of state | `Eof | `Alert of alert ] *
[ `Response of Cstruct.t ] * [ `Data of Cstruct.t option ]
| `Fail of alert * [ `Response of Cstruct.t ]
]
val handle_tls : state -> Cstruct.t -> ret
As the signature shows, errors are signalled through the ret
type, which is a polymorphic variant. This
reflects the actual internal structure: all the errors are represented as
values, and operations are composed using an error monad.
Other entry points share the same basic behaviour: they transform the prior state and input bytes into the later state and output bytes.
Here’s a rough outline of what happens in handle_tls
:
-
TLS packets consist of a header, which contains the protocol version, length, and content type, and the payload of the given content type. Once inside our main handler, we separate the buffer into TLS records, and process each individually. We first check that the version number is correct, then decrypt, and verify the mac.
-
Decrypted data is then dispatched to one of four sub-protocol handlers (Handshake, Change Cipher Spec, Alert and Application Data). Each handler can return a new handshake state, outgoing data, application data, the new decryption state or an error (with the outgoing data being an interleaved list of buffers and new encryption states).
-
The outgoing buffers and the encryption states are traversed to produce the final output to be sent to the communication partner, and the final encryption, decryption and handshake states are combined into a new overall state which is returned to the caller.
Handshake is (by far) the most complex TLS sub-protocol, with an elaborate state machine. Our client and server encode this state as a “flat” sum type, with exactly one incoming message allowed per state. The handlers first parse the handshake packet (which fails in case of malformed or unknown data) and then dispatch it to the handling function. The handshake state is carried around and a fresh one is returned from the handler in case it needs updates. It consists of a protocol version, the handshake state, configuration, renegotiation data, and possibly a handshake fragment.
Logic of both handshake handlers is very localised, and does not mutate any global data structures.
Core API
OCaml permits the implementation a module to be exported via a more abstract signature that hides the internal representation details. Our public API for the core library consists of the Tls.Engine and Tls.Config modules.
Tls.Engine
contains the basic reactive function handle_tls
, mentioned above,
which processes incoming data and optionally produces a response, together with
several operations that allow one to initiate message transfer like
send_application_data
(which processes application-level messages for
sending), send_close_notify
(for sending the ending message) and reneg
(which initiates full TLS renegotiation).
The module also contains the only two ways to obtain the initial state:
val client : Config.client -> (state * Cstruct.t)
val server : Config.server -> state
That is, one needs a configuration value to create it. The Cstruct.t
that client
emits is the initial Client Hello since in TLS,
the client starts the session.
Tls.Config
synthesizes configurations, separately for client and server
endpoints, through the functions client_exn
and server_exn
. They take a
number of parameters that define a TLS session, check them for consistency, and
return the sanitized config
value which can be used to create a state
and,
thus, a session. If the check fails, they raise an exception.
The parameters include the pair of a certificate and its private key for the
server, and an X509.Authenticator.t
for the client, both produced by our
ocaml-x509 library and described in a previous article.
This design reflects our attempts to make the API as close to “fire and forget” as we could, given the complexity of TLS: we wanted the library to be relatively straightforward to use, have a minimal API footprint and, above all, fail very early and very loudly when misconfigured.
Effectful front-ends
Clearly, reading and writing network data does change the state of the world.
Having a pure value describing the state of a TLS session is not really useful
once we write something onto the network; it is certainly not the case that we
can use more than one distinct state
to process further data, as only one
value is in sync with the other endpoint at any given time.
Therefore we wrap the core types into stateful structures loosely inspired by
sockets and provide IO operations on those. The structures of mirage
and lwt
front-ends mirror one another.
In both cases, the structure is pull-based in the sense that no processing is done until the client requires a read, as opposed to a callback-driven design where the client registers a callback and the library starts spinning in a listening loop and invoking it as soon as there is data to be processed. We do this because in an asynchronous context, it is easy to create a callback-driven interface from a demand-driven one, but the opposite is possible only with unbounded buffering of incoming data.
One exception to demand-driven design is the initial session creation: the library will only yield the connection after the first handshake is over, ensuring the invariant that it is impossible to interact with a connection if it hasn’t already been fully established.
Mirage
The Mirage
interface matches the FLOW
signature (with additional TLS-specific operations). We provide a functor that
needs to be applied to an underlying TCP module, to obtain a TLS transport on
top. For example:
module Server (Stack: STACKV4) (Entropy: ENTROPY) (KV: KV_RO) =
struct
module TLS = Tls_mirage.Make (Stack.TCPV4) (Entropy)
module X509 = Tls_mirage.X509 (KV) (Clock)
let accept conf flow =
TLS.server_of_tcp_flow conf flow >>= function
| `Ok tls ->
TLS.read tls >>= function
| `Ok buf ->
TLS.write tls buf >>= fun () -> TLS.close buf
let start stack e kv =
TLS.attach_entropy e >>= fun () ->
lwt authenticator = X509.authenticator kv `Default in
let conf = Tls.Config.server_exn ~authenticator () in
Stack.listen_tcpv4 stack 4433 (accept conf) ;
Stack.listen stack
end
Lwt
The lwt
interface has two layers. Tls_lwt.Unix
is loosely based
on read/write operations from Lwt_unix
and provides in-place update of
buffers. read
, for example, takes a Cstruct.t
to write into and returns the
number of bytes read. The surrounding module, Tls_lwt
, provides a simpler,
Lwt_io
-compatible API built on top:
let main host port =
Tls_lwt.rng_init () >>= fun () ->
lwt authenticator = X509_lwt.authenticator (`Ca_dir nss_trusted_ca_dir) in
lwt (ic, oc) = Tls_lwt.connect ~authenticator (host, port) in
let req = String.concat "\r\n" [
"GET / HTTP/1.1" ; "Host: " ^ host ; "Connection: close" ; "" ; ""
] in
Lwt_io.(write oc req >>= fun () -> read ic >>= print)
We have further plans to provide wrappers for Async
and plain Unix
in a
similar vein.
Attacks on TLS
TLS the most widely deployed security protocol on the Internet and, at over 15 years, is also showing its age. As such, a flaw is a valuable commodity due to the commercially sensitive nature of data that is encrypted with TLS. Various vulnerabilities on different layers of TLS have been found - heartbleed and others are implementation specific, advancements in cryptanalysis such as collisions of MD5 lead to vulnerabilities, and even others are due to incorrect usage of TLS (truncation attack or BREACH). Finally, some weaknesses are in the protocol itself. Extensive overviews of attacks on TLS are available.
We look at protocol level attacks of TLS and how ocaml-tls implements mitigations against these. TLS 1.2 RFC provides an overview of attacks and mitigations, and we track our progress in covering them. This is slightly out of date as the RFC is roughly six years old and in the meantime more attacks have been published, such as the renegotiation flaw.
As already mentioned, we track all our mitigated and open security issues on our GitHub issue tracker.
Due to the choice of using OCaml, a memory managed programming language, we obstruct entire bug classes, namely temporal and spatial memory safety.
Cryptanalysis and improvement of computational power weaken some ciphers, such as RC4 and 3DES (see issue 8 and issue 10). If we phase these two ciphers out, there wouldn’t be any matching ciphersuite left to communicate with some compliant TLS-1.0 implementations, such as Windows XP, that do not support AES.
Timing attacks
When the timing characteristics between the common case and the error case are different, this might potentially leak confidential information. Timing is a very prominent side-channel and there are a huge variety of timing attacks on different layers, which are observable by different attackers. Small differences in timing behaviour might initially be exploitable only by a local attacker, but advancements to the attack (e.g. increasing the number of tests) might allow a remote attacker to filter the noise and exploit the different timing behaviour.
Timing of cryptographic primitives
We already mentioned cache timing attacks on our AES implementation, and that we use blinding techniques to mitigate RSA timing attacks.
By using a memory managed programming language, we open the attack vector of garbage collector (GC) timing attacks (also mentioned in our nocrypto introduction).
Furthermore, research has been done on virtual machine side channels (l3, cross vm and cache timing), which we will need to study and mitigate appropriately.
For the time being we suggest to not use the stack on a multi-tenant shared host or on a shared host which malicious users might have access to.
Bleichenbacher
In 1998, Daniel Bleichenbacher discovered a timing flaw in the PKCS1 encoding of the premaster secret: the TLS server failed faster when the padding was wrong than when the decryption failed. Using this timing, an attacker can run an adaptive chosen ciphertext attack and find out the plain text of a PKCS1 encrypted message. In TLS, when RSA is used as the key exchange method, this leads to discovery of the premaster secret, which is used to derive the keys for the current session.
The mitigation is to have both padding and decryption failures use the exact same amount of time, thus there should not be any data-dependent branches or different memory access patterns in the code. We implemented this mitigation in Handshake_server.
Padding oracle and CBC timing
Vaudenay discovered a vulnerability involving block ciphers: if an attacker can distinguish between bad mac and bad padding, recovery of the plaintext is possible (within an adaptive chosen ciphertext attack). Another approach using the same issue is to use timing information instead of separate error messages. Further details are described here.
The countermeasure, which we implement here, is to continue
with the mac computation even though the padding is
incorrect. Furthermore, we send the same alert (bad_record_mac
)
independent of whether the padding is malformed or the mac is
incorrect.
Lucky 13
An advancement of the CBC timing attack was discovered in 2013, named Lucky 13. Due to the fact that the mac is computed over the plaintext without padding, there is a slight (but measurable) difference in timing between computing the mac of the plaintext and computing the fake mac of the ciphertext. This leaks information. We do not have proper mitigation against Lucky 13 in place yet. You can find further discussion in issue 7 and pull request 49.
Renegotiation not authenticated
In 2009, Marsh Ray published a vulnerability of the TLS protocol which lets an attacker prepend arbitrary data to a session due to unauthenticated renegotiation. The attack exploits the fact that a renegotiation of ciphers and key material is possible within a session, and this renegotiated handshake is not authenticated by the previous handshake. A man in the middle can initiate a session with a server, send some data, and hand over the session to a client. Neither the client nor the server can detect the man in the middle.
A fix for this issue is the secure renegotiation extension, which embeds authenticated data of the previous handshake into the client and server hello messages. Now, if a man in the middle initiates a renegotiation, the server will not complete it due to missing authentication data (the client believes this is the first handshake).
We implement and require the secure renegotiation extension by
default, but it is possible to configure ocaml-tls
to not require
it – to be able to communicate with servers and
clients which do not support this extension.
Implementation of the mitigation is on the server side in
ensure_reneg and on the client side in validate_reneg. The
data required for the secure renegotiation is stored in
handshake_state
while sending and receiving Finished
messages. You can find further discussion in issue 3.
TLS 1.0 and known-plaintext (BEAST)
TLS 1.0 reuses the last ciphertext block as IV in CBC mode. If an attacker has a (partially) known plaintext, she can find the remaining plaintext. This is known as the BEAST attack and there is a long discussion about mitigations. Our mitigation is to prepend each TLS-1.0 application data fragment with an empty fragment to randomize the IV. We do this exactly here. There is further discussion in issue 2.
Our mitigation is slightly different from the 1/n-1 splitting proposed here: we split every application data frame into a 0 byte and n byte frame, whereas they split into a 1 byte and a n-1 byte frame.
Researchers have exploited this vulnerability in 2011, although it was known since 2006. TLS versions 1.1 and 1.2 use an explicit IV, instead of reusing the last cipher block on the wire.
Compression and information leakage (CRIME)
When using compression on a chosen-plaintext, encrypting this can leak information, known as CRIME. BREACH furthermore exploits application layer compression, such as HTTP compression. We mitigate CRIME by not providing any TLS compression support, while we cannot do anything to mitigate BREACH.
Traffic analysis
Due to limited amount of padding data, the actual size of transmitted data can be recovered. The mitigation is to implement length hiding policies. This is tracked as issue 162.
Version rollback
SSL-2.0 is insecure, a man in the middle can downgrade the version to SSL-2.0. The mitigation we implement is that we do not support SSL-2.0, and thus cannot be downgraded. Also, we check that the version of the client hello matches the first two bytes in the premaster secret here. You can find further discussion in issue 5.
Triple handshake
A vulnerability including session resumption and renegotiation was discovered by the miTLS team, named triple handshake. Mitigations include disallowing renegotiation, disallowing modification of the certificate during renegotiation, or a hello extension. Since we do not support session resumption yet, we have not yet implemented any of the mentioned mitigations. There is further discussion in issue 9.
Alert attack
A fragment of an alert can be sent by a man in the middle during the initial handshake. If the fragment is not cleared once the handshake is finished, the authentication of alerts is broken. This was discovered in 2012; our mitigation is to discard fragmented alerts.
EOF.
Within six months, two hackers managed to develop a clean-slate TLS stack, together with required crypto primitives, ASN.1, and X.509 handling, in a high-level pure language. We interoperate with widely deployed TLS stacks, as shown by our demo server. The code size is nearly two orders of magnitude smaller than OpenSSL, the most widely used open source library (written in C, which a lot of programming languages wrap instead of providing their own TLS implementation). Our code base seems to be robust – the demo server successfully finished over 22500 sessions in less than a week, with only 11 failing traces.
There is a huge need for high quality TLS implementations, because several TLS implementations suffered this year from severe security problems, such as heartbleed, goto fail, session id, Bleichenbacher, change cipher suite and GCM DoS. The main cause is implementation complexity due to lack of abstraction, and memory safety issues.
We still need to address some security issues, and improve our performance. We invite people to do rigorous code audits (both manual and automated) and try testing our code in their services.
Please be aware that this release is a beta and is missing external code audits. It is not yet intended for use in any security critical applications.
Acknowledgements
Since this is the final post in our series, we would like to thank all people who reported issues so far: Anil Madhavapeddy, Török Edwin, Daniel Bünzli, Andreas Bogk, Gregor Kopf, Graham Steel, Jerome Vouillon, Amir Chaudhry, Ashish Agarwal. Additionally, we want to thank the miTLS team (especially Cedric and Karthikeyan) for fruitful discussions, as well as the OCaml Labs and Mirage teams. And thanks to Peter Sewell and Richard Mortier for funding within the REMS, UCN, and Horizon projects. The software was started in Aftas beach house in Mirleft, Morocco.
Posts in this TLS series:
- Irmin 0.12 with portable filesystem watching released
- MirageOS Summer 2016 Hackathon roundup
- MirageOS Summer 2016 Hackathon announcement, and talk roundup
- MirageOS Spring 2016 Hackathon!
- MirageOS security advisory 00: mirage-net-xen
- Introducing Functoria
- Run Mirage Unikernels on KVM/QEMU with Solo5
- MirageOS hackathon
- Introducing Charrua — a DHCP implementation
- Unikernel.org


Forum: Public ReviewIssue: EVAL-WHEN-OBSOLETE-KEYWORDS
References: Moon's public review comment #2 (X3J13/92-3202)
Issue EVAL-WHEN-NON-TOP-LEVEL
Category: CHANGE
Edit history: 20 Dec 1992, Version 1 by Loosemore
08 Jan 1993, Version 2 by Loosemore (comments from Moon)
12 Sept 1993, Version 3 by Loosemore (add second proposal)
Status: Proposal REMOVE-OLD-KEYWORDS failed 3-6-0, March 1993
Proposal X3J13-MARCH-1993 passed 6-1, March 1993
Problem description:
The language in draft 12.24 about the relationship between
the new EVAL-WHEN keywords :EXECUTE, :COMPILE-TOPLEVEL, and
:LOAD-TOPLEVEL and the old, deprecated keywords EVAL, COMPILE, and
LOAD is confusing. (For example, it's inconsistent about what
EVAL means when not at top level.) The compatibility argument
for retaining two sets of keywords with slightly different
meanings is outweighed by the resulting confusion.
Proposal (EVAL-WHEN-OBSOLETE-KEYWORDS:REMOVE-OLD-KEYWORDS):
Eliminate all mention of the old keywords EVAL, COMPILE, and LOAD
in connection with EVAL-WHEN.
Proposal (EVAL-WHEN-OBSOLETE-KEYWORDS:X3J13-MARCH-1993):
Generalize the use of the old keywords; define that eval-when always
treats eval the same as :execute, compile the same as :compile-toplevel,
and load the same as :load-toplevel.
Rationale:
Adopting this suggestion would shorten the specification and remove
an inconsistency. There are so many incompatibilities between
CLtL1, CLtL2, and the draft standard that it's pointless to try to
allow for a modeless implementation that is simultaneously compatible
with all of them.
Current practice:
Moon says he's not aware of any implementations whose current releases
don't support the new keywords.
Cost to implementors:
Probably small; implementations could continue to support the old
keywords as an extension.
Cost to users:
This is an incompatible change, but one that is easy to detect. A
good implementation should complain about unrecognized EVAL-WHEN
keywords, anyway.
Aesthetics:
Making the standard simpler is a good thing.
Editorial impact:
The dictionary entry for EVAL-WHEN is the only place affected.
(The discussion of EVAL-WHEN in the concept section relating to
file compilation uses the new keywords exclusively.)
Discussion:
Moon says:
I like EVAL-WHEN-NON-TOP-LEVEL:REMOVE-OLD-KEYWORDS (version 1).
Moon suggested (in his public review comment) that another way to
resolve this problem would be to define the behavior of the old
keywords to be exactly the same as that of the new keywords.


The OpenRM Scene Graph implements a scene graph API using OpenGL for hardware accelerated rendering. OpenRM is intended to be used to construct high performance, portable graphics and scientific visualization applications on Unix/Linux/Windows platforms.
lispbuilder-openrm provides the bindings and utility functions necessary to integrate OpenRM with Common Lisp.
The lispbuilder-openrm relies on the OpenGL libraries being installed on the host OS. Although lispbuilder-openrm itself does not require lispbuilder-sdl, several of the OpenRM examples require SDL to manage the window, key handling and creation of the OpenGL context. OpenRM itself provides support for this, but this has not yet been tested in these bindings.
The lispbuilder-openrm package is a part of the Common Lisp Application Builder project which is in turn a part of the umbrella Common Lisp Gardeners project.
The purpose of this document is to describe a step-by-step process by which the user may install, configure and begin using the lispbuilder-openrm package to create applications using one of the supported Lisp implementations.
The list of credits is contained in CONTRIBUTORS. A copy of the license is contained in COPYING.
The following table describes the status of the Lisp implementations that have been tested with lispbuilder-openrm:
| Lisp Implementation | lispbuilder-openrm Status | Comments | ||
| Win32 | Linux | MacOS | ||
| CLISP v2.38 | Working | Unknown | Unknown | |
| Lispworks v2.4.6 Personal | Working | Unknown | Unknown | As of [03/30/06], use the 'CFFI-060214' tarball for Windows as more recent versions do not work. |
| Allegro Trial Edition 7.0 | Unknown | Unknown | Unknown | |
| OpenMCL | NA | NA | Unknown | |
| SBCL | When, oh when? | Working | Unknown | |
| CMUCL | When already? | Unknown | NA | Justin WIP Debian (sarge) |
The packages asdf.lisp, CFFI and lispbuilder-sdl must be installed prior to the installation of lispbuilder-openrm. The installation procedures for these pacakges are described in the lispbuilder-sdl documentation.
In addition the the lisp packages described above, OpenRM for Windows requires the PThreads Win32 libraries, as well as the Windows specific msvcr70.dll and msvcrtd.dll libraries. All these binaries are included in the lispbuilder-openrm-binaries package on sourceforge. The installation instructions for this package are described below. The Linux installation does not require these additional binaries.
This section describes the installation of the lispbuilder-openrm package.
c:\programme\clisp-2.38\asdf\lispbuilder-openrm_0.1\lispbuilder-openrm.asd
This section describes the installation of the OpenRM binaries for Windows.
c:\programme\clisp-2.38\asdf\lispbuilder-openrm\lispbuilder-openrm-binaries.asd
c:\programme\clisp-2.38\asdf\lispbuilder-openrm\bin\librm.dll
c:\programme\clisp-2.38\asdf\lispbuilder-openrm\bin\librmaux.dll
c:\programme\clisp-2.38\asdf\lispbuilder-openrm\bin\librmi.dll
c:\programme\clisp-2.38\asdf\lispbuilder-openrm\bin\librmv.dll
c:\programme\clisp-2.38\asdf\lispbuilder-openrm\bin\msvcr70.dll
c:\programme\clisp-2.38\asdf\lispbuilder-openrm\bin\msvcrtd.dll
c:\programme\clisp-2.38\asdf\lispbuilder-openrm\bin\pthreadGC.dll
c:\programme\clisp-2.38\asdf\lispbuilder-openrm\bin\pthreadGCE.dll
c:\programme\clisp-2.38\asdf\lispbuilder-openrm\bin\pthreadVC.dll
c:\programme\clisp-2.38\asdf\lispbuilder-openrm\bin\pthreadVCE.dll
c:\programme\clisp-2.38\asdf\lispbuilder-openrm\bin\pthreadVSE.dll
This section describes the installation of the lispbuilder-openrm package for Linux users. Download the latest version of the lispbuilder-openrm package from sourceforge, and:
(asdf-install:install "lispbuilder-openrm_0.1.tgz")
, orThis section describes the installation of the OpenRM binaries for Linux.
Add the following to the ASDF *central-registry*:
(pushnew "/programme/clisp-2.38/asdf/lispbuilder-openrm_0.1/" asdf:*central-registry*)
Set "lispbuilder-openrm_0.1/" to the version number of the lispbuilder-openrm package, e.g. "lispbuilder-openrm_{version_number}/".
Now start CLISP and enter the following at the prompt:
(load "c:/programme/clisp-2.38/asdf/asdf.lisp") (asdf:operate 'asdf:load-op :lispbuilder-openrm-examples)
ASDF will take care of loading the CFFI, :lispbuilder-sdl and :lispbuilder-openrm dependencies. The OpenRM binaries will also be loaded into the Lisp image at this time.
Great. If you received any errors during this processes then go back and verify that the pathnames entered into the *central-registry* match the directories where the packages were installed. Also verify that the OpenRM binaries are located somewhere in the search path, as described above.
To verify that lispbuilder-openrm is installed correctly, run the example by entering:
(rm-examples:jballs)
You should see a window as show below. Click-drag the mouse using the left, right or middle mouse buttons to arc, dolly and translate the camera viewpoint.
The 'A' key will display bounding boxes around each object in the scene:
(rm-examples:spotlight)
Click-drag the mouse while depressing the left mouse button to rotate the spotlight.


Herring Cove
Toggle navigation
勇者不惧
List
Links
Projects
About
勇者不惧
Blog of Qin GUO
List
Links
Projects
About
@twitter
linkedin
facebook
github
FRESHLY MINTED
新的冒险!
Read More
Greeting!
This is a blog of
Qin GUO
, a big fan of Computer Vison.
In this place, you will find some blog posts, interesting links, and more. You'll also find a number of ways to get in touch. Feel free to contact me.
Recent Posts
新的冒险
FRESH
10 Nov 2014
View More...
Please enable JavaScript to view the
comments powered by Disqus.


"x" "1" "BROWN,B AVALOS,J LEE,C AND DOOLITTLE,D, THE EFFECT OF TOBACCO SMOKE,NICOTINE, AND COTININE ON THE MUTAGENICITY OF4-(METHYLNITROSAMINO)-1-(3-PYRIDYL)-1-BUTANOL (NNAL), MUTAT. RES.494(1-2):21-29, 2001"


Functions Hub is an open marketplace that lets you upload functions for use in Function-as-a-Service environments and share them with other developers. It is mostly decentralised -- any accounts are outsourced to XMPP servers (those capable of XEP0049/Private Namespaces and XEP0060/PubSub, such as jabber.root.cz / jabb.im), and all functions are stored in a Git repository. No vendor lock-in!
Functions Hub is 'de-facto standards compliant' and does not mandate any new tools. Submit your functions through the usual deployment tools (aws, wsk, gcloud). For the time being, submission works without authentication.
This is a research prototype by the invincible Service Tooling initiative of the Service Prototyping Lab at Zurich University of Applied Sciences in Switzerland. Things may and will break. We will not run the marketplace commercially, but we'd be happy to hear if you do. No cats were harmed and no venture capital was burnt for setting this up. Credits: The marketplace frontend is a derivative of a previous research prototype by Uliana Andriiyeshyna.


Five Below starts selling products for more than $5
More merchandise remains under $5
by Leah Asmelash, CNN(CNN) - Five Below, a popular store focusing on products for $5 and under, is undergoing a colossal change: Its price point is going up for the first time in 17 years.
No longer will the store, a bargain hunter's dream, only sell items priced at $5 and under. Now, the store will be raising prices on certain items -- including tech products and certain toys and games.
"We've always done everything possible to absorb cost increases," the company said in a statement. "Recently, we had to raise prices above $5 on tech items to keep providing the products you love."
For the toys and games priced up to $10, the company will keep them in a separate area of the store, called "Ten Below Gift Shop." For electronics, Five Below is adding a section called "Ten Below Tech."
But the name "Five Below" is one thing that's not changing. The store -- known for selling everything from makeup and clothes to candy -- will be keeping its original name despite the change.
Most of the store's merchandise remains at $5 and below, according to the company.


--- layout: post title: "Composite as a monoid - a business rules example" description: "Composites are monoids. An example in C#, F#, and Haskell." date: 2018-05-17 6:45 UTC tags: [Software Design, F#, Haskell, Design Patterns] --- {% include JB/setup %}
{{ page.description }}
Towards the end of the first decade of the third millennium, I'd been writing object-oriented code for about ten years, and I'd started to notice some patterns in my code. I'd read Design Patterns 6-7 years earlier, but I noticed that I tended to use only a small subset of the patterns from the book - particularly Composite, Decorator, Chain of Responsibility, and a few others.
In particular, I noticed that modelling seemed to be easier, and the code better structured, when I could apply the Composite design pattern. It was also clear, however, that I couldn't always use the Composite pattern, so I started to speculate on what could be the distinguishing factors. In 2010, I made a first attempt at identifying when a Composite is possible, and when it isn't. Unfortunately, while it was a fine attempt (which I'll return to later), it didn't lead anywhere. Ultimately, I gave up on the subject and moved on to other things.
One of my interests in the next decade became functional programming. One day in late 2016 I came across this Code Review question by Scott Nimrod. It was an solution to the Business Rules kata, which, briefly told, is about implementing changing business rules in a sustainable manner.
In my answer to the question, I gave an outline (repeated below) of how I would address the problem in F#. As a comment to my answer, Scott wrote:
"Feels like the Decorator Pattern..."
I responded,
"Not really; it's the Composite pattern..."
A few days later, as I was doing something else, it suddenly dawned on me that not only was a few lines of F# code equivalent to the Composite design pattern, but those lines of code were also manifestations of fundamental abstractions from category theory. Originally, I thought Composite was a combination of applicative functors and monoids, but as I investigated, I discovered that Composites are simply monoids.
This article shows a concrete example of that discovery, starting with my original F# code, subsequently translating it to C# to demonstrate that it's a Composite, and concluding with a translation to Haskell in order to demonstrate that it all fits with the formalisation of Monoid
there.
The kata is about modelling volatile business rules in a sustainable manner. Particularly, you must implement various business rules associated with payments for products and services. Making a rough outline of a model, I started by introducing some types in F#:
type Membership = Basic | Gold type Good = | PhysicalProduct of string | Book of string | Video of string | Membership of Membership | Upgrade type Command = | Slip of string * (Good list) | Activate of Membership | Upgrade | PayAgent
This basically states that there's a closed hierarchy of goods, and a closed hierarchy of business commands, as well as a Membership
enumeration. A good can be a physical product with a name, a book with a name, a membership or upgrade, and so on. A command can be a packing slip, a membership activation, and so on.
Since I was only interested in a rough outline of a solution, I only sketched four business rules, all implemented as functions. The first creates a packing slip for certain goods:
// Good -> Command list let slipForShipping = function | PhysicalProduct name -> [Slip ("Shipping", [PhysicalProduct name])] | Book name -> [Slip ("Shipping", [Book name])] | Video name -> [Slip ("Shipping", [Video name])] | _ -> []
This function takes a Good
value as input and returns a list of Command
values as output. If the Good
is a PhysicalProduct
, Book
, or Video
, it returns a packing slip command; otherwise, it returns an empty list of commands.
The next business rule is a similar function:
// Good -> Command list let slipForRoyalty = function | Book name -> [Slip ("Royalty", [Book name])] | _ -> []
This business rule generates a royalty slip for any Book
, but does nothing for any other Good
.
The third business rule activates a membership:
// Good -> Command list let activate = function | Membership x -> [Activate x] | _ -> []
If the Good
is a Membership
, the activate
function returns a list containing a single Activate
command; otherwise, it returns an empty list.
Finally, the last rule upgrades a membership:
// Good -> Command list let upgrade = function | Good.Upgrade -> [Upgrade] | _ -> []
Similar to the previous functions, this one looks at the type of Good
, and returns an Upgrade
command when the input is an Upgrade
good, and an empty list otherwise.
Notice that all four functions share the same type: Good -> Command list
. I designed them like that on purpose, because this enables you to compose a list of business rules to a function that looks like a single rule:
// ('a -> 'b list) list -> 'a -> 'b list let handle rules good = List.collect (fun r -> r good) rules
This handle
function takes a list of business rules (rules
) and returns a new function with the type Good -> Command list
(or, actually, a function with the type 'a -> 'b list
- once again I've fallen into the trap of using too descriptive names). Notice that this is the same type as the individual rules.
You can now compose the four specific business rules:
// Good -> Command list let handleAll = handle [slipForShipping; slipForRoyalty; activate; upgrade]
This function also has the type Good -> Command list
although it's a composition of four rules.
You can use it like this F# Interactive example:
> handleAll (Book "The Annotated Turing");; val it : Command list = [Slip ("Shipping",[Book "The Annotated Turing"]); Slip ("Royalty",[Book "The Annotated Turing"])]
(Yes, I like The Annotated Turing - read my review on Goodreads.)
Notice that while each of the business rules produces only zero or one Command
values, in this example handleAll
returns two Command
values.
This design, where a composition looks like the things it composes, sounds familiar.
You can translate the above F# model to an object-oriented model in C#. Translating discriminated unions like Good
and Command
to C# always involves compromises. In order to keep the example as simple as possible, I decided to translate each of those to a marker interface, although I loathe that 'pattern':
public interface IGood { }
While the interface doesn't afford any behaviour, various classes can still implement it, like, for example, Book
:
public class Book : IGood { public Book(string name) { Name = name; } public string Name { get; } }
Other IGood
'implementations' looks similar, and there's a comparable class hierarchy for ICommand
, which is another marker interface.
The above F# code used a shared function type of Good -> Command list
as a polymorphic type for a business rule. You can translate that to a C# interface:
public interface IRule { IReadOnlyCollection<ICommand> Handle(IGood good); }
The above slipForShipping
function becomes a class that implements the IRule
interface:
public class SlipForShippingRule : IRule { public IReadOnlyCollection<ICommand> Handle(IGood good) { if (good is PhysicalProduct || good is Book || good is Video) return new[] { new SlipCommand("Shipping", good) }; return new ICommand[0]; } }
Instead of pattern matching on a discriminated union, the Handle
method examines the subtype of good
and only returns a SlipCommand
if the good
is either a PhysicalProduct
, a Book
, or a Video
.
The other implementations are similar, so I'm not going to show all of them, but here's one more:
public class ActivateRule : IRule { public IReadOnlyCollection<ICommand> Handle(IGood good) { var m = good as MembershipGood; if (m != null) return new[] { new ActivateCommand(m.Membership) }; return new ICommand[0]; } }
Since 'all' members of IRule
return collections, which form monoids over concatenation, the interface itself gives rise to a monoid. This means that you can create a Composite:
public class CompositeRule : IRule { private readonly IRule[] rules; public CompositeRule(params IRule[] rules) { this.rules = rules; } public IReadOnlyCollection<ICommand> Handle(IGood good) { var commands = new List<ICommand>(); foreach (var rule in rules) commands.AddRange(rule.Handle(good)); return commands; } }
Notice how the implementation of Handle
follows the template for monoid accumulation. It starts with the identity, which, for the collection concatenation monoid is the empty collection. It then loops through all the composed rules
and updates the accumulator commands
in each iteration. Here, I used AddRange
, which mutates commands
instead of returning a new value, but the result is the same. Finally, the method returns the accumulator.
You can now compose all the business rules and use the composition as though it was a single object:
var rule = new CompositeRule( new SlipForShippingRule(), new SlipForRoyaltyRule(), new ActivateRule(), new UpgradeRule()); var book = new Book("The Annotated Turing"); var commands = rule.Handle(book);
When the method returns, commands
contains two SlipCommand
objects - a packing slip, and a royalty slip.
You can also port the F# code to Haskell, which is usually easy as long as the F# is written in a 'functional style'. Since Haskell has an explicit notion of monoids, you'll be able to see how the two above solutions are monoidal.
The data types are easy to translate to Haskell - you only have to adjust the syntax a bit:
data Membership = Basic | Gold deriving (Show, Eq, Enum, Bounded) data Good = PhysicalProduct String | Book String | Video String | Membership Membership | UpgradeGood deriving (Show, Eq) data Command = Slip String [Good] | Activate Membership | Upgrade | PayAgent deriving (Show, Eq)
The business rule functions are also easy to translate:
slipForShipping :: Good -> [Command] slipForShipping pp@(PhysicalProduct _) = [Slip "Shipping" [pp]] slipForShipping b@(Book _) = [Slip "Shipping" [b]] slipForShipping v@(Video _) = [Slip "Shipping" [v]] slipForShipping _ = [] slipForRoyalty :: Good -> [Command] slipForRoyalty b@(Book _) = [Slip "Royalty" [b]] slipForRoyalty _ = [] activate :: Good -> [Command] activate (Membership m) = [Activate m] activate _ = [] upgrade :: Good -> [Command] upgrade (UpgradeGood) = [Upgrade] upgrade _ = []
Notice that all four business rules share the same type: Good -> [Command]
. This is conceptually the same type as in the F# code; instead of writing Command list
, which is the F# syntax, the Haskell syntax for a list of Command
values is [Command]
.
All those functions are monoids because their return types form a monoid, so in Haskell, you can compose them without further ado:
handleAll :: Good -> [Command] handleAll = mconcat [slipForShipping, slipForRoyalty, activate, upgrade]
mconcat
is a built-in function that aggregates any list of monoidal values to a single value:
mconcat :: Monoid a => [a] -> a
Since all four functions are monoids, this just works out of the box. A Composite is just a monoid. Here's an example of using handleAll
from GHCi:
*BusinessRules> handleAll $ Book "The Annotated Turing" [Slip "Shipping" [Book "The Annotated Turing"],Slip "Royalty" [Book "The Annotated Turing"]]
The result is as you'd come to expect.
 Haskell already understands monoids, so composition happens automatically.
If you prefer, you could even skip the handle
function too:
*BusinessRules> mconcat [slipForShipping, slipForRoyalty, activate, upgrade] $ Book "Blindsight" [Slip "Shipping" [Book "Blindsight"],Slip "Royalty" [Book "Blindsight"]]
(Yes, you should also read Blindsight.)
It's not that composition as such is built into Haskell, but rather that the language is designed around a powerful ability to model abstractions, and one of the built-in abstractions just happens to be monoids. You could argue, however, that many of Haskell's fundamental abstractions are built from category theory, and one of the fundamental characteristics of a category is how morphisms compose.
Composite are monoids. This article shows an example, starting with a solution in F#. You can translate the F# code to object-oriented C# and model the composition of business rules as a Composite. You can also translate the F# code 'the other way', to the strictly functional language Haskell, and thereby demonstrate that the solution is based on a monoid.
This article is a repost of a guest post on the NDC blog, reproduced here with kind permission.


In addition, this is a project that allowed me to do some work in Angular, MongoDB, Node, Express, Gulp, and Browserify. See here for more details.