Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Net::FTP & if statement behavior

by alienhuman (Pilgrim)
on Jan 02, 2002 at 22:44 UTC ( [id://135751]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I'm using Net::FTP to great effect, however I've recently found some very strange behavior when I put a ftp method call into an if statement.

To wit:

The following method returns the value "1" when it is successful, as described in the perldoc, and evidenced in this snippet from the debugger...

DB<2> print $ftp->cwd( $whole ); Net::FTP=GLOB(0x47dbf4)>>> CWD /web Net::FTP=GLOB(0x47dbf4)<<< 250 CWD command successful. 1

However, the same method call inside an if statement doesn't evaluate TRUE... which is not at all what I expected...

DB<3> print if( $ftp->cwd( $whole ) ); Net::FTP=GLOB(0x47dbf4)>>> CWD /web Net::FTP=GLOB(0x47dbf4)<<< 250 CWD command successful. DB<4>

Why doesn't this if( $ftp->cwd( ... ) ); evaluate to true, if the value returned by $ftp->cwd is 1?

Thanks,

Chris

Replies are listed 'Best First'.
Re: Net::FTP & if statement behavior
by projekt21 (Friar) on Jan 02, 2002 at 22:53 UTC
    Hi,
    what I can see is:
    print 1; # prints "1" print if 1; # prints nothing
    try something like:
    print 1 if ( $ftp->cwd( $whole ) );
    Does this help you?

    alex pleiner <alex@zeitform.de>
    zeitform Internet Dienste

      Nice catch, projekt21.

      On first scan of the code, I missed the if (or at least the behavioral significance of it). Conceivably, alienhuman may have made the same mistake.

      dmm

      You can give a man a fish and feed him for a day ...
      Or, you can
      teach him to fish and feed him for a lifetime
      Ugh. What a rookie mistake. Thx.
Re: Net::FTP & if statement behavior
by derby (Abbot) on Jan 02, 2002 at 23:03 UTC
    Chris,

    but it is evaluating as "true." There are no "real" boolean operators types in perl. Somewhere in the docs (try perldoc perlsyn) it states that a statement is "false" if it evaluates to the null string (""), 0, "0" (string of just ascii 060), or undef. Everything else is true. Now there are some extra things when dealing with "0" but basically with Net::FTP, you could do something like this:

    if( $ftp->cwd( $whole ) ) { print "Ftp command suceeded\n"; } else { print "Ftp command failed\n"; }

    Update Ug. I need to more clearly read the questions. -derby

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-24 02:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found