Hacker News new | ask | show | jobs
by colomon 2793 days ago
print in Perl 6 works the same as print in Perl 5.

put/say are output functions which automatically add a newline at the end.

If you want fancier formatting, printf is available as well.

And notice the relative lengths of these names. As much as possible, p6 is organized so that the more commonly used thing will have the shorter name.

PS I don't think I have ever used put in p6. My code uses say, or print if I don't want a newline added to the output.

1 comments

As for the say function, it's also supposed to output text in a more human readable format. It calls the `.gist` of its argument, which you can override in user-defined classes. Take for example the following code snippet from Perl 6 Deep Dive:

    class Chemical { 
        has $.formula; 
        method gist { 
            my $output = $!formula; 
            $output ~~ s:g/(<[0..9]>)/{(0x2080+$0).chr}/; 
            $output; 
        }
     }


     my $chem = Chemical.new(formula => 'Al6O13Si2'); 
     say $chem; # Al₆O₁₃Si₂

     #`[
     Without overriding the `gist` method:

     say $chem; # Chemical.new(formula => 'Al6O13Si2')
     ]