Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

[solved] autoflush STDOUT 1 does not need import?

by basiliscos (Pilgrim)
on Sep 14, 2015 at 12:02 UTC ( [id://1141914]=perlquestion: print w/replies, xml ) Need Help??

basiliscos has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks!

I do not understand, why the following code

use strict; use warnings; autoflush STDOUT 1;
Works without any import? I expected that strict/warnings should complain, unless I have something like:
use strict; use warnings; use IO::Handle; autoflush STDOUT 1;

The autoflush isn't listed among perl build-in functions. Why it is imported by default? Where (perldoc) I can read about it and similar surprises?

WBR, basiliscos.

Replies are listed 'Best First'.
Re: autoflush STDOUT 1 does not need import?
by choroba (Cardinal) on Sep 14, 2015 at 12:42 UTC
    Globs (and STDOUT is one of them) contain an IO object, even if its class hasn't been used. This blogpost describes the hairy details of the situation and gives advice on how to behave.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: autoflush STDOUT 1 does not need import?
by CountZero (Bishop) on Sep 14, 2015 at 12:53 UTC
    In Perl-5.12-delta we read "Filehandles are now always blessed into IO::File". Hence the methods of IO::Handle are available.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: autoflush STDOUT 1 does not need import?
by nevdka (Pilgrim) on Sep 14, 2015 at 12:33 UTC

    My understanding of this is that you're using indirect object notation, calling the autoflush method on the STDOUT file handle. That line is essentially the same as:

    STDOUT->autoflush(1)

    You don't need to import anything to call methods on objects. STDOUT is an object that supports the autoflush method, so you don't need to import anything to get it.

      OMG, indeed! I was sure that this was used in early releases of Perl5, and should no longer present in modern Perl.

      WBR, basiliscos.
Re: autoflush STDOUT 1 does not need import?
by shmem (Chancellor) on Sep 14, 2015 at 14:23 UTC

    When a method which cannot be resolved is invoked on a file handle, perl loads modules from the IO namespace to resolve it. The notation autoflush STDOUT 1 is equivalent to STDOUT->autoflush(1), which triggers the loading of IO::Handle:

    qwurx [shmem] ~> perl -e 'use strict; use warnings; print$_.$/ for sor +t keys %INC; autoflush STDOUT 1' strict.pm warnings.pm qwurx [shmem] ~> perl -e 'use strict; use warnings; STDOUT->autoflush( +1); print$_.$/ for sort keys %INC' Carp.pm Exporter.pm Fcntl.pm IO.pm IO/File.pm IO/Handle.pm IO/Seekable.pm SelectSaver.pm Symbol.pm XSLoader.pm strict.pm warnings.pm

    This is documented in Method Call Variations in perlobj.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1141914]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-25 18:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found