Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

How does my work with a trailing conditional

by DeaconBlues (Monk)
on Apr 21, 2005 at 16:11 UTC ( #450078=perlquestion: print w/replies, xml ) Need Help??

DeaconBlues has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I have run into a problem with 'my' and some bad code. I would like to know how Perl handles this situation.

The code looks something like this:

sub foo { my $string .= 'appended stuff' if ($conditional1); $string .= 'more appended stuff' if ($conditional2); }

The code does not fail syntax checking. Under mod perl (Apache::Registry) I am getting shared memory issues across daemons.

If $conditional1 is false:

  1. what is the scope of $string?
  2. is it declared?
  3. is it initialized?

This works fine, btw.

sub foo { my $string = ''; $string .= 'appended stuff' if ($conditional1); $string .= 'more appended stuff' if ($conditional2); }

Thanks for any help.

s/pre/code/g - dvergin 2005-04-24

Replies are listed 'Best First'.
Re: How does my work with a trailing conditional
by ikegami (Patriarch) on Apr 21, 2005 at 16:19 UTC
    The behaviour of
    my ... if ...,
    my ... for ...,
    etc
    is documented as being undefined. There are therefore no answers to your questions.
Re: How does my work with a trailing conditional
by dragonchild (Archbishop) on Apr 21, 2005 at 16:31 UTC
    ikegami is correct in that the syntax under question is documented as having undefined behavior.

    That said, the way it generally behaves is:

    • my has both compile-time and run-time behaviors. The compile-time behavior allocates the space for $string. The run-time behavior will assign the value on the RHS of the operator to $string.
    • STATMENT if COND will perform the run-time portions of STATEMENT if COND evaluates to true ... at RUNTIME.

    Generally, this trick is used to declare and initialize static variables within functions. Your second try is much better and cleaner, anyways.

      The run-time behavior will assign the value on the RHS of the operator to $string.
      The important run-time behaviour of my is to make a note to release the scalar on scope exit. Thus  my $foo if $false means that the var isn't released at the end of the block, making the value still available at the next entry to the block.

      Dave.

Re: How does my work with a trailing conditional
by jdhedden (Deacon) on Apr 21, 2005 at 19:14 UTC
    Personally, I eschew the use of trailing conditionals, and I can see that using them with 'my' is pathological. Best to use the ternary conditional operator '?:' with 'my'.
    sub foo { my $string = ($conditional1) ? 'appended stuff' : ''; $string .= 'more appended stuff' if ($conditional2); }
    If you wanted your variable to remain undefined when the conditional is false, you can use:
    my $var = ($conditional) ? 'assigned value' : undef;
Re: How does my work with a trailing conditional
by davidrw (Prior) on Apr 21, 2005 at 16:18 UTC
    I don't see how it would affect your shared memory problems at all, but note that:
    my $string .= 'appended stuff' if ($conditional1);
    Doesn't make much sense -- should just use the '=' operator instead of the '.=' operator, since there's nothing to append to since you're only just now declaring the variable. (Usage is fine in the last example) Maybe that's the problem since it's essentially doing something like this?
    my $string = $string . 'appended stuff' if ($conditional1); #which is basically (or is it not and that's the problem?): my $string = undef . 'appended stuff' if ($conditional1);

    To answer your 2) & 3), this code will leave $string as undef:
    my $conditional1 = 0; my $string = 'blah' if $conditional1; # $string is undef
      Well usually you don't make code of that quality, of course. But when you have one or more of the following:
    • you have a tight deadline,
    • you are editing someone else's code,
    • you are adding functionality to older code,
    • you are coding tired,
    • you are coding without a prior design,
    • chances are that you make mistakes. Usually, you rely on errors and warnings. Yet, if those are not available, it can be hard to solve an error presented.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://450078]
Approved by ghenry
Front-paged by Thilosophy
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2022-01-28 06:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (73 votes). Check out past polls.

    Notices?