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

So, here's what I am trying to do. I am trying to obtain information about a computer and print it to a file. My approach is to use 'qq//' to put and format the information collected and then print it as a block to a file. I wanted to issue a function call to neatly place stars in the document to separate each data segment (see my failed print "*"x 80 line). How do you make a function call in qq//?

sub find_system_info { my $file_handle = shift; my $kernal_release = qx/uname -r/; my $partition_list = qx/fdisk/; my $disk_space = qx/df/; my $active_interfaces = qx/ifconfig/; my $output_basic = qq( + + + + 'print "*" x80' + + Kernal Release: $kernal_release + + + ` Partition List: + + + + $partition_list + + + Disk Space: + + $disk_space + + + + + + Network Interfaces: + + $active_interfaces + + + + + + + + + + ); print $file_handle $output_basic; }

Replies are listed 'Best First'.
Re: Function call inside qq//
by choroba (Cardinal) on Apr 02, 2014 at 13:44 UTC
    You can use the "pram" operator:
    $output = qq( @{[ '*' x 80 ]} );

    but it might be easier to assign the start to a variable before creating the output:

    my $STARS = '*' x 80; my $output = qq( $STARS );

    Using print would print the line of stars in the moment of assignment, and insert the return value of print (hopefully 1) into the created string.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Function call inside qq//
by AnomalousMonk (Archbishop) on Apr 02, 2014 at 17:35 UTC

    In addition to the 'pram' operator (also known as the Baby Cart From Hell operator), there's something like:

    c:\@Work\Perl\monks>perl -wMstrict -le "sub stars { return \ ('*' x $_[0]); } ;; my $str = qq{foo-${ stars(5) }-bar}; print qq{'$str'}; " 'foo-*****-bar'

    But why bother? Just use good old sprintf; that's what it's there for.

    c:\@Work\Perl\monks>perl -wMstrict -le "sub stars { return '*' x $_[0]; } ;; my $str = sprintf 'foo%s%sbar', stars(5), '=' x 7; print qq{'$str'}; " 'foo*****=======bar'

    Update: Added sprintf example, Baby Cart WP link.