On Tue, Feb 8, 2011 at 12:48 PM, Shlomi Fish <[email protected]> wrote:
> Hi Gaal,
>
> On Tuesday 08 Feb 2011 12:19:01 Gaal Yahas wrote:
>> I suppose you can use a functional idiom to encapsulate this.
>>
>> # not tested
>> sub with_path($&) {
>>   my($new_path, $code) = @_;
>>   local $ENV{PATH} = $new_path;
>>   $code->();
>> }
>>
>> {
>>   # ...
>>   with_path((join $path_sep, $ENV{PATH}, $some_dir), {
>>     # Code to run with modified path
>>   });
>> }
>>
>
> My problem with my two snippets was not in the call to local, but with the
> fact that $ENV{PATH} is mentioned twice there, which is also the case in your
> snippet (which is also kidna wordy and cluttered, at least if done only
> once.).

I don't know that this is avoidable. local localizes its argument, so
local ($ENV{PATH} . ...) won't work. Since the new value depends on
the old one there are two distinct places where you have to refer to
the variable.

As for clutter, that's in the eye of the beholder. If the callsite
syntax becomes clearer in your opinion, putting the ugliness in a
well-named function can be worth it.

> The general paradigm you are describing can still be considered a valid
> approach, and I've seen it used in several CPAN APIs such as DBIx-Class and
> KiokuDB . But see what I wrote about it here:
>
> http://community.livejournal.com/shlomif_tech/49156.html

Not sure how general your case is as a counterexample. What you can
close on and the side effects of the closure greatly determine the
usefulness of this style of programming.

You need rw params, and I think using $_[0] would have gotten you them.

The extra length is partly a result of your style preferences (you
didn't HAVE to have a blank line between params and body). Apart from
the first issue -- what localization operates on -- these are
syntactic matters. FWIW Perl 6 makes them nicer.

> Regards,
>
>        Shlomi Fish
>
> --
> -----------------------------------------------------------------
> Shlomi Fish       http://www.shlomifish.org/
> "The Human Hacking Field Guide" - http://shlom.in/hhfg
>
> Chuck Norris can make the statement "This statement is false" a true one.
>
> Please reply to list if it's a mailing list post - http://shlom.in/reply .
> _______________________________________________
> Perl mailing list
> [email protected]
> http://mail.perl.org.il/mailman/listinfo/perl
>



-- 
Gaal Yahas <[email protected]>
http://gaal.livejournal.com/
_______________________________________________
Perl mailing list
[email protected]
http://mail.perl.org.il/mailman/listinfo/perl

Reply via email to