Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Printing an array in a Here Doc

by tmtech1 (Initiate)
on Jun 27, 2006 at 19:50 UTC ( [id://557861]=perlquestion: print w/replies, xml ) Need Help??

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

I'm new to perl. I was given a script to modify that should append the output below (as a result of a cmd run) to an existing log file and also save it to a variable/array to then print out in an email. I'm not sure how to print each line (in the array) from within the *here* doc. Please advise. Thanks!

OUTPUT:
Multiutil: The MASTER_UPDATE_PACKET packet sent from BLFST_REPLICA at 2006-06-22 22:45:07 for 'MASTR' cannot be replayed: This replica has not replayed epoch 8757 from replica BLFST_REPLICA, it has only replayed through 8756.
Multiutil: Packet E:\MultiSite_Packets\ClearQuest\var\shipping\ms_ship\incoming\sync_BLFST_REPLICA_22-June-06_23-45-07.xml not processed...

CODE:
$hostname = `hostname`; use Net::SMTP; @Output = ""; @OutputDetails = ""; $Log_File = "E:\\Backups\\LOGs\\Sync_Import.log"; open (STDOUT,">>$Log_File"); open (Output,"multiutil <cmd>|"); while(<Output>) { $TheLine = $_; chomp($TheLine); #Get rid of line break push (@OutputDetails, $TheLine); } Mail_Body; Mail_Msg; sub Mail_Body{ $body0 = "This message sent by the Import Process"; $body1 = "\n $TheLine\n"; <==== REPLACE w/@OutputDetails } sub Mail_Msg{ $smtp = Net::SMTP->new( "mail.company.com" ); $smtp->mail( "ALIAS\@company.com" )"; $smtp->to('ALIAS\@company.com'); $smtp->data(); $smtp->datasend("Subject:Job Failure\n"); $smtp->datasend(<<EOF_MAIL $body0 $body1 <==== THIS SHOULD BE EVERY LINE IN @OutputDetails EOF_MAIL ); $smtp->datasend(); $smtp->quit(); }

Replies are listed 'Best First'.
Re: Printing an array in a Here Doc
by Fletch (Bishop) on Jun 27, 2006 at 20:09 UTC
    my @array = qw( what happened when you tried it? ); print <<EOT; @array EOT

    It works fine; you may just want newlines not spaces between elements (in which case see the documentation for $" in perlvar).

      I do need newlines. But, I couldn't figure out the exact place to put the loop. Could I put it inside of the here doc?
        No. Whatsoever control statements are not evaluated in a here-doc. The here-doc is essentially a string, and if you place an array inside it, it is interpolated into the string. Yes, there is looping over the array elements going on behind the scenes, and in this loop the array elements are joined with the contents of the variable $" as glue.

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Printing an array in a Here Doc
by esskar (Deacon) on Jun 27, 2006 at 20:28 UTC
    hi, i tried to cleanup your code
    use strict; # safety first use Net::SMTP; my $hostname = `hostname`; my $Log_File = "E:\\Backups\\LOGs\\Sync_Import.log"; open( STDOUTSAVE, ">&STDOUT" ); # save the original STDOUT open( STDOUT, ">>$Log_File" ) or die "Unable to open $Log_File: $!"; # open (Output, "multiutil <cmd>|"); # do not see the nameing; better open( PIPE, "multiutil <cmd>|" ) or do { # save the last open error my $err = $!; # set back to saved STDOUT open( STDOUT, ">&STDOUTSAVE" ); die "Unable to open pipe: $err"; }; my @OutputDetails = map { s!\r?\n!!g; "$_\n" } <PIPE>; # add the line to the front unshift @OutputDetails, "This message sent by the Import Process\n\n"; close PIPE; # close the handle again Mail_Msg(); # set back to saved STDOUT open( STDOUT, ">&STDOUTSAVE" ); sub Mail_Msg { my $smtp = Net::SMTP->new("mail.company.com"); $smtp->mail('ALIAS@company.com'); $smtp->to('ALIAS@company.com'); $smtp->data(); $smtp->datasend(" Subject: Job Failure \n "); $smtp->datasend( <<EOF_MAIL @OutputDetails EOF_MAIL ); $smtp->datasend(); $smtp->quit(); }
    but did not really test it. hope you get the idea.
    have fun.

    UPDATE: minor code changes
      Will try this if the suggested join doesn't work as expected. Thanks All!
Re: Printing an array in a Here Doc
by jdtoronto (Prior) on Jun 27, 2006 at 20:16 UTC
Re: Printing an array in a Here Doc
by jwkrahn (Abbot) on Jun 27, 2006 at 22:34 UTC
    It looks like you need to put the contents of "multiutil <cmd>" into a single string:
    use warnings; use strict; use Sys::Hostname; use Net::SMTP; my $hostname = hostname; my $Log_File = "E:/Backups/LOGs/Sync_Import.log"; open LOG, '>>', $Log_File or die "Cannot open '$Log_File' $!"; select LOG; # Make LOG the default output filehandle my $body0 = 'This message sent by the Import Process'; my $body1 = `multiutil <cmd>`; sub Mail_Msg{ $smtp = Net::SMTP->new( 'mail.company.com' ); $smtp->mail( 'ALIAS@company.com' ); $smtp->to( 'ALIAS@company.com' ); $smtp->data(); $smtp->datasend( "Subject:Job Failure\n" ); $smtp->datasend( <<"EOF_MAIL" ); $body0 $body1 EOF_MAIL $smtp->datasend(); $smtp->quit(); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://557861]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-18 09:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found