Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jozef (8299)

  (email not shown publicly)

Journal of jozef (8299)

Saturday May 16, 2009
08:28 AM

my $code = "work" unless Run::Env->mod_perl and time % 2;

[ #38984 ]

So time ago 迪拉斯 told me I should not use my together with a condition. He told me it's undefined behaviour, if the contrition is not met and asked me what am I expecting to have in the variable if the condition is not met. I was expecting undef and it actually worked like that. I was not too much excited and just told my self "let's just don't do it from now on".

Days passed and one day we got a strange behaviour in our mod_perl application. A variable was set even it was not supposed to be. The reason was simple. The condition was not met, but instead of undef the variable had the value from a previous request... Was funny in our case but in others it could lead to security problem.

Lesson learned - next time pay more attention to what my colleagues are telling and never use my+if :-)

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • Perl::Critic warns against this, try checking your code with it.

  • I find this sort of code to be less problematic:

    my $foo = test() ? 'bar' : undef;

  • If I get it right the faulty statement was something like: my $var = 'passed' if cond();, right? This looks really strange and funny to me. I understand it as execute this statement (which declares a variable) only if the condition is true. If the condition is false then the statement shouldn't be executed, thus no creation of variable. That's not what Perl might do as I'm expecting Jozef to be using "strict" and his module might have not compiled otherwise avoiding this peculiar situation. You where usin

    • It generally keeps the “pad” (the allocation space for lexicals, basically) around. Sometimes, eg. for recursive calls, it will allocate extra pads that may be garbage-collected later. So this faux-feature actually doesn’t work reliably except in the simplest use cases. It’s a good thing that 5.10 deprecates it and 5.12 will probably throw an error for such usages.

  • my $stuff = test() || '' if $some_thing;