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:
    539
    rooster575
    Dec 2, 2003
  2. thomson
    Replies:
    10
    Views:
    2,781
    Eliyahu Goldin
    Jun 20, 2005
  3. thomson
    Replies:
    0
    Views:
    541
    thomson
    Jun 20, 2005
  4. Fred
    Replies:
    3
    Views:
    481
    Alf P. Steinbach
    Aug 10, 2003
  5. Alex Vinokur
    Replies:
    4
    Views:
    676
    Ron Natalie
    Sep 24, 2003
  6. Tor Erik Soenvisen
    Replies:
    14
    Views:
    813
    Tim Roberts
    Nov 23, 2006
  7. Oodini
    Replies:
    1
    Views:
    2,068
    Keith Thompson
    Sep 27, 2005
  8. Alex Hall
    Replies:
    5
    Views:
    343
    Lawrence D'Oliveiro
    May 11, 2010
Loading...