Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You seem a bit new to Perl, so I will give you a thorough explanation why your code is bad and how you can improve it:</p> <h3>1 Naming conventions:</h3> <p>I see that this seems to be symbolic code, but <code>$emailgoodintheory</code> is far less readable than <code>$emailGoodInTheory</code> or <code>$email_good_in_theory</code>. Pick any scheme and stick to it, just don't write all lowercase.</p> <p>I suppose that <code>$emailgoodintheory</code> holds a single email address. Then applying the regex <code>s/\s//g</code> or the transliteration <code>tr/\s//</code> will be enough; space characters are not case sensitive.</p> <p>Using a module to validate adresses is a very good idea. :-)</p> <h3>2 Perl Data Types</h3> <p>Perl has three man types of variables:</p> <p><em>Scalars</em> can hold strings, numbers or references. They are denoted by the <code>$</code> sigil.</p> <p><em>Arrays</em> can hold an ordered sequence of Scalars. They are denoted by the <code>@</code> sigil.</p> <p><em>Hashes</em> can hold an unordered set of Scalars. Some people tend to know them as dicitonaries. All keys and all values must be Scalars. Hashes are denoted by the <code>%</code> sigil.</p> <p>A word on <em>context</em>: When getting a value/element from a hash/array, you have to change the sigil to the data type you want. Usually, we only recover one value (which always is a scalar), so you write <code>$array[$i]</code> or <code>$hash{$key}</code>. This does not follow any references so</p> <pre><code> my $arrayref = [1, 2, 3]; my @array = ($arrayref); print @array[0]; </code></pre> <p>will not print <code>123</code>, but <code>ARRAY(0xABCDEF)</code> and give you a warning.</p> <h3>3 Loops in Perl:</h3> <p>Your loop syntax is very weird! You can use C-style loops:</p> <pre><code>for (my $i = 0; $i &lt; @array; $i++) </code></pre> <p>where <code>@array</code> gives the length of the array, because we have a <em>scalar context</em>. You could also give <code>$i</code> the range of all possible indices in your array:</p> <pre><code>for my $i (0 .. $#array) </code></pre> <p>where <code>..</code> is the range operator (in <em>list context</em>) and <code>$#array</code> gives the highest available index of our array. We can also use a <em>foreach</em>-loop:</p> <pre><code>foreach my $element (@array) </code></pre> <p>Note that in Perl, the keywords <code>for</code> and <code>foreach</code> are interchangeable.</p> <h3>4 What your loop does:</h3> <pre><code>foreach my $address(@toarraya) { print qq~ $address[$arrcnt]&lt;br /&gt; ~: $arrcnt++; } </code></pre> <p>Here you put each element of <code>@toarraya</code> into the scalar <code>$address</code>. Then you try to use it as an array (wrong!) and get the index <code>$arrcnt</code> out of it. This does not work; I hope your program <code>die</code>d.</p> <p>You can use every loop type given above (you don't need to count manually), but the standard <code>foreach</code> loop will suit you best:</p> <pre><code>foreach my $address (@toarraya){ print "$address&lt;br/&gt;\n"; } </code></pre> <p>A note on quoting syntax: while <code>qq~ quoted ~</code> is absolutely legal, this is the most obfuscated code I have seen today. The standard quote <code>"</code> would suffice, and when using <code>qq</code>, try to use some sort of parenthesis (<code>({[&lt;|</code>) as delimiter.</p> <h3>5 complete code:</h3> <p>I assume you wanted to write this:</p> <pre><code>my @addressList = split /\r?\n/, CGI::param('toarray'); foreach my $address (@addressList) { # eliminate white spaces $address =~ s/\s//g; # Test for validity unless (Email::Valid-&gt;address($address)) { # complain, die, you decide # I recommend: print "&lt;strong&gt;Invalid address »$address«&lt;/strong&gt;&lt;br/&gt;"; next; } print "$address&lt;br/&gt;\n"; # send that email } </code></pre> <p>And never forget to <code>use strict; use warnings;</code> and possibly <code>use utf8</code>.</p>
    singulars
    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