Reputation: 39
I have one file which contain device name and it's an output from shell script depending upon region.So region is variable which can be change....e.g devicelist=device.region
Now I want to add this device list into email body. For sending email I am using perl script and I have tried below method but its not working...
my $file = "$ENV{devicelist}";
open my $fh, '<', $file;
print while (<$fh>);
I am getting this message as : GLOB(0x11aeea8)
Perl script ....
my $file = "/tmp/devicelist";open my $fh, '<', $file;print while (<$fh>);
$logger->debug("$logid >> Device names is $deviceinfo");
$smtp->datasend("Customer Name : $custname\n\n");
$smtp->datasend("Site Location : $sitename\n\n");
$smtp->datasend("Region : $region\n\n");
my $file = "/tmp/devicelist";
open my $fh, '<', $file;
print while (<$fh>);
$smtp->datasend("Device Info : $deviceinfo\n\n"); 1st way
$smtp->datasend("Device Info perl : $fh\n\n"); 2nd way
This request is dealing where i am sending email when there are more than 10 device down and I want to present those 10 devices names. Other information are showing perfectly fine as those are single value stored in variable like region,state ..etc...
Thanks
Upvotes: 1
Views: 275
Reputation: 45321
Printing by default goes to standard out, and not to the same place where $smtp->data send()
is sending string parameters.
I think you just want to re-write your while loop and direct the output correctly; so...
print while (<$fh>); #is basically saying:
while (<$fh>) {
print;
} #but in your smtp block, you don't want to be printing, so write
foreach $fileline (<$fh>) { $smtp->datasend($fileline); }
Once you try that, if it's working and therefore this explains what was wrong, then look into slurping the whole file in at once with something like:
my $file = "/tmp/devicelist";
open my $fh, '<', $file;
my $filecontents; { local $/; $filecontents = <$fh>; }
$smtp->datasend($filecontents);
Other than that, when you say:
print while (<$fh>);
I am getting this message as : GLOB(0x11aeea8)
Do you mean, you didn't want to get GLOB(0x11aeea8) or that this is correct output? If it's the former, I think that's because you wanted to write something like:
while (<$fh>) {print $_;}
Upvotes: 0
Reputation: 104
All you need is to change
print while (<$fh>);
to
$smtp->datasend($_) while (<$fh>);
Upvotes: 1
Reputation: 385590
Are you asking how to load a file's content into a variable?
my $file; { local $/; $file = <$fh>; }
Upvotes: 0