Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: opening new file in different directory

by graq (Curate)
on Oct 23, 2001 at 19:10 UTC ( [id://120808]=note: print w/replies, xml ) Need Help??


in reply to opening new file in different directory

To start with, I would not use relative paths. I would define a base path and work from there.
#!perl -w use strict; my $file = "graq.txt"; my $source = &get_path("oldfiles/$file"); my $result = &get_path("newfiles/$file"); open( READ, "<$source" ) or die( "Could not open $source: $!\n" ); open( WRITE, ">$result" ) or die( "Could not write to $result: $!\n" ) +; while(<READ>) { # Do stuff. print WRITE $_; } close WRITE; close READ; # --------------------------------- # Local Functions. sub base_path { '/home/main/sub'; } sub get_path { return &base_path."/".$_[0]; }
You could define them with use constant or some other way. Which is just another TIMTOWTDI...

editTypo in die()/edit

--
Graq

Replies are listed 'Best First'.
Re: Re: opening new file in different directory
by George_Sherston (Vicar) on Oct 23, 2001 at 19:32 UTC
    I might be being a bit thick but I can't get it to work. It looks as though the WRITE line only works if $result is a file, not a file path. I find that
    my $file = "/home/htdocs/hosted/thinweb/trainingboard.co.uk/consumers/ +savedsearches/$g{'SearchFile'}"; open(WRITE,">$file") or die $!;
    fails, whereas
    chdir "/home/htdocs/hosted/thinweb/trainingboard.co.uk/consumers/saved +searches"; open(WRITE,">$g{'SearchFile'}") or die $!;
    succeeds. (NB the earlier lines I posted were re-written to make the examples easier to read - the above are as in my actual script, just in case there's something else funny going on... in which case I apologise for my over-zealous desire to do a tidy post).

    § George Sherston
      Ok. First things first, add some verbosity to your error handling:
      open(WRITE,">$file") or die( "Cannot write to $file: $!" );

      And then let us know what the whole error is, makes it much easier :)

      --
      Graq

        Same error as before, No such file or directory.

        I left out the verbosity because (A) I haven't decided how to do it yet, so not sure what I have to put in my error string (B) $! points me to the line, which is all I need to know for now... and I know it well anyway, alas (C) I didn't want too much wrapping and (D) I guessed that nobody wd be copying my code since it's effectively labelled as broken.

        Still, I know this is one of those points on which the monastery is rightly strict.... /me goes into the dungeon for a moment's mortification.

        § George Sherston
        Not to be an ass, but how would this help us? It seems the germane info is in $! not the script name and line number.

        I'm really asking and not trying to fish slap you.

        Ira,

        "So... What do all these little arrows mean?"
        ~unknown

      Man this smells like a permissions issue. It's possible to have permission to cd into a directory without having permission to ls in it. This is a freaky benefit of the *nix user/group permission bits. If'n I was you, I'd play with accessing these files from the command line, just with touch and vi, to see if there's some intermediate directory that's got permission funkiness. Make sure you do this with the user that the script runs as.

      Or, another good thing to see, would be the ls -ld output on the directories you've listed up there.

      This is one of those things that, as blackmateria said, should work. It's irritating that it doesn't.

      Ira,

      "So... What do all these little arrows mean?"
      ~unknown

        I am not convinced it has anything to do with the permissions on the file or directory.
        From the paths used in the original post, it is not a Windoze machine, so with a little cut'n'paste from a Mandrake box:
        __PASTE-ONE__ ~/temp/graq> ls -l -r-------- 1 graq graq 7 Oct 24 15:50 Testing.txt ~/temp/graq> cd .. ~/temp> chmod 100 graq ~/temp> perl relpath.pl graq/Testing.txt: Permission denied __END__
        So the error is Permission denied for a file with permission problems.
        __PASTE-TWO__ ~/temp> cat relpath.pl #!/usr/bin/perl -w use strict; my $file = 'graq/Testing.txt'; open( TESTING, ">$file") or die ( "$file: $!\n" ); print TESTING `date`; close TESTING; __END__

        <a href="http://www.graq.co.uk">Graq</a>

Log In?
Username:
Password:

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

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

    No recent polls found