Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answers from Greg Hewgill and ~unutbu are correct. I just wanted to add that it's best not to pre-declare variables, that may have helped a tad in understanding what was wrong.</p> <p>Here is your identical code with some slight changes:</p> <pre><code>#!/usr/bin/perl # import packages use Net::POP3; use Getopt::Long; use Mail::Message; use List::Util qw(shuffle); use strict; use warnings; # read command line options # display usage message in case of error my ($host, $user, $pass); GetOptions ('h|host=s' =&gt; \$host, 'u|user=s' =&gt; \$user, 'p|pass=s' =&gt; \$pass) or die("Input error. Try calling me with: -h &lt;host&gt; -u &lt;username&gt; -p &lt;password&gt;"); # file operations open (my $email_file, "&gt;&gt;", "Mail.txt") or die ("Error opening Mail.txt for write: $!"); open (my $random_name, "&lt;", "out.txt") or die ("Error opening out.txt for read: $!"); my @array = shuffle(&lt;$random_name&gt;); close($random_name); # initiate connection # default timeout = 120 sec my $conn = Net::POP3-&gt;new($host) or die("ERROR: Unable to connect.\n"); # login my $numMsg = $conn-&gt;login($user, $pass) or die("ERROR: Unable to login.\n"); # get message numbers # iterate over list and print first 20 lines of each if ($numMsg &gt; 0) { my $msgList = $conn-&gt;list(); foreach my $msg (keys(%$msgList)) { my $rawdata = $conn-&gt;get($msg); my $msg_obj = Mail::Message-&gt;read($rawdata); my $body = $msg_obj-&gt;body; print $email_file $body; print $email_file "\n====================================================\n"; print shift @array; } } else { print "Mailbox is empty.\n"; } # close connection $conn-&gt;quit(); close($email_file) or die "Error closing Mail.txt from write: $!"; </code></pre> <ul> <li>I removed the predeclaration of variables.</li> <li>I changed the two opens to both use parenthesis and both check for errors.</li> <li>I moved declaring and setting @array to just after out.txt is opened.</li> <li>Since $random_file is not needed after @array is set, I close it on the next line.</li> <li>Lastly, I check for errors when closing Mail.txt which was opened for writing. It is very important to check the return value of close on a file you opened for writing as certain errors, like running out of disk space while writing to the file, will not be seen in the initial open but will be visible by checking that close($fh) returned true.</li> </ul> <p>There is still room for improvement but those were the biggies. I must say though, your code was a pretty good start for someone new to Perl. Using use strict and warnings, the foreach loop to iterate over the keys of the hash as well as Getopt::Long vs trying to parse command line arguments yourself are good to see.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload