chdir ($variable)

Discussion in 'Perl Misc' started by maheshpop1@gmail.com, May 17, 2006.

  1. Guest

    Hi Guys,

    the following piece of code doesnt seem to work

    my $folder= $hash{working_directory};
    chdir("$folder") or die "can't change dir $folder" ;

    I printed out the output and checked if the the variable $folder was
    being loaded and it was being loaded correctly.

    while this works if I put in the absolute path.

    chdir("/test/rep") or die "can't change dir /test/rep" ;

    hmmmm any thoughts.

    cheers,
    pop.
    , May 17, 2006
    #1
    1. Advertising

  2. David Squire Guest

    wrote:
    > Hi Guys,
    >
    > the following piece of code doesnt seem to work
    >
    > my $folder= $hash{working_directory};
    > chdir("$folder")


    Why are you quoting that variable?

    > or die "can't change dir $folder" ;


    Perl is your friend. Let it help by telling what went wrong:

    or die "can't change dir $folder: $!";

    >
    > I printed out the output and checked if the the variable $folder was
    > being loaded and it was being loaded correctly.
    >
    > while this works if I put in the absolute path.
    >
    > chdir("/test/rep") or die "can't change dir /test/rep" ;
    >
    > hmmmm any thoughts.


    hmmmm. Are you sure that the working directory of your script is what
    you think it is? Quick and dirty check: print `pwd`;

    DS
    David Squire, May 17, 2006
    #2
    1. Advertising

  3. Guest

    oh yeah forgot to add it throws an error
    "cant change dir /test/rep
    No such file or directory at custom.pl at like 156"

    any ideas,

    cheers,
    pop
    , May 17, 2006
    #3
  4. Guest

    Hi David,

    Still learning, I put in the $! and it gave me this error

    "can't change dir /test/rep/
    No such file or directory at custom.pl line 156."

    The working dir of the script is /users/XXX/perlscripts/ and I am
    trying to access a folder at the home level /test/rep to run a command
    in that folder.

    pop
    , May 17, 2006
    #4
  5. David Squire Guest

    wrote:
    > oh yeah forgot to add it throws an error
    > "cant change dir /test/rep


    Didn't you say in the first post that it *works* if you use the absolute
    path? Which is true?

    > No such file or directory at custom.pl at like 156"


    "like"? Please cut and paste real error messages.

    >
    > any ideas,


    Yes. As suggested in my previous post, check what directory your script
    is actually running in.

    Also, please read and follow the posting guidelines if you hope that
    people will continue helping you. Start by quoting context when you reply.

    DS
    David Squire, May 17, 2006
    #5
  6. wrote:
    > Hi Guys,
    >
    > the following piece of code doesnt seem to work
    >
    > my $folder= $hash{working_directory};
    > chdir("$folder") or die "can't change dir $folder" ;
    >
    > I printed out the output and checked if the the variable $folder was
    > being loaded and it was being loaded correctly.
    > [...]


    Perhaps $folder has a trailing \n. Chomp it to be sure.

    BTW, you don't have to put variables in double quotes like that. Just
    use "chdir($folder)."
    Sumo Wrestler (or just ate too much), May 17, 2006
    #6
  7. Sumo Wrestler (or just ate too much) <> wrote:
    > wrote:


    >> chdir("$folder") or die "can't change dir $folder" ;


    > Perhaps $folder has a trailing \n. Chomp it to be sure.



    If you adopt a helpful style of diag message, then such cases
    become obvious when they happen:

    chdir $folder or die "can't change dir '$folder' $!";


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, May 17, 2006
    #7
  8. Paul Lalli Guest

    Ferry Bolhar wrote:
    > pop:
    >
    > > my $folder= $hash{working_directory};
    > > chdir("$folder") or die "can't change dir $folder" ;

    >
    > Quotes not required in chdir.


    That is a misstatement of the truth. Quotes are dependent on what
    they're quoting, not where they're being used. Double quotes are not
    required around a single scalar variable. Quotes of some kind are
    definately required around an otherwise bare word, if strict is
    enabled. Quotes of some kind are always required around a string that
    consists of non-word characters.

    chdir($folder); #good
    chdir("$folder"); #okay, but pointless (see: perldoc -q quoting)
    chidr('$folder'); #probably wrong, unless you have a folder actually
    named '$folder'

    chdir(tmp); #bad, breaks if strict is enabled.
    chdir("tmp"); #good
    chdir('tmp'); #better

    chdir(/tmp/foo); #wrong, breaks regardless of strict
    chdir("/tmp/foo"); #good
    chdir('/tmp/foo'); #better

    Paul Lalli
    Paul Lalli, May 17, 2006
    #8
  9. At 2006-05-17 06:48AM, <> wrote:
    > Hi David,
    >
    > Still learning, I put in the $! and it gave me this error
    >
    > "can't change dir /test/rep/
    > No such file or directory at custom.pl line 156."
    >
    > The working dir of the script is /users/XXX/perlscripts/ and I am
    > trying to access a folder at the home level /test/rep to run a command
    > in that folder.
    >
    > pop


    Are you trying to chdir to /users/XXX/perlscripts/test/rep or /test/rep ?
    If the former, drop the leading slash.

    --
    Glenn Jackman
    Ulterior Designer
    Glenn Jackman, May 17, 2006
    #9
  10. Paul Lalli <> wrote:

    > Quotes of some kind are always required around a string that
    > consists of non-word characters.



    s/consists of/contains/;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, May 17, 2006
    #10
  11. Guest

    Tad McClellan wrote:
    > Paul Lalli <> wrote:
    >
    > > Quotes of some kind are always required around a string that
    > > consists of non-word characters.

    >
    >
    > s/consists of/contains/;
    >
    >
    > --
    > Tad McClellan SGML consulting
    > Perl programming
    > Fort Worth, Texas

    Hi Guys

    Found the problem. t indeed is a new line character at the end of the
    value which is causing it. Somehow chomp didnt work either here.

    I was reading from a text file a line and splitting it into different
    key, value pairs in a hash map. Take this line in the text file
    1:query_action,checkin:working_directory,/temp/rep

    If you observe there is a :)) seperating the one key,value from
    another. The last one is the working_directory and there is no :))
    after its value. So when I read it in
    my $folder= $hash{working_directory};
    .. It loaded itself as
    /temp/rep with an additional \n (newline) character. So the chdir did
    not work. I changed the text file to show as

    1:query_action,checkin:working_directory,/temp/rep: <-------- (added
    another :)) at the end of the string).

    Now it loads okay. somehow chomp didnt work either.

    thanks for the assist guys (Still learning programming :)))) so kindly
    bear with any newbie questions from my side.
    pop.
    , May 18, 2006
    #11
  12. Ben Morrow Guest

    Quoth "Ferry Bolhar" <>:
    > Paul Lalli:
    > > Quotes of some kind are always required around a string that
    > > consists of non-word characters.

    >
    > Almost always. Not in regular expressions.


    ObPedant: The // (or whatever) around a regex *are* quote characters :).

    Ben

    --
    All persons, living or dead, are entirely coincidental.
    Kurt Vonnegut
    Ben Morrow, May 18, 2006
    #12
  13. Ferry Bolhar <> wrote:
    > Paul Lalli:


    >> Quotes of some kind are always required around a string that

    ^^^^^^^^^^^^^^^
    >> consists of non-word characters.

    >
    > Almost always. Not in regular expressions.



    A "regex" is not a "string".


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, May 18, 2006
    #13
    1. Advertising

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. Jon Schull

    cgi-tb after os.chdir

    Jon Schull, Jul 14, 2003, in forum: Python
    Replies:
    0
    Views:
    399
    Jon Schull
    Jul 14, 2003
  2. Yun Mao

    os.environ and os.path.chdir

    Yun Mao, Sep 20, 2003, in forum: Python
    Replies:
    4
    Views:
    427
    Thomas Bellman
    Sep 20, 2003
  3. Tom McDavid
    Replies:
    0
    Views:
    470
    Tom McDavid
    May 13, 2004
  4. Philippe C. Martin

    os.chdir + GUI question

    Philippe C. Martin, Dec 3, 2005, in forum: Python
    Replies:
    0
    Views:
    290
    Philippe C. Martin
    Dec 3, 2005
  5. Replies:
    3
    Views:
    593
    DataSmash
    Jun 2, 2006
Loading...

Share This Page