Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Comma's and blocks

by erroneousBollock (Curate)
on Oct 04, 2007 at 06:05 UTC ( [id://642572]=note: print w/replies, xml ) Need Help??


in reply to Comma's and blocks

Why are the comma's excluded in some spots and not others?
They're not excluded... you may have completely misunderstood some of the syntactic forms.

map {/dothis/} @variable; grep {/more stuff/} @more;
That's roughly equivalent to:
map(sub {/dothis/}, @variable); grep(sub {/more stuff/}, @more);
except that map() and grep() play with $_ on your behalf.

As for:

print A "hello"; print $somevar, "can be seperated like this";
The second print statement is equivalent to
print STDOUT $somevar, "can be seperated like this";
.. unless you've called select() to change which is the default filehandle.

How does this work with your own prototypes and subroutines?
Prototypes allow the Perl parser to apply (for your subs) the same parser tricks it employs on built-in functions (like push()).

Which perldoc covers this in depth?
perlsyn, perlsub, perlfunc should do it.

-David

Replies are listed 'Best First'.
Re^2: Comma's and blocks
by zer (Deacon) on Oct 04, 2007 at 06:30 UTC
    David,

    Thanks for the response! The map and grep is a lot clearer (kind of).

    #ex 1 map (sub {/dothis/},@_); map {/and this/}, @_; #now doesnt compile #ex 2 print STDOUT "hello"; print <STDOUT> , "Hello"; #runs
    In the above example. The first line is making sense and opened my eyes to that for the map. As well the print uses a comma when a proper file handle '<' and '>' are used. Why do the use of proper terms affect the use of commas?
      print <STDOUT> , "Hello"; #runs
      That doesn't do what you think.

      <STDOUT> reads from the STDOUT filehandle (which won't do anything useful).

      You can't put a comma between the file-handle and the printable content, the syntax is not short for any other syntax.

        print HANDLE expression

      is the normal form.

        print expression

      is parsed by perl to mean   print LATESTHANDLE expression

      where LATESTHANDLE is the last handle given to select() (the default is STDOUT).

      -David

      zerAs well the print uses a comma when a
      proper file handle '<' and '>' are used. Why do the use of proper
      terms affect the use of commas

      As has been said in another response,
      the  print <STDOUT> , "Hello";
      invokes reading a filehandle in list context and printing
      the result afterwards - but *not* through your "proper file handle" ;-)
      It has probably also been noted elswhere
      that this is like  print STDOUT <STDOUT>, "Hello";

      The notation print filehandle LIST stems (IIRC) from the
      "indirect object notation", like  $q = new CGI; #(no comma)
      which would, in "direct" object notation read:  $q = CGI->new()

      From this point of view, the file example might be equivalent
      to  STDOUT->print("Hello"); #, which indeed is the case.
      The above idioms can be used in Perl after including the IO::Handle
      module, then it'll look like (pseudocode):
      # from http://perldoc.perl.org/IO/Handle.html $io = new IO::Handle; # indirect object notation +! if ($io->fdopen(fileno(STDOUT),"w")) { $io->print("Hello"); # direct object notation! }
      See: IO::Handle documentation

      Regards
      mwa

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://642572]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2024-04-25 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found