Live data from Hacker News

Perl: When DWIM Doesn't

bits.shutterstock.com

21–28 of 28 posts

Re: Perl: When DWIM Doesn't

#21
post #17

I basically gave up on perl after discovering this: foo foreach @bar; sub foo { s/foo/bar/ print; } This clobbers the content of @bar due to mutating the $_ variable. Which is a useful feature. But imagine that foo, instead of directly mutating the value passed to it, calls into a complex set of other code. Now you have a bomb where working code can break in crazy ways if any of it gets changed to modify $_. Having t…

This is not much different than mutating a global variable inside a function.

I would write your code as:

    foo($_) foreach @bar;

    sub foo {
        my $var = shift;
        $var =~ s/foo/bar/;
        print $var;
    }
You can also fix this is to localize $_ inside the function, if you don't want to pass $_ as a parameter.

    foo foreach @bar;

    sub foo {
        my $var = $_;
        $var =~ s/foo/bar/;
        print $var;
    }
You can even use $_ instead of $var (although it looks a little weird assigning $_ to itself):

    foo foreach @bar;

    sub foo {
        my $_ = $_;
        s/foo/bar/;
        print;
    }

Re: Perl: When DWIM Doesn't

#22
post #14

I love Perl. I wrote it professionally for 10 years. I have great friends who still do. The community is full of smart, fantastic people. Five years ago, however, I learned Python for a job and never looked back. In terms of programming languages they're practically the same thing. But Python is easier to read, easier to write, and easier to hire for. And stuff like this (dereferencing, "array context," worrying abou…

Assuming you're not a troll, you have bad timing who left just before Moose and dependable grammar extensions. I've been doing some Python lately and regularly scream when I lack usable anonymous funs (>1 instruction) or can't find a lib I know exist on CPAN, etc. ymmv.

Not a troll, real human, but I guess I was unconsciously trolling. Oh well.

Re: Perl: When DWIM Doesn't

#23
post #20

Both OPs examples would be spotted by unit tests and finding actual bug after that is quite trivial. So I agree, you can trigger some features unpredictably, but this just shows power behind tool and remind you to be well self-organized =)

Frankly, the first one is a clear explanation of why you should have a config object not a hash. Don't use a freeform data structure for data that isn't actually freeform if you want the perl5 VM to be able to catch errors for you. Shutterstock are legendary round the perl community for being a hive of badly written ancient code that never gets significant refactoring effort though; combine this with a significant te…

> Shutterstock are legendary round the perl community for being a hive of badly written ancient code that never gets significant refactoring effort though

I like to think that we're legendary for choosing a sane upgrade path to a services based approach built on Modern Perl. For our help debugging Dancer's (https://github.com/sukria/Dancer/issues/642) and Feersum's (https://github.com/stash/Feersum/issues/12) header parsing troubles. For our continuous sponsorship of YAPC (http://yapcna.org/sponsorship/our-sponsors).

Heh, we're legends in my own mind! [grin]

Re: Perl: When DWIM Doesn't

#24
post #17

I basically gave up on perl after discovering this: foo foreach @bar; sub foo { s/foo/bar/ print; } This clobbers the content of @bar due to mutating the $_ variable. Which is a useful feature. But imagine that foo, instead of directly mutating the value passed to it, calls into a complex set of other code. Now you have a bomb where working code can break in crazy ways if any of it gets changed to modify $_. Having t…

You're funny. If this really pushed you over the edge, you were born -on- the edge, and didn't give Perl a fair shake.

Re: Perl: When DWIM Doesn't

#25
post #24
post #17

I basically gave up on perl after discovering this: foo foreach @bar; sub foo { s/foo/bar/ print; } This clobbers the content of @bar due to mutating the $_ variable. Which is a useful feature. But imagine that foo, instead of directly mutating the value passed to it, calls into a complex set of other code. Now you have a bomb where working code can break in crazy ways if any of it gets changed to modify $_. Having t…

You're funny. If this really pushed you over the edge, you were born -on- the edge, and didn't give Perl a fair shake.

Joey's example is bad (or, at best, fragile) Perl, but he's also written a fair amount of useful and popular code in Perl too.

Re: Perl: When DWIM Doesn't

#26
post #17

I basically gave up on perl after discovering this: foo foreach @bar; sub foo { s/foo/bar/ print; } This clobbers the content of @bar due to mutating the $_ variable. Which is a useful feature. But imagine that foo, instead of directly mutating the value passed to it, calls into a complex set of other code. Now you have a bomb where working code can break in crazy ways if any of it gets changed to modify $_. Having t…

I think it's a pretty weak reason to give up on a language because you were using a global variable and expecting it to automatically be treated as a local variable within your function.

As others have mentioned, you are using a global variable ($_) inside a function. This is generally considered a bad practice.

The standard perl idiom of getting your function arguments via shift would make this a non-issue.

Re: Perl: When DWIM Doesn't

#27
post #4

re: open() creating temporary file if file arg is undef Fortunately both IO::File (core module) & Path::Class don't do this: use IO::File; my $fh = IO::File->new( $config->{file_path}, 'r' ) or die "can't open $config->{file_path}: $!"; use Path::Class qw ; my $fh = file( $config->{file_path} )->openr; Both above spot the undef gotcha. NB. And ->openr() throws an exception. ref: https://metacpan.org/module/IO::File |…

!!! Supplementary for future readers of this comment thread !!!

The pragma autodie also spots this problem!

So....

    use strict;
    use warnings;
    use autodie;

    our $config;
    $config->{file_paht} = "somefile";

    open my $fh, '{file_path};
Will throw an exception at open(). NB. Bizarrely the error is Use of uninitialized value $file in sprintf... but hey the problem is still caught :)

Re: Perl: When DWIM Doesn't

#28
post #24

Earlier quoted context omitted.

You're funny. If this really pushed you over the edge, you were born -on- the edge, and didn't give Perl a fair shake.

Joey's example is bad (or, at best, fragile) Perl, but he's also written a fair amount of useful and popular code in Perl too.

Yes thanks.. to be clear, I have programmed in perl since 1995.
Post reply on HN