0

So I have never used Perl prior to today, and am only doing so because my professor gave an example on this assignment with Perl. I took a picture of his code and am using exactly that, but it is not working for me

#!/usr/bin/ perl

open(FFF, "<usb256.001");
while(<FFF>)
{
    if ( /([a-z0-9_.]+\@[a-z0-9_.]+\.[a-z0-9_](2,))/gi )
    {
        $email = $1;
        print = "$email\n";
    }
}
close(FFF);

it says Can't modify print in scalar assignment at datafinder.pl line 9, near ""$email\n";" which I do not understand. My professor was using linux and I am using windows, so I don't know if that may be causing issues. Also, I don't know what "gi" means at the end of the if statement, if anyone could give insight on that as well Thanks for any help given!

dcalvert
  • 61
  • 1
  • 9
  • Just `print "$email\n";` is sufficient; this isn't a platform-sensitive problem. `gi` means "global" and "case insensitive" on the regex pattern and isn't Perl-specific. VTC-ing as no longer reproducible or resolved in a way unlikely to help future visitors. Also, always `use strict;` and `use warnings;` at the top of your script. – ggorlen Feb 23 '20 at 19:28
  • @ggorlen Ah yes that was simply a typo by me, didn't mean to put the =. Also, it now compiles but it does not print anything, do you see any problem with this as to why it may not match to any emails? – dcalvert Feb 23 '20 at 19:35
  • 1
    No way to say without seeing a relevant snippet of your input. `(2,)` looks wrong though. Do you really want to match a literal `2,`? If you want to match 2 or more of the previous `[a-z0-9_]` alternation, use `{2,}`. Also, [writing genuine email regex is non-trivial](https://stackoverflow.com/questions/201323/how-to-validate-an-email-address-using-a-regular-expression). – ggorlen Feb 23 '20 at 19:36
  • 1
    @ggorlen that was it! I got it to work. Thank you so much for your help – dcalvert Feb 23 '20 at 19:54
  • the /g isn't doing anything useful here; perhaps your professor meant `while`, not `if`? otherwise, it is just printing the first email address on each line (and reading a USB image by line is a weird thing to do anyway) – ysth Feb 23 '20 at 20:21
  • I understand this is an assignment, but in general I recommend [Email::Address::XS](https://metacpan.org/pod/Email::Address::XS) to parse emails or lists of emails. – Grinnz Feb 25 '20 at 21:00

0 Answers0