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

Unexpected behavior of function 'say'

by farang (Friar)
on Mar 05, 2013 at 15:11 UTC ( #1021840=perlquestion: print w/ replies, xml ) Need Help??
farang has asked for the wisdom of the Perl Monks concerning the following question:

Greetings monks, new Perl hacker here, about three weeks into the journey, having fun and figuring out the lay of the land.

Alright, so perldoc -f say tells me this:

This keyword is available only when the "say" feature is enabled; see feature. Alternately, include a "use v5.10" or later to the current scope.

This leads me to believe the following code shouldn't work.

use strict; use warnings; say STDOUT 'like a charm';

Yet with an explicit filehandle, it does work (tested on v5.16 and v5.14). The question in this case is: why does the code work?

Comment on Unexpected behavior of function 'say'
Download Code
Re: Unexpected behavior of function 'say'
by toolic (Chancellor) on Mar 05, 2013 at 15:21 UTC

    Tip #6 from the Basic debugging checklist: B::Deparse:

    use warnings; use strict 'refs'; 'STDOUT'->say('like a charm');

    Not that I know what that means, but it is a clue.

    Also, when I run the code on 5.12, I get an error:

    Can't locate object method "say" via package "IO::File" at ...
Re: Unexpected behavior of function 'say'
by tobyink (Abbot) on Mar 05, 2013 at 15:22 UTC

    It's the indirect method call syntax. Your code is interpreted as:

    use strict; use warnings; STDOUT->say('like a charm');

    So you're not using the say keyword; you're using the say method in IO::Handle.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
Re: Unexpected behavior of function 'say'
by LanX (Abbot) on Mar 05, 2013 at 15:24 UTC
    hmm not in 5.10

    lanx@nc10-ubuntu:~$ perl use strict; use warnings; say STDOUT 'like a charm'; Can't locate object method "say" via package "IO::Handle" at - line 4.

    But the last line should lead to the answer.

    print FH is (supposed to be) indirect object syntax of the filehandle-object, so somehow the method say was activated by default in your environment.

    Anyway calling say w/o FH shouldn't work!

    Cheers Rolf

      This is because of a change in Perl 5.14:

      Filehandle method calls load IO::File on demand

      The OP's code does work in Perl 5.10, if you run it with the -MIO::Handle parameter to manually load the IO::Handle package.

      package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
        So print FH was magic in doing so prior to 5.14?

        I hate breakes in symmetry...

        BTW: I checked IO::Handle , it's a normal method and features aren't checked.

        sub say { @_ or croak 'usage: $io->say(ARGS)'; my $this = shift; local $\ = "\n"; print $this @_; }

        Cheers Rolf

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-04-17 03:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (439 votes), past polls