Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: Comma's and blocks

by zer (Deacon)
on Oct 04, 2007 at 06:30 UTC ( #642577=note: print w/replies, xml ) Need Help??


in reply to Re: Comma's and blocks
in thread Comma's and blocks

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?

Replies are listed 'Best First'.
Re^3: Comma's and blocks
by erroneousBollock (Curate) on Oct 04, 2007 at 06:35 UTC
    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

Re^3: Comma's and blocks
by mwah (Hermit) on Oct 04, 2007 at 09:41 UTC
    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://642577]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2022-08-08 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?