in reply to Hidden Secrets of PERL

C has lot of reference materials and Books to explore the hidden treasures. Does PERL has any books/Reference to explore its hidden treasures.

It has excellent documentation in addition to many books.

I am looking something like,
$a = 10; $b = 20; print $a +$b;

perldoc -f print.

This code will not print 30 as there is ambiguity in the print statement. It treats $a as file handle and + before the $b as prefix operator and tries to print $b to $a.

There is no ambiguity, although one has to admit that the case may be confusing. But it's perfectly well documented.

Is there any place where these kind of hidden treasures are listed ?

Again, in the docs, in which as a perl_lover you will also be delighted to discover that it's also explained why there's not such a thing as PERL: see perldoc -q difference between "perl" and "Perl".

Replies are listed 'Best First'.
Re^2: Hidden Secrets of PERL
by bart (Canon) on Oct 11, 2006 at 10:50 UTC
    There is no ambiguity, although one has to admit that the case may be confusing. But it's perfectly well documented.
    Eh, no... there is ambiguity as the syntax of this piece of code is ambiguous for the grammar.

    Only, Perl has a well documented way to disambiguate this case.

    Other ambiguities include code block versus anonymous hash, and the opening slash for regexes for optional function arguments:

    map { $_ => length } @list # code block or hash?
    rand /2 /m # division or regex?

    Occasionally, in particular for the former example, Pelr makes an initial guess and guesses wrong, producing a syntax error further down in the parsing stream — too late to go back and try the other route. It requires a well known disambiguation trick to resolve the problem, early enough:

    +{ $_ => length } # hashref { ; $_ => length } # code block
      Only, Perl has a well documented way to disambiguate this case.

      If it does, it certainly doesn't say so in the documentation of print. This is the documentation of print (from 5.8.5):

             print FILEHANDLE LIST
             print LIST
             print   Prints a string or a list of strings.  Returns true if success-
                     ful.  FILEHANDLE may be a scalar variable name, in which case
                     the variable contains the name of or a reference to the file-
                     handle, thus introducing one level of indirection.  (NOTE: If
                     FILEHANDLE is a variable and the next token is a term, it may
                     be misinterpreted as an operator unless you interpose a "+" or
                     put parentheses around the arguments.)  If FILEHANDLE is omit-
                     ted, prints by default to standard output (or to the last
                     selected output channel--see "select").  If LIST is also omit-
                     ted, prints $_ to the currently selected output channel.  To
                     set the default output channel to something other than STDOUT
                     use the select operation.  The current value of $, (if any) is
                     printed between each LIST item.  The current value of "$\" (if
                     any) is printed after the entire LIST has been printed.
                     Because print takes a LIST, anything in the LIST is evaluated
                     in list context, and any subroutine that you call will have one
                     or more of its expressions evaluated in list context.  Also be
                     careful not to follow the print keyword with a left parenthesis
                     unless you want the corresponding right parenthesis to termi-
                     nate the arguments to the print--interpose a "+" or put paren-
                     theses around all the arguments.
      
                     Note that if you're storing FILEHANDLES in an array or other
                     expression, you will have to use a block returning its value
                     instead:
      
                         print { $files[$i] } "stuff\n";
                         print { $OK ? STDOUT : STDERR } "stuff\n";
      

      The only part that hints about ambiguaty is the part I boldfaced. But it doesn't explain at all why it parses the statement differently whether there's a space between the third and fourth token. Not that I expect most programmers to know what the phrase next token is a term means.