1

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

3 Answers 3

1

All you need is to change

print while (<$fh>);

to

$smtp->datasend($_) while (<$fh>);
0

Are you asking how to load a file's content into a variable?

my $file; { local $/; $file = <$fh>; }
2
  • Thanks for the response ... Do you mean I should try like ... my $file = "/tmp/devicelist"; my $file; { local $/; $file = <$fh>; } $smtp->datasend("Device Info perl : $fh\n\n");
    – user28104
    Dec 31, 2012 at 5:21
  • 1
    $fh is a file handle. You can't send a file handle! I presume you want to send the contents of the file, so I showed you how to get the contents of the file. You haven't changed anything in what you posted in your comment.
    – ikegami
    Dec 31, 2012 at 6:33
0

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 $_;}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.