From fayland at gmail.com Mon Dec 24 19:16:34 2007 From: fayland at gmail.com (Fayland Lam) Date: Tue, 25 Dec 2007 11:16:34 +0800 Subject: [PBP-pm] what's the correct way to do eval Message-ID: <47707612.9070707@gmail.com> have to replace this: my $has_proc_pid_file = eval "use Proc::PID::File; 1;"; it complaints Expression form of "eval" at line 7, column 25. See page 161 of PBP. Thanks. -- Fayland Lam // http://www.fayland.org/ Foorum based on Catalyst // http://www.foorumbbs.com/ From merlyn at stonehenge.com Mon Dec 24 19:59:22 2007 From: merlyn at stonehenge.com (Randal L. Schwartz) Date: Mon, 24 Dec 2007 19:59:22 -0800 Subject: [PBP-pm] what's the correct way to do eval In-Reply-To: <47707612.9070707@gmail.com> (Fayland Lam's message of "Tue, 25 Dec 2007 11:16:34 +0800") References: <47707612.9070707@gmail.com> Message-ID: <86ve6ntof9.fsf@blue.stonehenge.com> >>>>> "Fayland" == Fayland Lam writes: Fayland> have to replace this: Fayland> my $has_proc_pid_file = eval "use Proc::PID::File; 1;"; Fayland> it complaints Expression form of "eval" at line 7, column 25. See page Fayland> 161 of PBP. There *is* no replacement for that, which is why PBP is a guide, not a gospel. What you did is perfectly fine for that task. -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training! From jeffrey_thalhammer at yahoo.com Tue Dec 25 01:12:29 2007 From: jeffrey_thalhammer at yahoo.com (Jeffrey Thalhammer) Date: Tue, 25 Dec 2007 01:12:29 -0800 (PST) Subject: [PBP-pm] what's the correct way to do eval Message-ID: <256890.91471.qm@web58702.mail.re1.yahoo.com> Yes, technically, merlyn is absolutely correct. My question is whether you actually need to "use" Proc::PID::File, or if "require" might be better suited. For example: my $has_proc_pid_file = eval {require Proc::PID::File}; That would be compliant with PBP and it gives you the opportunity to delay loading of Proc::PID::File until runtime, which may or may not be desirable in your case. If not, then you'll have to stick with the "use". Since you're consciously choosing to deviate from PBP (with good reason), you can silence perlcritic by appending a "##no critic" pseudo-pragma to the statement like this: my $has_proc_pid_file = eval "use Proc::PID::File"; ## no critic (ProhibitStringyEval) Hope that helps. -Jeff ----- Original Message ---- From: Randal L. Schwartz To: Fayland Lam Cc: pbp-pm at pm.org Sent: Monday, December 24, 2007 7:59:22 PM Subject: Re: [PBP-pm] what's the correct way to do eval >>>>> "Fayland" == Fayland Lam writes: Fayland> have to replace this: Fayland> my $has_proc_pid_file = eval "use Proc::PID::File; 1;"; Fayland> it complaints Expression form of "eval" at line 7, column 25. See page Fayland> 161 of PBP. There *is* no replacement for that, which is why PBP is a guide, not a gospel. What you did is perfectly fine for that task. From chris at chrisdolan.net Tue Dec 25 21:00:23 2007 From: chris at chrisdolan.net (Chris Dolan) Date: Tue, 25 Dec 2007 23:00:23 -0600 Subject: [PBP-pm] what's the correct way to do eval In-Reply-To: <86ve6ntof9.fsf@blue.stonehenge.com> References: <47707612.9070707@gmail.com> <86ve6ntof9.fsf@blue.stonehenge.com> Message-ID: <792B6E14-8EDB-4109-A337-698D06AA0205@chrisdolan.net> On Dec 24, 2007, at 9:59 PM, Randal L. Schwartz wrote: >>>>>> "Fayland" == Fayland Lam writes: > > Fayland> have to replace this: > Fayland> my $has_proc_pid_file = eval "use Proc::PID::File; 1;"; > > Fayland> it complaints Expression form of "eval" at line 7, column > 25. See page > Fayland> 161 of PBP. > > There *is* no replacement for that, which is why PBP is a guide, > not a gospel. > > What you did is perfectly fine for that task. > Huh? Isn't the following equivalent? my $has_proc_pid_file = eval "require Proc::PID::File; Proc::PID::File->import; 1;"; I'm not saying it's definitely better, but I thought it was a valid replacement. In fact, I was sure of it until I noticed it was Randal saying otherwise. :-) Chris From merlyn at stonehenge.com Tue Dec 25 21:03:29 2007 From: merlyn at stonehenge.com (Randal L. Schwartz) Date: Tue, 25 Dec 2007 21:03:29 -0800 Subject: [PBP-pm] what's the correct way to do eval In-Reply-To: <792B6E14-8EDB-4109-A337-698D06AA0205@chrisdolan.net> (Chris Dolan's message of "Tue, 25 Dec 2007 23:00:23 -0600") References: <47707612.9070707@gmail.com> <86ve6ntof9.fsf@blue.stonehenge.com> <792B6E14-8EDB-4109-A337-698D06AA0205@chrisdolan.net> Message-ID: <86tzm6rqse.fsf@blue.stonehenge.com> >>>>> "Chris" == Chris Dolan writes: Chris> Huh? Isn't the following equivalent? Chris> my $has_proc_pid_file = eval "require Proc::PID::File; Chris> Proc::PID::File->import; 1;"; It's the eval $string that P::C is complaining about. And there's not a good way around that. -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training! From merlyn at stonehenge.com Wed Dec 26 18:06:16 2007 From: merlyn at stonehenge.com (Randal L. Schwartz) Date: Wed, 26 Dec 2007 18:06:16 -0800 Subject: [PBP-pm] what's the correct way to do eval In-Reply-To: <86zlvwrizw.fsf@blue.stonehenge.com> (Randal L. Schwartz's message of "Wed, 26 Dec 2007 18:04:03 -0800") References: <47707612.9070707@gmail.com> <86ve6ntof9.fsf@blue.stonehenge.com> <792B6E14-8EDB-4109-A337-698D06AA0205@chrisdolan.net> <86tzm6rqse.fsf@blue.stonehenge.com> <86zlvwrizw.fsf@blue.stonehenge.com> Message-ID: <86ve6kriw7.fsf@blue.stonehenge.com> >>>>> "Randal" == Randal L Schwartz writes: Randal> Perhaps if you put most of this into a BEGIN block, I'd be less Randal> horrified. Something like: my $HAS_FEATURE_FOO; # default undef BEGIN { $HAS_FEATURE_FOO = eval { require Feature::Foo } and Feature::Foo->import; } And yes, that "and" is "and" and not "&&" for a reason. :) -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training! From merlyn at stonehenge.com Wed Dec 26 18:04:03 2007 From: merlyn at stonehenge.com (Randal L. Schwartz) Date: Wed, 26 Dec 2007 18:04:03 -0800 Subject: [PBP-pm] what's the correct way to do eval In-Reply-To: (Chris Dolan's message of "Wed, 26 Dec 2007 20:01:37 -0600") References: <47707612.9070707@gmail.com> <86ve6ntof9.fsf@blue.stonehenge.com> <792B6E14-8EDB-4109-A337-698D06AA0205@chrisdolan.net> <86tzm6rqse.fsf@blue.stonehenge.com> Message-ID: <86zlvwrizw.fsf@blue.stonehenge.com> >>>>> "Chris" == Chris Dolan writes: Chris> How about this: Chris> my $has_proc_pid_file; Chris> eval { require Proc::PID::File; Proc::PID::File->import; $has_proc_pid_file Chris> = 1; }; Chris> Or perhaps this is equivalent (untested): Chris> my $has_proc_pid_file = eval { require Proc::PID::File; Chris> Proc::PID::File->import; 1; }; I don't like calling ->import if it's not early enough to provide subroutine definitions for either avoiding barewords or getting prototypes. It's ... uh ... misleading. Perhaps if you put most of this into a BEGIN block, I'd be less horrified. -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training! From chris at chrisdolan.net Wed Dec 26 18:01:37 2007 From: chris at chrisdolan.net (Chris Dolan) Date: Wed, 26 Dec 2007 20:01:37 -0600 Subject: [PBP-pm] what's the correct way to do eval In-Reply-To: <86tzm6rqse.fsf@blue.stonehenge.com> References: <47707612.9070707@gmail.com> <86ve6ntof9.fsf@blue.stonehenge.com> <792B6E14-8EDB-4109-A337-698D06AA0205@chrisdolan.net> <86tzm6rqse.fsf@blue.stonehenge.com> Message-ID: On Dec 25, 2007, at 11:03 PM, Randal L. Schwartz wrote: >>>>>> "Chris" == Chris Dolan writes: > > Chris> Huh? Isn't the following equivalent? > > Chris> my $has_proc_pid_file = eval "require Proc::PID::File; > Chris> Proc::PID::File->import; 1;"; > > It's the eval $string that P::C is complaining about. And there's > not a good way around that. > How about this: my $has_proc_pid_file; eval { require Proc::PID::File; Proc::PID::File->import; $has_proc_pid_file = 1; }; Or perhaps this is equivalent (untested): my $has_proc_pid_file = eval { require Proc::PID::File; Proc::PID::File->import; 1; }; Chris