home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / RiscOS / APP / DEVS / PERL / RPC106.ZIP / Rpc106 / Docs / perldata < prev    next >
Text File  |  1997-11-23  |  25KB  |  602 lines

  1. NAME
  2.     perldata - Perl data types
  3.  
  4. DESCRIPTION
  5.   Variable names
  6.  
  7.     Perl has three data structures: scalars, arrays of scalars, and
  8.     associative arrays of scalars, known as "hashes". Normal arrays
  9.     are indexed by number, starting with 0. (Negative subscripts
  10.     count from the end.) Hash arrays are indexed by string.
  11.  
  12.     Values are usually referred to by name (or through a named
  13.     reference). The first character of the name tells you to what
  14.     sort of data structure it refers. The rest of the name tells you
  15.     the particular value to which it refers. Most often, it consists
  16.     of a single *identifier*, that is, a string beginning with a
  17.     letter or underscore, and containing letters, underscores, and
  18.     digits. In some cases, it may be a chain of identifiers,
  19.     separated by `::' (or by `'', but that's deprecated); all but
  20.     the last are interpreted as names of packages, to locate the
  21.     namespace in which to look up the final identifier (see the
  22.     "Packages" entry in the perlmod manpage for details). It's
  23.     possible to substitute for a simple identifier an expression
  24.     which produces a reference to the value at runtime; this is
  25.     described in more detail below, and in the perlref manpage.
  26.  
  27.     There are also special variables whose names don't follow these
  28.     rules, so that they don't accidentally collide with one of your
  29.     normal variables. Strings which match parenthesized parts of a
  30.     regular expression are saved under names containing only digits
  31.     after the `$' (see the perlop manpage and the perlre manpage).
  32.     In addition, several special variables which provide windows
  33.     into the inner working of Perl have names containing punctuation
  34.     characters (see the perlvar manpage).
  35.  
  36.     Scalar values are always named with '$', even when referring to
  37.     a scalar that is part of an array. It works like the English
  38.     word "the". Thus we have:
  39.  
  40.     $days            # the simple scalar value "days"
  41.     $days[28]        # the 29th element of array @days
  42.     $days{'Feb'}        # the 'Feb' value from hash %days
  43.     $#days            # the last index of array @days
  44.  
  45.     but entire arrays or array slices are denoted by '@', which
  46.     works much like the word "these" or "those":
  47.  
  48.     @days            # ($days[0], $days[1],... $days[n])
  49.     @days[3,4,5]        # same as @days[3..5]
  50.     @days{'a','c'}        # same as ($days{'a'},$days{'c'})
  51.  
  52.     and entire hashes are denoted by '%':
  53.  
  54.     %days            # (key1, val1, key2, val2 ...)
  55.  
  56.     In addition, subroutines are named with an initial '&', though
  57.     this is optional when it's otherwise unambiguous (just as "do"
  58.     is often redundant in English). Symbol table entries can be
  59.     named with an initial '*', but you don't really care about that
  60.     yet.
  61.  
  62.     Every variable type has its own namespace. You can, without fear
  63.     of conflict, use the same name for a scalar variable, an array,
  64.     or a hash (or, for that matter, a filehandle, a subroutine name,
  65.     or a label). This means that $foo and @foo are two different
  66.     variables. It also means that `$foo[1]' is a part of @foo, not a
  67.     part of $foo. This may seem a bit weird, but that's okay,
  68.     because it is weird.
  69.  
  70.     Because variable and array references always start with '$',
  71.     '@', or '%', the "reserved" words aren't in fact reserved with
  72.     respect to variable names. (They ARE reserved with respect to
  73.     labels and filehandles, however, which don't have an initial
  74.     special character. You can't have a filehandle named "log", for
  75.     instance. Hint: you could say `open(LOG,'logfile')' rather than
  76.     `open(log,'logfile')'. Using uppercase filehandles also improves
  77.     readability and protects you from conflict with future reserved
  78.     words.) Case *IS* significant--"FOO", "Foo", and "foo" are all
  79.     different names. Names that start with a letter or underscore
  80.     may also contain digits and underscores.
  81.  
  82.     It is possible to replace such an alphanumeric name with an
  83.     expression that returns a reference to an object of that type.
  84.     For a description of this, see the perlref manpage.
  85.  
  86.     Names that start with a digit may contain only more digits.
  87.     Names which do not start with a letter, underscore, or digit are
  88.     limited to one character, e.g., `$%' or `$$'. (Most of these one
  89.     character names have a predefined significance to Perl. For
  90.     instance, `$$' is the current process id.)
  91.  
  92.   Context
  93.  
  94.     The interpretation of operations and values in Perl sometimes
  95.     depends on the requirements of the context around the operation
  96.     or value. There are two major contexts: scalar and list. Certain
  97.     operations return list values in contexts wanting a list, and
  98.     scalar values otherwise. (If this is true of an operation it
  99.     will be mentioned in the documentation for that operation.) In
  100.     other words, Perl overloads certain operations based on whether
  101.     the expected return value is singular or plural. (Some words in
  102.     English work this way, like "fish" and "sheep".)
  103.  
  104.     In a reciprocal fashion, an operation provides either a scalar
  105.     or a list context to each of its arguments. For example, if you
  106.     say
  107.  
  108.     int( <STDIN> )
  109.  
  110.     the integer operation provides a scalar context for the <STDIN>
  111.     operator, which responds by reading one line from STDIN and
  112.     passing it back to the integer operation, which will then find
  113.     the integer value of that line and return that. If, on the other
  114.     hand, you say
  115.  
  116.     sort( <STDIN> )
  117.  
  118.     then the sort operation provides a list context for <STDIN>,
  119.     which will proceed to read every line available up to the end of
  120.     file, and pass that list of lines back to the sort routine,
  121.     which will then sort those lines and return them as a list to
  122.     whatever the context of the sort was.
  123.  
  124.     Assignment is a little bit special in that it uses its left
  125.     argument to determine the context for the right argument.
  126.     Assignment to a scalar evaluates the righthand side in a scalar
  127.     context, while assignment to an array or array slice evaluates
  128.     the righthand side in a list context. Assignment to a list also
  129.     evaluates the righthand side in a list context.
  130.  
  131.     User defined subroutines may choose to care whether they are
  132.     being called in a scalar or list context, but most subroutines
  133.     do not need to care, because scalars are automatically
  134.     interpolated into lists. See the "wantarray" entry in the
  135.     perlfunc manpage.
  136.  
  137.   Scalar values
  138.  
  139.     All data in Perl is a scalar or an array of scalars or a hash of
  140.     scalars. Scalar variables may contain various kinds of singular
  141.     data, such as numbers, strings, and references. In general,
  142.     conversion from one form to another is transparent. (A scalar
  143.     may not contain multiple values, but may contain a reference to
  144.     an array or hash containing multiple values.) Because of the
  145.     automatic conversion of scalars, operations, and functions that
  146.     return scalars don't need to care (and, in fact, can't care)
  147.     whether the context is looking for a string or a number.
  148.  
  149.     Scalars aren't necessarily one thing or another. There's no
  150.     place to declare a scalar variable to be of type "string", or of
  151.     type "number", or type "filehandle", or anything else. Perl is a
  152.     contextually polymorphic language whose scalars can be strings,
  153.     numbers, or references (which includes objects). While strings
  154.     and numbers are considered pretty much the same thing for nearly
  155.     all purposes, references are strongly-typed uncastable pointers
  156.     with builtin reference-counting and destructor invocation.
  157.  
  158.     A scalar value is interpreted as TRUE in the Boolean sense if it
  159.     is not the null string or the number 0 (or its string
  160.     equivalent, "0"). The Boolean context is just a special kind of
  161.     scalar context.
  162.  
  163.     There are actually two varieties of null scalars: defined and
  164.     undefined. Undefined null scalars are returned when there is no
  165.     real value for something, such as when there was an error, or at
  166.     end of file, or when you refer to an uninitialized variable or
  167.     element of an array. An undefined null scalar may become defined
  168.     the first time you use it as if it were defined, but prior to
  169.     that you can use the defined() operator to determine whether the
  170.     value is defined or not.
  171.  
  172.     To find out whether a given string is a valid nonzero number,
  173.     it's usually enough to test it against both numeric 0 and also
  174.     lexical "0" (although this will cause -w noises). That's because
  175.     strings that aren't numbers count as 0, just as they do in awk:
  176.  
  177.     if ($str == 0 && $str ne "0")  {
  178.         warn "That doesn't look like a number";
  179.     }
  180.  
  181.     That's usually preferable because otherwise you won't treat IEEE
  182.     notations like `NaN' or `Infinity' properly. At other times you
  183.     might prefer to use a regular expression to check whether data
  184.     is numeric. See the perlre manpage for details on regular
  185.     expressions.
  186.  
  187.     warn "has nondigits"        if       /\D/;
  188.     warn "not a whole number"   unless /^\d+$/;
  189.     warn "not an integer"        unless /^[+-]?\d+$/
  190.     warn "not a decimal number" unless /^[+-]?\d+\.?\d*$/
  191.     warn "not a C float"
  192.         unless /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;
  193.  
  194.     The length of an array is a scalar value. You may find the
  195.     length of array @days by evaluating `$#days', as in csh.
  196.     (Actually, it's not the length of the array, it's the subscript
  197.     of the last element, because there is (ordinarily) a 0th
  198.     element.) Assigning to `$#days' changes the length of the array.
  199.     Shortening an array by this method destroys intervening values.
  200.     Lengthening an array that was previously shortened *NO LONGER*
  201.     recovers the values that were in those elements. (It used to in
  202.     Perl 4, but we had to break this to make sure destructors were
  203.     called when expected.) You can also gain some measure of
  204.     efficiency by pre-extending an array that is going to get big.
  205.     (You can also extend an array by assigning to an element that is
  206.     off the end of the array.) You can truncate an array down to
  207.     nothing by assigning the null list () to it. The following are
  208.     equivalent:
  209.  
  210.     @whatever = ();
  211.     $#whatever = -1;
  212.  
  213.     If you evaluate a named array in a scalar context, it returns
  214.     the length of the array. (Note that this is not true of lists,
  215.     which return the last value, like the C comma operator.) The
  216.     following is always true:
  217.  
  218.     scalar(@whatever) == $#whatever - $[ + 1;
  219.  
  220.     Version 5 of Perl changed the semantics of `$[': files that
  221.     don't set the value of `$[' no longer need to worry about
  222.     whether another file changed its value. (In other words, use of
  223.     `$[' is deprecated.) So in general you can assume that
  224.  
  225.     scalar(@whatever) == $#whatever + 1;
  226.  
  227.     Some programmers choose to use an explicit conversion so
  228.     nothing's left to doubt:
  229.  
  230.     $element_count = scalar(@whatever);
  231.  
  232.     If you evaluate a hash in a scalar context, it returns a value
  233.     which is true if and only if the hash contains any key/value
  234.     pairs. (If there are any key/value pairs, the value returned is
  235.     a string consisting of the number of used buckets and the number
  236.     of allocated buckets, separated by a slash. This is pretty much
  237.     useful only to find out whether Perl's (compiled in) hashing
  238.     algorithm is performing poorly on your data set. For example,
  239.     you stick 10,000 things in a hash, but evaluating %HASH in
  240.     scalar context reveals "1/16", which means only one out of
  241.     sixteen buckets has been touched, and presumably contains all
  242.     10,000 of your items. This isn't supposed to happen.)
  243.  
  244.   Scalar value constructors
  245.  
  246.     Numeric literals are specified in any of the customary floating
  247.     point or integer formats:
  248.  
  249.     12345
  250.     12345.67
  251.     .23E-10
  252.     0xffff            # hex
  253.     0377            # octal
  254.     4_294_967_296        # underline for legibility
  255.  
  256.     String literals are usually delimited by either single or double
  257.     quotes. They work much like shell quotes: double-quoted string
  258.     literals are subject to backslash and variable substitution;
  259.     single-quoted strings are not (except for "`\''" and "`\\'").
  260.     The usual Unix backslash rules apply for making characters such
  261.     as newline, tab, etc., as well as some more exotic forms. See
  262.     the section on "Quote and Quotelike Operators" in the perlop
  263.     manpage for a list.
  264.  
  265.     Octal or hex representations in string literals (e.g. '0xffff')
  266.     are not automatically converted to their integer representation.
  267.     The hex() and oct() functions make these conversions for you.
  268.     See the "hex" entry in the perlfunc manpage and the "oct" entry
  269.     in the perlfunc manpage for more details.
  270.  
  271.     You can also embed newlines directly in your strings, i.e., they
  272.     can end on a different line than they begin. This is nice, but
  273.     if you forget your trailing quote, the error will not be
  274.     reported until Perl finds another line containing the quote
  275.     character, which may be much further on in the script. Variable
  276.     substitution inside strings is limited to scalar variables,
  277.     arrays, and array slices. (In other words, names beginning with
  278.     $ or @, followed by an optional bracketed expression as a
  279.     subscript.) The following code segment prints out "The price is
  280.     $100."
  281.  
  282.     $Price = '$100';    # not interpreted
  283.     print "The price is $Price.\n";     # interpreted
  284.  
  285.     As in some shells, you can put curly brackets around the name to
  286.     delimit it from following alphanumerics. In fact, an identifier
  287.     within such curlies is forced to be a string, as is any single
  288.     identifier within a hash subscript. Our earlier example,
  289.  
  290.     $days{'Feb'}
  291.  
  292.     can be written as
  293.  
  294.     $days{Feb}
  295.  
  296.     and the quotes will be assumed automatically. But anything more
  297.     complicated in the subscript will be interpreted as an
  298.     expression.
  299.  
  300.     Note that a single-quoted string must be separated from a
  301.     preceding word by a space, because single quote is a valid
  302.     (though deprecated) character in a variable name (see the
  303.     "Packages" entry in the perlmod manpage).
  304.  
  305.     Three special literals are __FILE__, __LINE__, and __PACKAGE__,
  306.     which represent the current filename, line number, and package
  307.     name at that point in your program. They may be used only as
  308.     separate tokens; they will not be interpolated into strings. If
  309.     there is no current package (due to a `package;' directive),
  310.     __PACKAGE__ is the undefined value.
  311.  
  312.     The tokens __END__ and __DATA__ may be used to indicate the
  313.     logical end of the script before the actual end of file. Any
  314.     following text is ignored, but may be read via a DATA
  315.     filehandle: main::DATA for __END__, or PACKNAME::DATA (where
  316.     PACKNAME is the current package) for __DATA__. The two control
  317.     characters ^D and ^Z are synonyms for __END__ (or __DATA__ in a
  318.     module). See the SelfLoader manpage for more description of
  319.     __DATA__, and an example of its use. Note that you cannot read
  320.     from the DATA filehandle in a BEGIN block: the BEGIN block is
  321.     executed as soon as it is seen (during compilation), at which
  322.     point the corresponding __DATA__ (or __END__) token has not yet
  323.     been seen.
  324.  
  325.     A word that has no other interpretation in the grammar will be
  326.     treated as if it were a quoted string. These are known as
  327.     "barewords". As with filehandles and labels, a bareword that
  328.     consists entirely of lowercase letters risks conflict with
  329.     future reserved words, and if you use the -w switch, Perl will
  330.     warn you about any such words. Some people may wish to outlaw
  331.     barewords entirely. If you say
  332.  
  333.     use strict 'subs';
  334.  
  335.     then any bareword that would NOT be interpreted as a subroutine
  336.     call produces a compile-time error instead. The restriction
  337.     lasts to the end of the enclosing block. An inner block may
  338.     countermand this by saying `no strict 'subs''.
  339.  
  340.     Array variables are interpolated into double-quoted strings by
  341.     joining all the elements of the array with the delimiter
  342.     specified in the `$"' variable (`$LIST_SEPARATOR' in English),
  343.     space by default. The following are equivalent:
  344.  
  345.     $temp = join($",@ARGV);
  346.     system "echo $temp";
  347.  
  348.     system "echo @ARGV";
  349.  
  350.     Within search patterns (which also undergo double-quotish
  351.     substitution) there is a bad ambiguity: Is `/$foo[bar]/' to be
  352.     interpreted as `/${foo}[bar]/' (where `[bar]' is a character
  353.     class for the regular expression) or as `/${foo[bar]}/' (where
  354.     `[bar]' is the subscript to array @foo)? If @foo doesn't
  355.     otherwise exist, then it's obviously a character class. If @foo
  356.     exists, Perl takes a good guess about `[bar]', and is almost
  357.     always right. If it does guess wrong, or if you're just plain
  358.     paranoid, you can force the correct interpretation with curly
  359.     brackets as above.
  360.  
  361.     A line-oriented form of quoting is based on the shell "here-doc"
  362.     syntax. Following a `<<' you specify a string to terminate the
  363.     quoted material, and all lines following the current line down
  364.     to the terminating string are the value of the item. The
  365.     terminating string may be either an identifier (a word), or some
  366.     quoted text. If quoted, the type of quotes you use determines
  367.     the treatment of the text, just as in regular quoting. An
  368.     unquoted identifier works like double quotes. There must be no
  369.     space between the `<<' and the identifier. (If you put a space
  370.     it will be treated as a null identifier, which is valid, and
  371.     matches the first empty line.) The terminating string must
  372.     appear by itself (unquoted and with no surrounding whitespace)
  373.     on the terminating line.
  374.  
  375.         print <<EOF;
  376.     The price is $Price.
  377.     EOF
  378.  
  379.         print <<"EOF";  # same as above
  380.     The price is $Price.
  381.     EOF
  382.  
  383.         print <<`EOC`;  # execute commands
  384.     echo hi there
  385.     echo lo there
  386.     EOC
  387.  
  388.         print <<"foo", <<"bar"; # you can stack them
  389.     I said foo.
  390.     foo
  391.     I said bar.
  392.     bar
  393.  
  394.         myfunc(<<"THIS", 23, <<'THAT');
  395.     Here's a line
  396.     or two.
  397.     THIS
  398.     and here's another.
  399.     THAT
  400.  
  401.     Just don't forget that you have to put a semicolon on the end to
  402.     finish the statement, as Perl doesn't know you're not going to
  403.     try to do this:
  404.  
  405.         print <<ABC
  406.     179231
  407.     ABC
  408.         + 20;
  409.  
  410.   List value constructors
  411.  
  412.     List values are denoted by separating individual values by
  413.     commas (and enclosing the list in parentheses where precedence
  414.     requires it):
  415.  
  416.     (LIST)
  417.  
  418.     In a context not requiring a list value, the value of the list
  419.     literal is the value of the final element, as with the C comma
  420.     operator. For example,
  421.  
  422.     @foo = ('cc', '-E', $bar);
  423.  
  424.     assigns the entire list value to array foo, but
  425.  
  426.     $foo = ('cc', '-E', $bar);
  427.  
  428.     assigns the value of variable bar to variable foo. Note that the
  429.     value of an actual array in a scalar context is the length of
  430.     the array; the following assigns the value 3 to $foo:
  431.  
  432.     @foo = ('cc', '-E', $bar);
  433.     $foo = @foo;            # $foo gets 3
  434.  
  435.     You may have an optional comma before the closing parenthesis of
  436.     a list literal, so that you can say:
  437.  
  438.     @foo = (
  439.         1,
  440.         2,
  441.         3,
  442.     );
  443.  
  444.     LISTs do automatic interpolation of sublists. That is, when a
  445.     LIST is evaluated, each element of the list is evaluated in a
  446.     list context, and the resulting list value is interpolated into
  447.     LIST just as if each individual element were a member of LIST.
  448.     Thus arrays lose their identity in a LIST--the list
  449.  
  450.     (@foo,@bar,&SomeSub)
  451.  
  452.     contains all the elements of @foo followed by all the elements
  453.     of @bar, followed by all the elements returned by the subroutine
  454.     named SomeSub when it's called in a list context. To make a list
  455.     reference that does *NOT* interpolate, see the perlref manpage.
  456.  
  457.     The null list is represented by (). Interpolating it in a list
  458.     has no effect. Thus ((),(),()) is equivalent to (). Similarly,
  459.     interpolating an array with no elements is the same as if no
  460.     array had been interpolated at that point.
  461.  
  462.     A list value may also be subscripted like a normal array. You
  463.     must put the list in parentheses to avoid ambiguity. For
  464.     example:
  465.  
  466.     # Stat returns list value.
  467.     $time = (stat($file))[8];
  468.  
  469.     # SYNTAX ERROR HERE.
  470.     $time = stat($file)[8];  # OOPS, FORGOT PARENTHESES
  471.  
  472.     # Find a hex digit.
  473.     $hexdigit = ('a','b','c','d','e','f')[$digit-10];
  474.  
  475.     # A "reverse comma operator".
  476.     return (pop(@foo),pop(@foo))[0];
  477.  
  478.     You may assign to `undef' in a list. This is useful for throwing
  479.     away some of the return values of a function:
  480.  
  481.     ($dev, $ino, undef, undef, $uid, $gid) = stat($file);
  482.  
  483.     Lists may be assigned to if and only if each element of the list
  484.     is legal to assign to:
  485.  
  486.     ($a, $b, $c) = (1, 2, 3);
  487.  
  488.     ($map{'red'}, $map{'blue'}, $map{'green'}) = (0x00f, 0x0f0, 0xf00);
  489.  
  490.     Array assignment in a scalar context returns the number of
  491.     elements produced by the expression on the right side of the
  492.     assignment:
  493.  
  494.     $x = (($foo,$bar) = (3,2,1));        # set $x to 3, not 2
  495.     $x = (($foo,$bar) = f());        # set $x to f()'s return count
  496.  
  497.     This is very handy when you want to do a list assignment in a
  498.     Boolean context, because most list functions return a null list
  499.     when finished, which when assigned produces a 0, which is
  500.     interpreted as FALSE.
  501.  
  502.     The final element may be an array or a hash:
  503.  
  504.     ($a, $b, @rest) = split;
  505.     local($a, $b, %rest) = @_;
  506.  
  507.     You can actually put an array or hash anywhere in the list, but
  508.     the first one in the list will soak up all the values, and
  509.     anything after it will get a null value. This may be useful in a
  510.     local() or my().
  511.  
  512.     A hash literal contains pairs of values to be interpreted as a
  513.     key and a value:
  514.  
  515.     # same as map assignment above
  516.     %map = ('red',0x00f,'blue',0x0f0,'green',0xf00);
  517.  
  518.     While literal lists and named arrays are usually
  519.     interchangeable, that's not the case for hashes. Just because
  520.     you can subscript a list value like a normal array does not mean
  521.     that you can subscript a list value as a hash. Likewise, hashes
  522.     included as parts of other lists (including parameters lists and
  523.     return lists from functions) always flatten out into key/value
  524.     pairs. That's why it's good to use references sometimes.
  525.  
  526.     It is often more readable to use the `=>' operator between
  527.     key/value pairs. The `=>' operator is mostly just a more
  528.     visually distinctive synonym for a comma, but it also arranges
  529.     for its left-hand operand to be interpreted as a string, if it's
  530.     a bareword which would be a legal identifier. This makes it nice
  531.     for initializing hashes:
  532.  
  533.     %map = (
  534.              red   => 0x00f,
  535.              blue  => 0x0f0,
  536.              green => 0xf00,
  537.        );
  538.  
  539.     or for initializing hash references to be used as records:
  540.  
  541.     $rec = {
  542.             witch => 'Mable the Merciless',
  543.             cat   => 'Fluffy the Ferocious',
  544.             date  => '10/31/1776',
  545.     };
  546.  
  547.     or for using call-by-named-parameter to complicated functions:
  548.  
  549.        $field = $query->radio_group(
  550.            name      => 'group_name',
  551.            values    => ['eenie','meenie','minie'],
  552.            default   => 'meenie',
  553.            linebreak => 'true',
  554.            labels    => \%labels
  555.        );
  556.  
  557.     Note that just because a hash is initialized in that order
  558.     doesn't mean that it comes out in that order. See the "sort"
  559.     entry in the perlfunc manpage for examples of how to arrange for
  560.     an output ordering.
  561.  
  562.   Typeglobs and Filehandles
  563.  
  564.     Perl uses an internal type called a *typeglob* to hold an entire
  565.     symbol table entry. The type prefix of a typeglob is a `*',
  566.     because it represents all types. This used to be the preferred
  567.     way to pass arrays and hashes by reference into a function, but
  568.     now that we have real references, this is seldom needed. It also
  569.     used to be the preferred way to pass filehandles into a
  570.     function, but now that we have the *foo{THING} notation it isn't
  571.     often needed for that, either. It is still needed to pass new
  572.     filehandles into functions (*HANDLE{IO} only works if HANDLE has
  573.     already been used).
  574.  
  575.     If you need to use a typeglob to save away a filehandle, do it
  576.     this way:
  577.  
  578.     $fh = *STDOUT;
  579.  
  580.     or perhaps as a real reference, like this:
  581.  
  582.     $fh = \*STDOUT;
  583.  
  584.     This is also a way to create a local filehandle. For example:
  585.  
  586.     sub newopen {
  587.         my $path = shift;
  588.         local *FH;    # not my!
  589.         open (FH, $path) || return undef;
  590.         return *FH;
  591.     }
  592.     $fh = newopen('/etc/passwd');
  593.  
  594.     Another way to create local filehandles is with IO::Handle and
  595.     its ilk, see the bottom of the "open()" entry in the perlfunc
  596.     manpage.
  597.  
  598.     See the perlref manpage, the perlsub manpage, and the section on
  599.     "Symbol Tables" in the perlmod manpage for more discussion on
  600.     typeglobs.
  601.  
  602.