Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

concatenating partially uninitialized values

by tos (Deacon)
on Jun 18, 2003 at 14:18 UTC ( [id://266829]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

to avoid the message

Use of uninitialized value in concatenation (.) or string at d:/Perl/site/lib/lz.pm line 190.
i changed the code-line 190 as follows
#my $outPath = $obj->{outpath}{absolut} . "-" . $obj->{sap}{jfExtNum}; my $outPath = $obj->{outpath}{absolut} . do { defined $obj->{sap}{jfExtNum} && "-" . $obj->{sap}{jfExtNum} };
the code is clear and works properly but i don't like it because it strikes me as rather bulky. Any suggestions for a more elegant way ? Can i perhaps avoid the do-Block ?.

thanks in advance

Replies are listed 'Best First'.
Re: concatenating partially uninitialized values
by TomDLux (Vicar) on Jun 18, 2003 at 14:23 UTC
    my $outPath = $obj->{outpath}{absolut} . ( defined $obj->{sap}{jfExtN +um} ) ? "-" . $obj->{sap}{jfExtNum} : "";

    I've never seen the do{} block used this way; the ternary operator is the "natural" way of handling such a situation. Don't know that I'd call it less bulky, though.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

Re: concatenating partially uninitialized values
by sgifford (Prior) on Jun 18, 2003 at 18:18 UTC
    I use small subs for this:
    sub undef2blank { return defined($_[0]) ? $_[0] : ""; } my $outPath = $obj->{outpath}{absolut} . "-" . undef2blank($obj->{sap} +{jfExtNum});
    It's probaby slower than the other ideas suggested here, but it's obvious what it does and keeps the general style of the original code.
      ++sgifford! Probably going to catch flames from the rabid OO'ers, but I like it.
      --
      Spring: Forces, Coiled Again!
Re: concatenating partially uninitialized values
by hardburn (Abbot) on Jun 18, 2003 at 14:27 UTC
    my $outPath = $obj->{outpath}{absolut} . (defined $obj->{sap}{jfExtNum} ? '-' . $obj->{sap}{jfExtNum} : '');

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: concatenating partially uninitialized values
by tommyw (Hermit) on Jun 18, 2003 at 14:45 UTC

    If all you want to do is deal with the warning:

    my $outPath = $obj->{outpath}{absolut} . "-" . ( $obj->{sap}{jfExtNum} + || '' );
    will use a default value of '' when the jfExtNum fails to have a true value (which is when it's undefined, or empty, or zero). That last case may or may not be an issue.

    But observe (again!) that your two code samples are not equivalent, as the second one omits the hyphen as well as the trailing number.

    --
    Tommy
    Too stupid to live.
    Too stubborn to die.

Re: concatenating partially uninitialized values
by Arguile (Hermit) on Jun 18, 2003 at 18:05 UTC

    If you know that you specifically want to use uninitialised variables, turn that warning off. It’s meant to help, not hinder you.

    no warnings qw(uninitialized);

    In this case, your two code snippets produce different results (the hyphen-minus is omitted in the second) so I wouldn’t suggest this technique. Be aware it’s there if you need it though.

    Example

    Before 5.6 this was only possible using $^W which turned off all warnings. Be very careful if you do that.

    use warnings; # This will generate a warning. print undef; { no warnings qw(uninitialized); # This won't. print undef; }

    See Also

Re: concatenating partially uninitialized values
by zby (Vicar) on Jun 18, 2003 at 14:30 UTC
    It is not equivalent! With the second code version you don't concatenate the '-' when the obj->{sap}{jfExtNum} is not defined.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-04-23 10:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found