hcherry has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/local/bin/perl -w use Email::Sender::Simple qw(sendmail); use Email::Simple; use Email::Simple::Creator; open (IN, 'top.log'); $input=<IN>; print $input; $lngth=length($input); print $lngth; $load = substr($input,16,4); print $load; $value=.1 if ($load > $value) { # print "$load greater than $value"; my $email = Email::Simple->create( header => [ To => '"Hardy Cherry" <hcherry@youngliving.com>', From => '"Hardy Cherry" <hcherry@youngliving.com>', Subject => "Load g", ], body => "$load greater than $value.\n", ); sendmail($email); }
"my" variable $email masks earlier declaration in same scope at hcshort.pl line 26. syntax error at hcshort.pl line 15, near ") {" syntax error at hcshort.pl line 28, near "}" Execution of hcshort.pl aborted due to compilation errors. I intend to use sendmail more than once but I'm a newbie in perl so I don't understand why I'm getting a syntax error and why the ()and {} seem to be ignored.

Replies are listed 'Best First'.
Re: understanding scope and () and {}
by ikegami (Patriarch) on Jan 27, 2011 at 18:06 UTC

    The OP with formatting:

    #!/usr/local/bin/perl -w use Email::Sender::Simple qw(sendmail); use Email::Simple; use Email::Simple::Creator; open (IN, 'top.log'); $input=<IN>; print $input; $lngth=length($input); print $lngth; $load = substr($input,16,4); print $load; $value=.1 if ($load > $value) { # print "$load greater than $value"; my $email = Email::Simple->create( header => [ To => '"Hardy Cherry" <hcherry@youngliving.com>', From => '"Hardy Cherry" <hcherry@youngliving.com>', Subject => "Load g", ], body => "$load greater than $value.\n", ); sendmail($email); }
    "my" variable $email masks earlier declaration in same scope at hcshor +t.pl line 26. syntax error at hcshort.pl line 15, near ") {" syntax error at hcshort.pl line 28, near "}" Execution of hcshort.pl aborted due to compilation errors.

    I intend to use sendmail more than once but I'm a newbie in perl so I don't understand why I'm getting a syntax error and why the ()and {} seem to be ignored.

    The problems stem from the missing semicolon after "$value=.1". Perl is seeing the following (which is perfectly legitimate):

    $value=.1 if ($load > $value)

    That can't be followed by an opening brace, so Perl gives a syntax error 'near ") {"'. Been there, done that.

Re: understanding scope and () and {}
by toolic (Bishop) on Jan 27, 2011 at 17:40 UTC
    hcherry,

    Welcome to the Monastery. Your post is very difficult to read. Please read Writeup Formatting Tips, then update your post using code tags around your code and error messages. Doing so will make it much easier for others to help you. And remember to hit the "Preview" button before you post.