On Wed, May 30, 2012 at 16:22:29 -0500 , Matt wrote:
> I did this. Snippet from larger code:
> 
>     eval {
>         $rststr = "";
>         local $SIG{ALRM} = sub { die "get timeout" };
>         alarm(30);
>         $rststr = get("http://"; . $dst . "/request.htm" ); # try to get my 
> data
>         alarm(0);
>     } ;
> 
> It 'appears' to work.  Does it look right?

I would write it a little differently:

my $rststr;
eval {
  # rest of the code here, except $rststr = "";
};

The difference here is that I'm declaring $rststr as a lexical rather
than a global. See http://perl.plover.com/FAQs/Namespaces.html for an
explanation about the different ways to declare variables in Perl.
Declaring $rststr with my keeps the program strict-compliant. strict is
perl's typo and mistake finder; learn it, love it, live it.

> One error I did receive.  If I did this 'eval { ... }' instead of this
> 'eval { ... } ;' I would get error about missing semicolon.  Does eval
> expect more arguments or something that it requires a semicolon?  Or
> is it because eval returns a result?

eval is a function, not a block construct like for/while/if. This means
that you need the semicolon after. See also
https://metacpan.org/module/Try::Tiny#BACKGROUND for things you need to
know when using eval. You don't *have* to use Try::Tiny*, but knowing
what its docs tell you is important.

* though I would strongly recommend at least considering using it

-- 
Chris Nehren           | Coder, Sysadmin, Masochist
Shadowcat Systems Ltd. | http://shadowcat.co.uk/

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to