Re: How to check if a DateTime is invalid (again - but this time without using eval)?
On Tue, Jul 11, 2017 at 10:56 AM, Paul Hoffman wrote:
> On Mon, Jul 10, 2017 at 09:44:15PM +0100, Zefram wrote:
> > Thomas (HFM) Wyant wrote:
> > >One of the edge cases with eval {} is the possibility that $@ gets
> > >clobbered before you get your hands on it.
> >
> > The possibility of it being clobbered by a destructor was fixed in 5.14.
> > (Destructors that do this are considered buggy, for pre-5.14 perls,
> > and are individually easy to fix, so the problem is still manageable on
> > those perl versions.)
>
> And in perl < 5.14 you can work around it easily enough:
>
> my $ok;
> eval {
> ...
> $ok = 1;
> };
> if ($ok) {
> ...
> }
>
I'd write that as ...
my $ok = eval { ...; 1 };
if ($ok) { ... }
I think that reads a little better.
Of course, I'd also just use Try::Tiny and use a catch instead of checking
$ok in the first place.
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
Simpler:
if (!eval {
# try
...
return 1; # ok
}) {
# catch
...
}
On Jul 11, 2017 12:06 PM, "Paul Hoffman" wrote:
On Mon, Jul 10, 2017 at 09:44:15PM +0100, Zefram wrote:
> Thomas (HFM) Wyant wrote:
> >One of the edge cases with eval {} is the possibility that $@ gets
> >clobbered before you get your hands on it.
>
> The possibility of it being clobbered by a destructor was fixed in 5.14.
> (Destructors that do this are considered buggy, for pre-5.14 perls,
> and are individually easy to fix, so the problem is still manageable on
> those perl versions.)
And in perl < 5.14 you can work around it easily enough:
my $ok;
eval {
...
$ok = 1;
};
if ($ok) {
...
}
Paul.
--
Paul Hoffman
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
On Mon, Jul 10, 2017 at 09:44:15PM +0100, Zefram wrote:
> Thomas (HFM) Wyant wrote:
> >One of the edge cases with eval {} is the possibility that $@ gets
> >clobbered before you get your hands on it.
>
> The possibility of it being clobbered by a destructor was fixed in 5.14.
> (Destructors that do this are considered buggy, for pre-5.14 perls,
> and are individually easy to fix, so the problem is still manageable on
> those perl versions.)
And in perl < 5.14 you can work around it easily enough:
my $ok;
eval {
...
$ok = 1;
};
if ($ok) {
...
}
Paul.
--
Paul Hoffman
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
Thomas (HFM) Wyant wrote:
>One of the edge cases with eval {} is the possibility that $@ gets
>clobbered before you get your hands on it.
The possibility of it being clobbered by a destructor was fixed in 5.14.
(Destructors that do this are considered buggy, for pre-5.14 perls,
and are individually easy to fix, so the problem is still manageable on
those perl versions.)
-zefram
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
That is not true.
One of the biggest edge cases is what happens if you have signals from
other processes. A signal caught inside of an eval {} that generates an
exception will generate a $@ there. This has to be handled each and every
time. The odds of catching an individual signal inside of a fast eval {}
like this are low, but if you've got a lot of evals and subprocesses,
eventually you'll encounter weird random misbehaviors that are hard to
understand...and which if understood can require a lot of rewriting to fix.
On Wed, Jul 5, 2017 at 8:23 AM, Eric Brine wrote:
> On Wed, Jul 5, 2017 at 10:59 AM, Thomas (HFM) Wyant <
> [email protected]> wrote:
>
>> One of the edge cases with eval {} is ...
>>
>
> All the edge cases are covered by the previously linked:
> https://metacpan.org/pod/Try::Tiny#BACKGROUND
>
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
On 05.07.2017 18:27, Bill Moseley wrote:
> Haven't those issues with eval been addressed in more recent versions of
> Perl?
>
> For example, in older Perls this used to reset $@, but now $@ is retained:
>
> use strict;
> use warnings;
>
> sub Foo::DESTROY {
> print "in Foo Destroy\n";
> eval { 1 };
> print "Foo has \$@ as '$@'\n"
> return;
> }
>
> eval {
> my $foo = bless {}, 'Foo';
> die "BOOM\n";
> };
> print "eval with $@\n";
>
>
> Returns:
>
> in Foo Destroy
> Foo has $@ as ''
> eval with BOOM
>
Thomas Klausner in one of his posts provided the following link:
http://blog.twoshortplanks.com/2011/06/06/unexceptional-exceptions-in-perl-5-14/
>From reading it, I have got the impression that the behavior in 5.14 has
been drastically improved, but that not all problems are solved. I have
to re-read, though (being new to exception handling in Perl).
Thanks for your help!
Regards,
Binarus
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
On 05.07.2017 16:59, Thomas (HFM) Wyant wrote:
> One of the edge cases with eval {} is the possibility that $@ gets
> clobbered before you get your hands on it.
I don't care about this since I don't use $@ (see my previous posts).
> my $dt = eval { $parser->parse( $input ) }
> or die "Unable to parse your date ($input)\n";
This is the way I want to go. In my case, the eval even doesn't need to
explicitly return something. The only thing I am interested in is if
DateTime -> new() has died inside the eval or not. In case it has, the
eval will return undef; in case it hasn't, eval will return a valid
date-time.
Thank you very much for your help!
Regards,
Binarus
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
On 03.07.2017 13:09, Rick Measham wrote:
> I'm not sure what you mean by 'local DIE handlers'. Are you talking
> about defining a local $SIG{__DIE__} ?
>
Exactly. Most examples I saw are doing this, but since I don't catch any
signals anywhere else in my application, I think I don't need it. On the
other hand, you never know about other libraries / modules, so I
probably will do it nevertheless. After all, it won't hurt (to my best
knowledge).
> I've been out of perl for a couple of years, but surely you should just
> use eval{} ?
>
>
> $dt = eval { $parser->parse( $input );};
>
> if( $@ ){
>
>echo "Unable to parse your date ($input)\n";
>
>exit;
>
> }
>
> echo "Your date is $dt\n";
>
>
> - Rick
>
Yes, I'll follow everybody's advice and use eval, but I won't use $@.
The only thing I must know is if DateTime -> new() inside the eval has
died or not, so I'll just test if the eval return undef.
Thanks for your help!
Regards,
Binarus
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
On 03.07.2017 17:11, Thomas Klausner wrote: > block eval indeed had some not-totally-implausible corner cases, as > listed here: > https://metacpan.org/pod/Try::Tiny#BACKGROUND > > AFAIK at least some of them were fixed in Perl 5.14: > http://blog.twoshortplanks.com/2011/06/06/unexceptional-exceptions-in-perl-5-14/ Thank you very much for the links. That was interesting reading. I didn't read Perl's own documentation about $@, but will do in the next days. I don't care about the problems with $@ because I don't use it. Actually, all subs I have used until now (from core or from modules), either return reasonable values or provide other ways besides exception handling to detect errors, and I've always been using that to handle errors. None of the subs I have used so far are dying in case of errors (provided that the respective error could be handled reasonably at all) (unless I have missed something; at least, I never have experienced such behavior even in the earliest stages of development where many errors occur during testing). So, in the moment when I decided to follow everybody's advice and to use eval to solve my problem, it was clear to me that I just would check if the eval would return undef to detect if DateTime -> new() had died inside the eval. In that specific case, I don't need to know more. > And regarding using exceptions or not: I don't think you have a choice > here. You will always encounter exceptions (disk full, DB-Server > crashed, ...), so you might as well get used to them and incorporate > them in your toolkit. I know nothing about .NET, so maybe exceptions are > horrible there. But in Perl they work quite well, IMO... You are right. At least, as long as DateTime -> new() just dies in case of invalid parameters, I obviously have no choice. I admit that it is easier than I thought, though. But interestingly enough, this is the very first case where I am actually forced to use exception handling in Perl; see my remarks above. This may also be due to the fact that Perl never has been my main programming language, so I am quite sure that I am doing many things which an experienced Perl programmer will do another way or won't do at all, and that I am not using Perl's capabilities at the fullest. Thank you very much for your help again! Regards, Binarus
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
On Wed, Jul 5, 2017 at 1:56 PM, Eric Brine wrote:
> On Wed, Jul 5, 2017 at 1:53 PM, Dave Rolsky wrote:
>
>> On Wed, Jul 5, 2017 at 12:49 PM, Eric Brine wrote:
>>
>>> On Wed, Jul 5, 2017 at 12:43 PM, Dave Rolsky wrote:
>>>
On Wed, Jul 5, 2017 at 11:27 AM, Bill Moseley wrote:
> Haven't those issues with eval been addressed in more recent versions
> of Perl? Well, that's one issue, but there are others, for example $@
> being set to a false value.
>
>>> That's kinda hard to do by accident.
>>>
>>>
>>
>> Assuming you are in full control of all the relevant code, yes, I suppose
>> it is. That said, a little extra defensive programming usually seems like
>> the best choice to me. What is gained by discouraging people from using
>> CPAN modules that help avoid corner cases?
>>
>> No, even with that assumption. die(""), die(0) and die(undef) all put
> true values in $@.
>
*without
>perl -E"for ('', 0, undef) { eval { die($_) }; say $@ ? 'true' : 'false' }"
true
true
true
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
On Wed, Jul 5, 2017 at 1:53 PM, Dave Rolsky wrote:
> On Wed, Jul 5, 2017 at 12:49 PM, Eric Brine wrote:
>
>> On Wed, Jul 5, 2017 at 12:43 PM, Dave Rolsky wrote:
>>
>>> On Wed, Jul 5, 2017 at 11:27 AM, Bill Moseley wrote:
>>>
Haven't those issues with eval been addressed in more recent versions
of Perl? Well, that's one issue, but there are others, for example $@
being set to a false value.
>>>
>> That's kinda hard to do by accident.
>>
>>
>
> Assuming you are in full control of all the relevant code, yes, I suppose
> it is. That said, a little extra defensive programming usually seems like
> the best choice to me. What is gained by discouraging people from using
> CPAN modules that help avoid corner cases?
>
> No, even with that assumption. die(""), die(0) and die(undef) all put true
values in $@.
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
On Wed, Jul 5, 2017 at 12:49 PM, Eric Brine wrote: > On Wed, Jul 5, 2017 at 12:43 PM, Dave Rolsky wrote: > >> On Wed, Jul 5, 2017 at 11:27 AM, Bill Moseley wrote: >> >>> Haven't those issues with eval been addressed in more recent versions of >>> Perl? Well, that's one issue, but there are others, for example $@ being >>> set to a false value. >>> >> > That's kinda hard to do by accident. > > Assuming you are in full control of all the relevant code, yes, I suppose it is. That said, a little extra defensive programming usually seems like the best choice to me. What is gained by discouraging people from using CPAN modules that help avoid corner cases? Cheers, Dave Rolsky http://blog.urth.org https://github.com/autarch
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
On Wed, Jul 5, 2017 at 12:43 PM, Dave Rolsky wrote: > On Wed, Jul 5, 2017 at 11:27 AM, Bill Moseley wrote: > >> Haven't those issues with eval been addressed in more recent versions of >> Perl? Well, that's one issue, but there are others, for example $@ being >> set to a false value. >> > That's kinda hard to do by accident.
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
On Wed, Jul 5, 2017 at 11:27 AM, Bill Moseley wrote:
> Haven't those issues with eval been addressed in more recent versions of
> Perl?
>
> For example, in older Perls this used to reset $@, but now $@ is retained:
>
> use strict;
> use warnings;
>
> sub Foo::DESTROY {
> print "in Foo Destroy\n";
> eval { 1 };
> print "Foo has \$@ as '$@'\n"
> return;
> }
>
> eval {
> my $foo = bless {}, 'Foo';
> die "BOOM\n";
> };
> print "eval with $@\n";
>
>
> Returns:
>
> in Foo Destroy
> Foo has $@ as ''
> eval with BOOM
>
>
Well, that's one issue, but there are others, for example $@ being set to a
false value. I also appreciate the semantic clarity of try/catch.
Cheers,
Dave Rolsky
http://blog.urth.org
https://github.com/autarch
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
Haven't those issues with eval been addressed in more recent versions of
Perl?
For example, in older Perls this used to reset $@, but now $@ is retained:
use strict;
use warnings;
sub Foo::DESTROY {
print "in Foo Destroy\n";
eval { 1 };
print "Foo has \$@ as '$@'\n"
return;
}
eval {
my $foo = bless {}, 'Foo';
die "BOOM\n";
};
print "eval with $@\n";
Returns:
in Foo Destroy
Foo has $@ as ''
eval with BOOM
On Wed, Jul 5, 2017 at 8:52 AM, Dave Rolsky wrote:
> On Wed, Jul 5, 2017 at 10:23 AM, Eric Brine wrote:
>
>> On Wed, Jul 5, 2017 at 10:59 AM, Thomas (HFM) Wyant <
>> [email protected]> wrote:
>>
>>> One of the edge cases with eval {} is ...
>>>
>>
>> All the edge cases are covered by the previously linked:
>> https://metacpan.org/pod/Try::Tiny#BACKGROUND
>>
>
> Yes, this is exactly why I would recommend always using Try::Tiny over
> plain eval.
>
> Cheers,
>
> Dave Rolsky
> http://blog.urth.org
> https://github.com/autarch
>
>
--
Bill Moseley
[email protected]
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
On Wed, Jul 5, 2017 at 10:23 AM, Eric Brine wrote: > On Wed, Jul 5, 2017 at 10:59 AM, Thomas (HFM) Wyant < > [email protected]> wrote: > >> One of the edge cases with eval {} is ... >> > > All the edge cases are covered by the previously linked: > https://metacpan.org/pod/Try::Tiny#BACKGROUND > Yes, this is exactly why I would recommend always using Try::Tiny over plain eval. Cheers, Dave Rolsky http://blog.urth.org https://github.com/autarch
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
On Wed, Jul 5, 2017 at 10:59 AM, Thomas (HFM) Wyant
wrote:
> One of the edge cases with eval {} is ...
>
All the edge cases are covered by the previously linked:
https://metacpan.org/pod/Try::Tiny#BACKGROUND
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
> On July 3, 2017 at 7:09 AM Rick Measham wrote:
>
>
> I'm not sure what you mean by 'local DIE handlers'. Are you talking about
> defining a local $SIG{__DIE__} ?
>
> I've been out of perl for a couple of years, but surely you should just
> use eval{} ?
>
>
> $dt = eval { $parser->parse( $input ); };
>
>
> if( $@ ){
>
>echo "Unable to parse your date ($input)\n";
>
>exit;
>
> }
>
>
>
> echo "Your date is $dt\n";
>
>
> - Rick
>
One of the edge cases with eval {} is the possibility that $@ gets clobbered
before you get your hands on it. I have never actually had this happen to me
(or at least I have never recognized it) but because I have a tendency to
blunder into situations where odd things happen I have developed the habit of
having the eval {} always return a true value, and then testing the value of
the eval {}. An exception will cause it to be false, even in the unlikely
(IM(NS)HO) case of $@ getting clobbered. In this specific case,
my $dt = eval { $parser->parse( $input ) }
or die "Unable to parse your date ($input)\n";
Off-topic changes to the above include the use of the "... or die" pattern,
which causes the output to go to STDERR and causes the script to terminate with
the equivalent of "exit 1". I don't think echo() is standard Perl -- maybe a
braino for print()?
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
Eric Brine wrote:
>On Mon, Jul 3, 2017 at 11:11 AM, Thomas Klausner wrote:
>> I personally don't like Try::Tiny, because it changes how 'return' works
>> inside an eval (well, try)
>
>What difference is there?
I think domm is actually thinking of TryCatch there. Inside an eval,
or Try::Tiny's try, a return expression returns from the eval/try.
Inside TryCatch's try, a return expression ignores the try scope and
returns from whatever it would have returned from without it, usually
from the enclosing subroutine.
$ perl -lwe 'sub z { eval { return 123 }; return 456; } print z()'
456
$ perl -MTry::Tiny -lwe 'sub z { try { return 123; }; return 456; } print z()'
456
$ perl -MTryCatch -lwe 'sub z { try { return 123; } return 456; } print z()'
123
-zefram
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
On Mon, Jul 3, 2017 at 11:11 AM, Thomas Klausner wrote: > I personally don't like Try::Tiny, because it changes how 'return' works > inside an eval (well, try) What difference is there?
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
Hi!
On Mon, Jul 03, 2017 at 10:00:52AM +0200, Binarus wrote:
> My main problem was that from my readings I got the impression that even
> the block eval might be problematic (please see my answer to Thomas
> Klausner's first post if you are interested what made me believe so).
Just for a bit more backgroud:
block eval indeed had some not-totally-implausible corner cases, as
listed here:
https://metacpan.org/pod/Try::Tiny#BACKGROUND
AFAIK at least some of them were fixed in Perl 5.14:
http://blog.twoshortplanks.com/2011/06/06/unexceptional-exceptions-in-perl-5-14/
I personally don't like Try::Tiny, because it changes how 'return' works
inside an eval (well, try), and I'm too stupid to remember this change
(and it bit me a few times). OTOH I never (even on ancient Perls had any
problem with three issues mentioned in Try::Tiny - Background (but then
"die Dummen hams Glück" as a local saying goes)
And regarding using exceptions or not: I don't think you have a choice
here. You will always encounter exceptions (disk full, DB-Server
crashed, ...), so you might as well get used to them and incorporate
them in your toolkit. I know nothing about .NET, so maybe exceptions are
horrible there. But in Perl they work quite well, IMO...
Greetings,
domm
--
#!/usr/bin/perl http://domm.plix.at
for(ref bless{},just'another'perl'hacker){s-:+-$"-g&&print$_.$/}
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
On 3/07/2017 17:44, Binarus wrote: This answer has 49 upvotes, so can't be too wrong. Oh no, this is the internet. That's likely 49 other people don't know the answer, but want to look smart. - Rick -- Message protected for iSite by MailGuard: e-mail anti-virus, anti-spam and content filtering.http://www.mailguard.com.au
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
I'm not sure what you mean by 'local DIE handlers'. Are you talking
about defining a local $SIG{__DIE__} ?
I've been out of perl for a couple of years, but surely you should just
use eval{} ?
$dt = eval { $parser->parse( $input );};
if( $@ ){
echo "Unable to parse your date ($input)\n";
exit;
}
echo "Your date is $dt\n";
- Rick
On 3/07/2017 17:49, Binarus wrote:
On 02.07.2017 22:02, Eric Brine wrote:
`eval BLOCK` is very different `eval EXPR`.
The latter involves the need to generate Perl code, and ask the related
risks. That's not the case for the former, which is the right tool for
the job here.
Thank you very much for your answer. I have decided to solve my problem
by using a block eval, probably with a local DIE handler added in the
block (but still thinking about whether this is necessary - I won't use
other DIE handlers and don't need to "catch errors" at any other places).
Regards,
Binarus
--
Message protected for iSite by MailGuard: e-mail anti-virus, anti-spam and
content filtering.http://www.mailguard.com.au
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
On 03.07.2017 05:57, Bill Ricker wrote: > Unpacking it a little ... > > 1. Scalar Eval > [...] > > 2. Block Eval > [...] > > 3. Try-Catch > [...] > Thank you very much for your elaborated and understandable answer. My main problem was that from my readings I got the impression that even the block eval might be problematic (please see my answer to Thomas Klausner's first post if you are interested what made me believe so). In the meantime, I have decided to go the way everybody proposes here. I'll use a block eval to catch the error, probably with a local DIE handler in the block (still thinking about that - I don't need to "catch errors" at other places, and I don't use DIE handlers at other places, so it is probably not necessary). Regards, Binarus
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
On 02.07.2017 22:02, Eric Brine wrote: > `eval BLOCK` is very different `eval EXPR`. > > The latter involves the need to generate Perl code, and ask the related > risks. That's not the case for the former, which is the right tool for > the job here. > Thank you very much for your answer. I have decided to solve my problem by using a block eval, probably with a local DIE handler added in the block (but still thinking about whether this is necessary - I won't use other DIE handlers and don't need to "catch errors" at any other places). Regards, Binarus
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
On 02.07.2017 19:01, Thomas Klausner wrote: > Hi! > > On Sun, Jul 02, 2017 at 11:59:56AM +0200, Binarus wrote: > >> - Most people recommend to "catch the error" DateTime throws when >> encountering such date-times, but if I got it right, that always >> involves eval, even when using modules like Try:Tiny and the like, so I >> don't want to do that. > > As somebody who most likely started on of the threads you mentioned, I > have to say that using eval is (as Zefram already said) the right > answer. > > I do not know why you do not want to use eval, it's very stable > (esp. in recent Perls) and not at all slow and dangerous. > Thank you very much for your answer. There are multiple reasons why I didn't want to go that way. First, I am not a fan of the try-catch stuff (i.e. "error catching") at all. For example, I have experienced all sorts of glitches and worries with try-catch in the MS .NET world. stackoverflow.com is full of questions (and in most cases, fundamentally different answers to the same question) regarding try-catch-finally in .NET. This is just an example - IMHO, this whole error catching thing is not a clean concept from the beginning on (in all programming languages). Having said this, I know that there are not many people left who have the same opinion, and of course, I am using what I have to use to reach my goal, so this is not worth any further discussion. The second (and by far more important) reason are threads like this one: https://stackoverflow.com/questions/4006267/what-is-the-best-way-to-handle-exceptions-in-perl The accepted answer there proposes error handling by eval blocks (as people here did), but then states: "It's fragile, though, and will break in a number of subtle edge cases that most people don't think about." >From my experience, I know that I will be the first one to run into one of these edge cases, whatever they may be. This answer has 49 upvotes, so can't be too wrong. Now, what should I make out of that? He doesn't give examples what this subtle edge cases could be, but I see no reason to mistrust the answer. Given the fact that I have found similar mysterious statements (i.e. warnings against eval without further explanation or examples) at other places as well, how do I know if I am affected? On the other hand, I have read the eval documentation in perldoc multiple times now and could not find any hint about when it could fail or why it could be dangerous (at least, not in the block eval part). I meanwhile tend to believe perldoc more than any other source; they usually emphasize what could go wrong in big red letters, and I didn't find such sections in the block eval documentation part. Thus, I have decided to use the eval block error catching in that case. Nevertheless, for the reasons mentioned above, I am not exceptionally happy about having to do so, and I still would prefer if DateTime would provide a method to check in advance if a date-time is valid or not. Again, thanks for your time and expertise. Regards, Binarus
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
On 02.07.2017 12:24, Zefram wrote: > Binarus wrote: >> - Most people recommend to "catch the error" DateTime throws when >> encountering such date-times, but if I got it right, that always >> involves eval, even when using modules like Try:Tiny and the like, so I >> don't want to do that. > > Do this. eval is the right tool for the job. > Thank you very much for your answer. I have decided to follow your advice. I will use an eval block where I try to construct the date-time, and will probably add a local DIE handler in there (although I have no other DIE handlers installed anywhere, so I am still thinking about it). Regards, Binarus
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
On Sun, Jul 2, 2017 at 4:02 PM, Eric Brine wrote:
> `eval BLOCK` is very different `eval EXPR`.
>
> The latter involves the need to generate Perl code, and ask the related
> risks. That's not the case for the former, which is the right tool for the
> job here.
>
The above is quite right.
Unpacking it a little ...
1. Scalar Eval
The evil eval that all the old docs warn against is classic original
interpretative eval:
eval "some string with $evil $unclean $bits"; # EVIL eval string
which may look like
eval $scalar; # came from who knows
(which may stringify any non-string $scalar at need)
and in either case it calls the compiler at run time, each time.
That way lies reliability and security madness.
(It still has its uses, rarely, when building certain kinds of DWIM magic
... but mortals should use a well-tested module that encapsulates that
magic.)
2. Block Eval
More recently added to Perl but so far back any perl not supporting it is
very very obsolete is :
eval { statements; expressions; } # Good eval BLOCK
which compiles once with the program.
This doesn't totally relieve your from untainting user input - they can
still feed you "XLII" instead of 42 - but is much less dangerous wrto
"Little Bobby Tables" injections.
3. Try-Catch
The other thing besides eval {oops} used to catch errors in modern perl
code is to use a module that provides a try-catch statement extension,
something like -
try{ statements; that; might; die or throw; } catch { error handler;
}
This is more than just simple syntactic sugar around eval { block; }
because it makes the error handling much less ugly.
(Especially if throwing object-oriented errors in ISA hierarchy.)
There are many competing try-catch modules.
https://metacpan.org/search?q=try%2Dcatch
( My usual rule of thumb is try a ::Simple or ::Tiny variant first :-)
But i'm not sure which if any is particularly better or worse adapted to
catching DateTime erroneous input errors. Maybe someone else has been
there. )
--
Bill Ricker
[email protected]
https://www.linkedin.com/in/n1vux
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
`eval BLOCK` is very different `eval EXPR`. The latter involves the need to generate Perl code, and ask the related risks. That's not the case for the former, which is the right tool for the job here. On Jul 2, 2017 6:03 AM, "Binarus" wrote: Dear all, I am a long time user of Perl and have done several web application back-ends based on it. But I haven't touched eval yet, mainly because I have got the impression that it is a very complex beast with many side effects and that using it is slow, unsafe or unclean in a certain sense. Furthermore, I just didn't need it until now. To make a long story short, I'd like to keep avoiding it. Now, I am working on an application which heavily uses DateTime (mainly for converting between users' time zones and the UTC time zone where the math is done). Due to the character of the UI, it is inevitable that the application will have to process invalid (i.e. formally correct, but actually non-existent) date-times (like the 6th of April in 2003 at 02:30 in the America/Chicago time zone, to cite the example from the DateTime documentation). My problem is how I could detect such invalid (i.e. non-existent) date-times. Of course, I have tried to do my homework before asking here and have done a lot of research: - Most people recommend to "catch the error" DateTime throws when encountering such date-times, but if I got it right, that always involves eval, even when using modules like Try:Tiny and the like, so I don't want to do that. - Some people recommend using undocumented functions / data arrays of DateTime::Timezone to get the periods from the time zone database and use that data to make an own algorithm to check whether a certain date-time in a certain time-zone does exist or not. Dave explicitly recommends against that approach, though, and I will follow his advice. - Date::Manip::TZ -> all_periods() seems to provide a clean way to get the period information for a certain time zone and year. But even when I know the periods, I still have to make my own algorithm for checking the date-time in question (which means reinventing a great part of the wheel). Additionally, including another module into my application solely to solve that problem seems a bit oversized. So I still have the hope that Dave eventually is willing to solve that problem. I am very well understanding that it might not be wise to change the DateTime -> new() function accordingly (for compatibility and technical reasons). But (in my naive thinking) making a new function for testing if a date-time exists probably is easy; after all, the respective tests already are in DateTime (otherwise, it couldn't throw an error / die when encountering non-existent date-times, could it?). So, I'd like to ask if there is any chance for the following to happen :-) 1) Clone the DateTime -> new() function and name the new function DateTime -> date_time_exists(). At this point, both functions still are exactly identical, including the parameters. 2) Change the code of the DateTime -> date_time_exists() function in the following way: a) Return a boolean value instead of a date-time object. b) At all places where it dies / throws an error due to the date-time being invalid, just return false instead of dying / throwing an error. c) If it reaches its end without having returned false, return true. Of course, testing a date-time (using -> date_time_exists()) before actually getting it (using -> new()) will cause a performance penalty, and there will be a lot of unnecessary code in -> date_time_exists() (which could be cleaned up later, though), and if there is a bug in -> new(), it is probably in -> date_time_exists() as well. On the other hand, I don't care about the performance penalty, and we finally would have a clean and quick solution for a problem affecting many people. Indeed, I have found dozens of threads on various sites and lists regarding this subject, most of them with the first answer recommending using eval to catch DateTime's errors, the next answer / comment explaining how complex, slow and dangerous this is and under which circumstances it will fail, the next answer / comment recommending using Try::Tiny to avoid those problems, the next answer explaining why you shouldn't use Try::Tiny, the next answer recommending using TryCatch instead of Try::Tiny, and so on (exaggerated by intention - I think we've got the idea). So I ultimately would like to be able to check if a date-time is valid *before* calling DateTime -> new(), meaning that I don't have to "catch errors". What do you think about it? Thank you very much! Binarus P.S. If there is another module which provides functions for such tests, please let me know (but even in this case, I would highly appreciate if DateTime would be extended accordingly; it is extremely reliable and well documented - thanks Dave!)
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
Hi!
On Sun, Jul 02, 2017 at 11:59:56AM +0200, Binarus wrote:
> - Most people recommend to "catch the error" DateTime throws when
> encountering such date-times, but if I got it right, that always
> involves eval, even when using modules like Try:Tiny and the like, so I
> don't want to do that.
As somebody who most likely started on of the threads you mentioned, I
have to say that using eval is (as Zefram already said) the right
answer.
I do not know why you do not want to use eval, it's very stable
(esp. in recent Perls) and not at all slow and dangerous.
Greetings,
domm
--
#!/usr/bin/perl http://domm.plix.at
for(ref bless{},just'another'perl'hacker){s-:+-$"-g&&print$_.$/}
Re: How to check if a DateTime is invalid (again - but this time without using eval)?
Binarus wrote: >- Most people recommend to "catch the error" DateTime throws when >encountering such date-times, but if I got it right, that always >involves eval, even when using modules like Try:Tiny and the like, so I >don't want to do that. Do this. eval is the right tool for the job. -zefram
