Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

print and say

by Anonymous Monk
on Sep 12, 2017 at 09:08 UTC ( #1199173=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all!
I am new to Perl and I am trying to work with a snippet of code I found. The code is not important, the gist is that it has a function and this function is called to produce some results:
use feature qw( say ); sub myfunct{ ..... return $result; } say for myfunct($str1, $str2);

The code works just fine and it prints the result on the screen, what I cannot understand are the following:
1) How can I store the result of the function instead of printing it?
2) What is the difference between say and print?

Replies are listed 'Best First'.
Re: print and say
by choroba (Bishop) on Sep 12, 2017 at 09:12 UTC
    1) What do you mean by "store"? If you want to store it in a variable, assign the function call to the variable:
    my $result = myfunct($str1, $str2);

    To store it in a file, print to a file:

    open my $OUT, '>', $filename or die $!; say {$OUT} myfunct($str1, $str2); close $OUT;

    2) Documented in say:

    Just like print, but implicitly appends a newline. say LIST is simply an abbreviation for { local $\ = "\n"; print LIST }.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: print and say
by LanX (Bishop) on Sep 12, 2017 at 09:12 UTC
    1) store?

     push @array, $_ for myfunct($str1, $str2);  

    the postfix-for automatically uses $_ as loop variable.

    But this looks weird, is your function returning a list?

    2) say vs print

    say starts a new line (inserts a "\n" at the end)

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

Re: print and say
by karlgoethebier (Monsignor) on Sep 12, 2017 at 10:05 UTC

    Perhaps you want sprintf?

    #!/usr/bin/env perl # $Id:,v 1.3 2017/09/12 09:55:38 karl Exp karl $ # use strict; use warnings; use feature qw(say); my $my_nose = sprintf( "%s", nose(q(cuke)) ); say $my_nose; sub nose { shift; } __END__

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: print and say
by Anonymous Monk on Sep 12, 2017 at 09:13 UTC
    Ok, for #2 I found the answer, say just prints a newline by default.
    So I am basically down to figuring out why I cannot store the result using:
    $answer = myfunct($str1, $str2);

    It does not print anything, while with:
    say for myfunct($str1, $str2);

    print for myfunct($str1, $str2);

    it gives me the result.

      Anonymous Monk:

      Your first code snippet stores the result in $answer, but you haven't told it to print yet. Here's a quickie bit of code to show you a few variations on the theme:

      use strict; use warnings; use v5.20; my ($a_val, @some_vals); # Store a scalar $a_val = scalar_func(10); # Print it later say "A random number from 1 .. 10:"; say $a_val; # Store a list @some_vals = list_func(5, 10); say "Print a list of values, each on one line"; say for @some_vals; say "Print all the values on a single line"; say @some_vals; say "Print all of them on a single line, with commas"; say join(", ", @some_vals); # return a random integer from 1 to the specified number sub scalar_func { my $range = shift; return 1 + int($range * rand); } # return $count random numbers from 1 .. $range sub list_func { my ($count, $range) = @_; return map { scalar_func($range) } 1 .. $count; }

      Will print something like:

      A random number from 1 .. 10: 1 Print a list of values, each on one line 3 1 10 10 3 Print all the values on a single line 3110103 Print all of them on a single line, with commas 3, 1, 10, 10, 3

      Also, LanX is right: you don't want to use the 'for' clause when your function returns a single value. It doesn't do anything wrong in this case, but it's misleading: When reading the code, it implies that your function is returning a list of results.


      When your only tool is a hammer, all problems look like your thumb.

      for does a loop over one element, that's nonsense.

      $answer = myfunct($str1, $str2); say $answer;

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        Disregarding efficiency, a for loop over one element can be "useful" as a "topicalizer", i.e. locally setting $_ and shortening code. It's suggested by the Perl documentation in Switch Statements for use with the experimental when.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1199173]
Approved by choroba
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2018-05-23 15:28 GMT
Find Nodes?
    Voting Booth?