Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Oh ye who are in the gall of perlness...what maketh me mistaketh :)

by snafu (Chaplain)
on Apr 19, 2001 at 02:06 UTC ( [id://73678]=perlquestion: print w/replies, xml ) Need Help??

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

The output below shows what the script is doing. I am doing something wrong in the if-then statement (something with my operator perhaps?)

user@hopper> perl -w use strict; use Cwd; my $sandbox = "/tmp/frontier"; test("$sandbox"); sub test() { my $directory = "@_"; my $pwd = getcwd; print "Directory is: $directory\nYou path is: " . getcwd ."\n"; if ( "$pwd" eq "$directory" ) { print "You are not in the same directory as $directory!\n"; } else { print "You are in $directory\n"; } } Sandbox is: /tmp/frontier Your pwd is: /u20/home/user You are in /tmp/frontier

----------
- Jim

  • Comment on Oh ye who are in the gall of perlness...what maketh me mistaketh :)
  • Download Code

Replies are listed 'Best First'.
Re: Oh ye who are in the gall or perlness...what maketh me mistaketh :)
by satchboost (Scribe) on Apr 19, 2001 at 02:16 UTC
    My first response is that you should simply be doing test($sandbox);, not test("$sandbox");. That isn't going to make a difference here, but my $directory = "@_"; makes me cringe. Doing my ($directory) = @_; would seem to be more standard.

    Along the same lines, doing if ("$pwd" eq "$directory") { also does too much string interpolation. Just do if ($pwd eq $directory) {.

    Now the problem you're having is that you're saying if the two variables are string-wise equivalent, complain that you're not in the same directory, otherwise say that you are. You probably wanted one of the following:
    unless ($pwd eq $directory) {
    or
    if ($pwd ne $directory) {

    That should fix your problem.

Re: Oh ye who are in the gall or perlness...what maketh me mistaketh :)
by indigo (Scribe) on Apr 19, 2001 at 02:12 UTC
    I think you want "$pwd" eq "$directory" to be "$pwd" ne "$directory"
      <whisper>
      pssst.. you don't need the quotes around the variable names.
      </whisper>

      Cheers,
      KM

Re: Oh ye who are in the gall or perlness...what maketh me mistaketh :)
by Sherlock (Deacon) on Apr 19, 2001 at 02:19 UTC
    Jim,
    I'm not sure exactly what you're trying to accomplish, but here's a whack at it: You want to check the current working directory (CWD) against the directory you have placed into $sandbox. If they're the same, you print "You are in the same directory as..." and, if they are different, you print "You are in directory...". Does that sound right?

    If that's the case, I believe you have your if test backwards. If the two are equal, you want to print the line, "You are in..." and if they're different, you want to print "You are not in..."

    Try this:

    if ( "$pwd" eq "$directory" ) { print "You are in $directory\n"; } else { print "You are not in the same directory as $directory!\n"; }
    It's quite possible I'm missing what you're really trying to do, but this was what I could come up with.

    -Sherlock
Re: Oh ye who are in the gall or perlness...what maketh me mistaketh :)
by snafu (Chaplain) on Apr 19, 2001 at 02:29 UTC
    I don't know...maybe I didn't get enough sleep last night. the "ne" vs "eq" worked.

    The use of my "s is habit from shell scripting. Am I really to believe that Perl will not get things confused if I don't use "s for somethings? If so, great!! If not, what should I watch out for? When should I absolutely use "s in my scripts (besides the obvious prints, etc)? As I recall, I tried calling a function that I had written and I tried passing a value to the function without using quotes. When I tried to compile it Perl screamed at me because the value was not quoted. Once I quoted it all was well.

    Sorry to have caused a burden guys. This really was, after looking at your responses, a dumb question. Thanks for the help.

    ----------
    - Jim

      In Perl, quotes are for literals only. If you don't have a literal, you don't need a quote. In the shell, you have to use quotes to get many characters with embedded whitespace treated as a single entity. Different purpose, so different timing.

      -- Randal L. Schwartz, Perl hacker

        Execellent. Thanks guys!

        ----------
        - Jim

      If you pass a value, you need to quote it. If you pass a scalar ($foo), then you do not need to quote it. You're passing variables around, not values.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-25 03:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found