Shortcut for if(defined($var) && $var ne "") ?

Discussion in 'Perl Misc' started by vikimun@gmail.com, Apr 6, 2008.

  1. Guest

    Is there shorter equivalent of if(defined($var) && $var ne "")
    that doesn't fall for the "0" case, and doesn't produce warning with -
    w ?

    Thanks
    V.M.
     
    , Apr 6, 2008
    #1
    1. Advertisements

  2. wrote:
    > Is there shorter equivalent of if(defined($var) && $var ne "")
    > that doesn't fall for the "0" case, and doesn't produce warning with -
    > w ?
    >
    > Thanks
    > V.M.

    Hmm,

    if (length $var) ....

    --jk
     
    Johann Kappacher, Apr 6, 2008
    #2
    1. Advertisements

  3. Sorry,

    it will produce warnings if it is not initialized.
    If you want to avoid warnings you must use defined().

    perl 5.10 introduces the defined-or Operator.
    => This is your solution.

    --jk
     
    Johann Kappacher, Apr 6, 2008
    #3
  4. Johann Kappacher wrote:
    > Sorry,
    >
    > it will produce warnings if it is not initialized.
    > If you want to avoid warnings you must use defined().
    >
    > perl 5.10 introduces the defined-or Operator.
    > => This is your solution.
    >
    > --jk


    .... but no, you need a defined-and operator!
    I give up, this exercise is futile!
     
    Johann Kappacher, Apr 6, 2008
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alvin Bruney

    Threads.. Session var lost, App var ok

    Alvin Bruney, Dec 2, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    535
    rooster575
    Dec 2, 2003
  2. thomson
    Replies:
    10
    Views:
    2,772
    Eliyahu Goldin
    Jun 20, 2005
  3. thomson
    Replies:
    0
    Views:
    535
    thomson
    Jun 20, 2005
  4. Fred
    Replies:
    3
    Views:
    479
    Alf P. Steinbach
    Aug 10, 2003
  5. Alex Vinokur
    Replies:
    4
    Views:
    664
    Ron Natalie
    Sep 24, 2003
  6. Tor Erik Soenvisen
    Replies:
    14
    Views:
    802
    Tim Roberts
    Nov 23, 2006
  7. Oodini
    Replies:
    1
    Views:
    2,054
    Keith Thompson
    Sep 27, 2005
  8. Alex Hall
    Replies:
    5
    Views:
    339
    Lawrence D'Oliveiro
    May 11, 2010
Loading...