Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^7: perl 5.12 BSD portability (CPAN test result)...print

by ikegami (Pope)
on Mar 16, 2013 at 18:35 UTC ( #1023843=note: print w/ replies, xml ) Need Help??


in reply to Re^6: perl 5.12 BSD portability (CPAN test result)...print
in thread perl 5.12 BSD portability (CPAN test result)...print

Assuming you're ok with breaking every program that does print $x, how would you implement IO::Handle::print without the print operator?

There are other issues, but they're not worth mentioning after the above two.


Comment on Re^7: perl 5.12 BSD portability (CPAN test result)...print
Download Code
Re^8: perl 5.12 BSD portability (CPAN test result)...print
by perl-diddler (Hermit) on Mar 16, 2013 at 20:06 UTC
    Um, I suppose I'm not sure I see the difference between a method called print, called indirectly, that appears to do the same thing as C<print> the operator.

    Could you explain the difference to someone to whom it is not obvious? ;-)

      Without an explicit filehandle, where does the output of print $x go? How is Perl to tell the difference between "print the contents of $_ to the filehandle in $x" from "print the contents of $x to the currently selected filehandle"?

        You mean like this:
        perl -e 'use P; my $x=27; P $x; my $y=\*STDOUT; P $y,"through STDOUT=%s", $x' 27 through STDOUT=27
        What it can't easily do is, both:
        P $x #and P STDOUT $x #without the comma
        where $x is not an object. That takes "special parsing"...but if the right prototype element was created to do that.. if not an object, then pass as a scalar), that would work too. I.e. I can get the 2nd case to work, but then the 1st case doesn't.

        So is the only reason print has to be an operator is to handle printing to a <FH> w/o the comma, since the first case -- telling the difference between "$X" as a var and $x as a FH is fairly straightforward.

        Note -- if you really wanted to print FROM file handle "$x", I'd have to add code for that...but if the file is open for READ/WRITE -- no way to tell.

        FWIW, P does print from File handles (but not as the first arg or w/o a format statement). It reads the file handle and prints the contents as part of the output:

        # perl -e 'use P; P \*STDOUT, "Input was: %s", \*STDIN; ' my input Input was: <*=my input >

      So you want me to explains how it's impossible to write the method without the print operator? Well, why don't you try it.

      Or are you asking me to explain why they don't do the same thing? Well, consider

      >perl -le"$x = 'abc'; print $x;" abc

      As you can see, it doesn't call the method $x of class abc. If it was an indirect method call, it would have the following outcome:

      >perl -le"$x = 'abc'; $x->print;" Can't locate object method "print" via package "abc" (perhaps you forg +ot to load "abc"?) at -e line 1.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2014-09-20 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (160 votes), past polls