home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / perl / Perl / Doc / perl5-notes
Encoding:
Text File  |  1994-10-18  |  10.7 KB  |  346 lines

  1. #!/usr/local/bin/perl5
  2. # perl5 -x notes 2> expected > actual ; diff expected actual
  3. open(EVAL, "| perl5 -x") || die "Can't pipe to perl5\n";
  4. while (<DATA>) {
  5.     m/prints ``(.*)''$/ && print STDERR $1,"\n";
  6.     print EVAL $_;
  7. }
  8. __END__
  9. #!/usr/local/bin/perl5
  10. #
  11. # Perl5a6 notes: Patchlevel 3
  12. #
  13. # This document is in the public domain.
  14. #
  15. # Written by Tony Sanders <sanders@bsdi.com>
  16. #
  17. # Quick examples of the new Perl5 features as of alpha6.  Look in the
  18. # file Changes, the man page, and in the test suite (esp t/op/ref.t)
  19. # for more information.  There are also a number of files in the alpha6
  20. # release (e.g., tie*) that show how to use various features.  Also, there
  21. # are a number of package modules in lib/*.pm that are of interest.
  22. #
  23. # Thanks to the following for their input:
  24. #     Johan.Vromans@NL.net
  25. #     Daniel Faken <absinthe@viva.chem.washington.edu>
  26. #     Tom Christiansen <tchrist@wraeththu.cs.colorado.edu>
  27. #     Dean Roehrich <roehrich@ferrari.cray.com>
  28. #     Larry Wall <lwall@netlabs.com>
  29. #     Lionel Cons <Lionel.Cons@cern.ch>
  30. #
  31.  
  32. # BEGIN { }
  33.     # executed at load time
  34.     print "doody\n";
  35.     BEGIN { print "howdy\n"; }        # prints ``howdy''
  36.                         # then prints ``doody''
  37. # END { }
  38.     # executed at exit time in reverse order of definition
  39.     END { print "blue sky\n"; }        # will print ``blue sky''
  40.     END { print "goodbye\n"; }        # will print ``goodbye''
  41.  
  42. # (expr?lval:lval) = value;
  43.     # The (?:) operator can be used as an lvalue.
  44.     $a = 1; $b = 2;
  45.     (defined $b ? $a : $b) = 10;
  46.     print "$a:$b\n";            # prints ``10:2''
  47.  
  48. # new functions: abs, chr, uc, ucfirst, lc, lcfirst
  49.     print abs(-10), "\n";            # prints ``10''
  50.     print chr(64), "\n";            # prints ``@''
  51.     print uc("the"), "\n";            # prints ``THE''
  52.     print ucfirst("the"), "\n";        # prints ``The''
  53.     print lc("THE"), "\n";            # prints ``the''
  54.     print lcfirst("THE"), "\n";        # prints ``tHE''
  55.  
  56. # references
  57.     # references
  58.     $thing1 = "testing";
  59.     $ref = \$thing1;            # \ creates a reference
  60.     print $$ref,"\n" if ${$ref} eq $$ref;    # deref, prints ``testing''
  61.  
  62.     # symbolic references
  63.     sub bat { "baz"; }
  64.     sub baz { print "foobar\n" };
  65.     &{&bat};                # prints ``foobar''
  66.  
  67. # symbol table assignment: *foo = \&func;
  68.     # replaces an item in the symbol table (function, scalar, array, hash)
  69.     # *foo = \$bar        replaces the scalar
  70.     # *foo = \%bar        replaces the hash table
  71.     # *foo = \@bar        replaces the array
  72.     # *foo = \&bar        replaces the function
  73.     # *foo = *bar        all of the above (including FILEHANDLE!)
  74.     # XXX: can't do just filehandles (yet)
  75.     #
  76.     # This can be used to import and rename a symbol from another package:
  77.     #     *myfunc = \&otherpack::otherfunc;
  78.  
  79. # AUTOLOAD { ...; }
  80.     # called if method not found, passed function name in $AUTOLOAD
  81.     # @_ are the arguments to the function.
  82. # goto &func;
  83.     # goto's a function, used by AUTOLOAD to jump to the function
  84. # qw/arg list/;   qw(arg list);
  85.     # quoted words, yields a list; works like split(' ', 'arg list')
  86.     # not a function, more like q//;
  87.     {
  88.         package AutoLoader;
  89.         AUTOLOAD {
  90.         eval "sub $AUTOLOAD" . '{ print "@_\n"}';
  91.         goto &$AUTOLOAD }
  92.         package JAPH;
  93.         @ISA = (AutoLoader);
  94.         sub foo2 { &bar }
  95.         foo2 qw(Just another Perl hacker,);
  96.         # prints ``Just another Perl hacker,''
  97.     }
  98. # Larry notes:
  99. # You might point out that there's a canned Autoloader base class in the
  100. # library.  Another subtlety is that $AUTOLOAD is always in the same
  101. # package as the AUTOLOAD routine, so if you call another package's
  102. # AUTOLOAD explicitly you have to set $AUTOLOAD in that package first.
  103.  
  104. # my
  105.     # lexical scoping
  106.     sub samp1 { print $z,"\n" }
  107.     sub samp2 { my($z) = "world"; &samp1 }
  108.     $z = "hello";
  109.     &samp2;                # prints ``hello''
  110.  
  111. # package;
  112.     # empty package; for catching non-local variable references
  113.     sub samp3 {
  114.         my $x = shift;        # local() would work also
  115.         package;            # empty package
  116.         $main::count += $x;        # this is ok.
  117.         # $y = 1;            # would be a compile time error
  118.     }
  119.  
  120. # =>
  121.     # works like comma (,); use for key/value pairs
  122.         # sometimes used to disambiguate the final expression in a block
  123.     # might someday supply warnings if you get out of sync
  124.     %foo = ( abc => foo );
  125.     print $foo{abc},"\n";        # prints ``foo''
  126.  
  127. # ::
  128.     # works like tick (') (use of ' is deprecated in perl5)
  129.         print $main::foo{abc},"\n";    # prints ``foo''
  130.  
  131. # bless ref;
  132.     # Bless takes a reference and returns an "object"
  133.     $oref = bless \$scalar;
  134.  
  135. # ->
  136.     # dereferences an "object"
  137.     $x = { def => bar };        # $x is ref to anonymous hash
  138.     print $x->{def},"\n";        # prints ``bar''
  139.  
  140.     # method derefs must be bless'ed
  141.     {
  142.         # initial cap is encouraged to avoid naming conflicts
  143.         package Sample;
  144.         sub samp4 { my($this) = shift; print $this->{def},"\n"; }
  145.         sub samp5 { print "samp5: ", $_[1], "\n"; }
  146.         $main::y = bless $main::x;    # $x is ref, $y is "object"
  147.     }
  148.     $y->samp4();            # prints ``bar''
  149.  
  150.     # indirect object calls (same as $y->samp5(arglist))
  151.     samp5 $y arglist;        # prints ``samp5: arglist''
  152.  
  153.     # static method calls (often used for constructors, see below)
  154.     samp5 Sample arglist;        # prints ``samp5: arglist''
  155.  
  156. # function calls without &
  157.     sub samp6 { print "look ma\n"; }
  158.     samp6;                # prints ``look ma''
  159.  
  160.     # "forward" decl
  161.     sub samp7;
  162.     samp7;                          # prints ``look pa''
  163.     sub samp7 { print "look pa\n"; }
  164.  
  165.     # no decl requires ()'s or initial &
  166.     &samp8;                # prints ``look da''
  167.     samp8();            # prints ``look da''
  168.     sub samp8 { print "look da\n"; }
  169.  
  170. # ref
  171.     # returns "object" type
  172.     {
  173.         package OBJ1;
  174.         $x = bless \$y;        # returns "object" $x in "class" OBJ1
  175.         print ref $x,"\n";        # prints ``OBJ1''
  176.     }
  177.  
  178.     # and non-references return undef.
  179.     $z = 1;
  180.     print "non-ref\n" unless ref $z;    # prints ``non-ref''
  181.  
  182.     # ref's to "builtins" return type
  183.     print ref \$ascalar,"\n";        # prints ``SCALAR''
  184.     print ref \@array,"\n";            # prints ``ARRAY''
  185.     print ref \%hash,"\n";            # prints ``HASH''
  186.     sub func { print shift,"\n"; }
  187.     print ref \&func,"\n";            # prints ``CODE''
  188.     print ref \\$scalar,"\n";        # prints ``REF''
  189.  
  190. # tie
  191.     # bind a variable to a package with magic functions:
  192.         #     new, DESTROY, fetch, store, delete, firstkey, nextkey
  193.     # The exact function list varies with the variable type,
  194.     # see the man page and tie* for more details.
  195.     # Usage: tie variable, PackageName, ARGLIST
  196.     {
  197.         package TIEPACK;
  198.         sub new { print "NEW: @_\n"; local($x) = $_[1]; bless \$x }
  199.         sub fetch { print "fetch ", ref $_[0], "\n"; ${$_[0]} }
  200.         sub store { print "store $_[1]\n"; ${$_[0]} = $_[1] }
  201.         DESTROY { print "DESTROY ", ref $_[0], "\n" }
  202.     }
  203.     tie $h, TIEPACK, "black_tie";    # prints ``NEW: TIEPACK black_tie''
  204.     print $h, "\n";            # prints ``fetch TIEPACK''
  205.                     # prints ``black_tie''
  206.     $h = 'bar';            # prints ``store bar''
  207.     untie $h;            # prints ``DESTROY SCALAR''
  208.  
  209. # References
  210.     $sref = \$scalar;        # $$sref is scalar
  211.     $aref = \@array;        # @$aref is array
  212.     $href = \%hash;            # %$href is hash table
  213.     $fref = \&func;            # &$fref is function
  214.     $refref = \$fref;        # ref to ref to function
  215.     &$$refref("call the function");    # prints ``call the function''
  216.  
  217. # Anonymous data-structures
  218.     %hash = ( abc => foo );        # hash in perl4 (works in perl5 also)
  219.     print $hash{abc},"\n";        # prints ``foo''
  220.     $ref = { abc => bar };        # reference to anon hash
  221.     print $ref->{abc},"\n";        # prints ``bar''
  222.  
  223.     @ary = ( 0, 1, 2 );        # array in perl4 (works in perl5 also)
  224.     print $ary[1],"\n";        # prints ``1''
  225.     $ref = [ 3, 4, 5 ];        # reference to anon array
  226.     print $ref->[1],"\n";        # prints ``4''
  227.  
  228. # Nested data-structures
  229.     @foo = ( 0, { name => foobar }, 2, 3 );        # $#foo == 3
  230.     $aref = [ 0, { name => foobar }, 2, 3 ];    # ref to anon array
  231.     $href = {                    # ref to hash of arrays
  232.         John => [ Mary, Pat, Blanch ],
  233.         Paul => [ Sally, Jill, Jane ],
  234.         Mark => [ Ann, Bob, Dawn ],
  235.     };
  236.     print $href->{Paul}->[0], "\n";    # prints ``Sally''
  237.     print $href->{Paul}[0],"\n";    # shorthand version, prints ``Sally''
  238.     print @{$href->{Mark}},"\n";    # prints ``AnnBobDawn''
  239.  
  240. # @ISA
  241.     # Multiple Inheritance (get rich quick)
  242.     {
  243.         package OBJ2; sub abc { print "abc\n"; }
  244.         package OBJ3; sub def { print "def\n"; }
  245.         package OBJ4; @ISA = ("OBJ2", "OBJ3");
  246.         $x = bless { foo => bar };
  247.         $x->abc;                    # prints ``abc''
  248.         $x->def;                    # prints ``def''
  249.     }
  250.  
  251. # Packages, Classes, Objects, Methods, Constructors, Destructors, etc.
  252.         # XXX: need more explinations and samples
  253.     {
  254.         package OBJ5;
  255.         sub new { print "NEW: @_\n"; my($x) = "empty"; bless \$x }
  256.         sub output { my($this) = shift; print "value = $$this\n"; }
  257.         DESTROY { print "OBJ5 DESTROY\n" }
  258.     }
  259.     # Constructors are often written as static method calls:
  260.     $x = new OBJ5;        # prints ``NEW: OBJ5''
  261.     $x->output;        # prints ``value = empty''
  262.     # The destructor is responsible for calling any base class destructors.
  263.     undef $x;        # prints ``OBJ5 DESTROY''
  264.  
  265. # require Package;
  266.     # same as:  BEGIN { require 'Package.pm'; }
  267. # require <float>;
  268.     # checks against the perl version number
  269.     require 5.000;        # requires perl 5.0 or better
  270.  
  271. # Package Modules
  272. # ===============
  273. # Yes, these are all very sketchy.  See the .pm file for details.
  274.  
  275. # DynamicLoader (builtin)
  276.     # Public: &bootstrap
  277.     # Load a shared library package on systems that support it
  278.     # This incomplete example was extracted from lib/POSIX.pm
  279.     #
  280.     # package POSIX;
  281.     # requires Exporter; require AutoLoader;
  282.     # @ISA = (Exporter, AutoLoader, DynamicLoader);
  283.     # @EXPORT = qw(closedir, opendir, [..., lots of functions]);
  284.     # bootstrap POSIX;
  285.  
  286. # Larry notes:
  287. # The gist of it is that DynamicLoader::bootstrap is only called if main.c
  288. # didn't already define MYPACKAGE::bootstrap.  So the .pm file doesn't know
  289. # (or care) whether the module is statically or dynamically loaded.
  290.  
  291. # AutoLoader.pm
  292.     # Public: &AUTOLOAD
  293.     # Causes functions from .../lib/perl/auto/PACKAGE/*.al to autoload
  294.     # when used but not defined.
  295.  
  296. # Config.pm
  297.     # Exports: %Config
  298.     # The data from the Configure script for perl programs (yeah)
  299.  
  300. # English.pm
  301.     # Exports: (lots of verbose variables)
  302.     # The "english" versions of things like $_ $| $=
  303.  
  304. # Exporter.pm
  305.     # Public: &import
  306.     # import PACKAGE [@symbols]
  307.     # requires PACKAGE to define @EXPORT
  308.     {
  309.         package FOOBAR;  
  310.         require Exporter;
  311.         @ISA = (Exporter);
  312.         @EXPORT = (foo, bar);
  313.         sub foo { print "FOO\n" };
  314.         sub bar { print "BAR\n" };
  315.         1;
  316.         package BAT;
  317.         # require FOOBAR;    # not in this example
  318.         import FOOBAR;
  319.         @ISA = ();
  320.         &foo;            # prints ``FOO''
  321.     }
  322.  
  323. # FileHandle.pm
  324.     # Exports: (lots of filehandle functions)
  325.     # English versions of various filehandle operations
  326.  
  327. # Hostname.pm
  328.     # Exports: &hostname
  329.     # Routine to get hostname
  330.     # {
  331.     #    require Hostname; import Hostname;
  332.     #    print &hostname,"\n";    # prints your hostname
  333.     # }
  334.  
  335. # POSIX.pm
  336.     # Exports: (posix functions and defines)
  337.     # POSIX.1 bindings
  338.  
  339. # SDBM_File.pm
  340.     # SDBM interfaces (use with `tie')
  341.     # Other DBM interfaces work the same way
  342.  
  343. # when the script exits the END section gets executed and prints ``goodbye''
  344. # ENDs are executed in reverse order of definition. prints ``blue sky''
  345. __END__
  346.