Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

TK filehandle path

by welle (Beadle)
on Jul 09, 2012 at 16:27 UTC ( #980718=perlquestion: print w/ replies, xml ) Need Help??
welle has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, what I am missing? I have these 2 subrutines: if I run create_file a new file is created, but I if I run it after running the subrutine open_directory_file (which reads the files stored in a directory) the other subrutine gives me errors, namely that I am trying to write on close filehandle MYFILE. Why? Thanks

my $output_directory="TEMPORARY"; sub open_directory_file { print "Open files from directory...\n"; my @all_file_contents; my $Directory = File::Spec->canonpath( $mw->chooseDirectory() ); my $shortpath = Win32::GetShortPathName( "$Directory" ); chdir($shortpath) or die "$!"; opendir (DIR, "$shortpath") or die "$!"; my @files = grep {/.*?\.txt/} readdir DIR; close DIR; } sub create_file{ open (MYFILE, ">$output_directory/token.txt"); print MYFILE "ciao"; close (MYFILE); }

The working subrutine open_directory_file (thanks roboticus for the hint!)

my $output_directory="TEMPORARY"; sub open_directory_file { print "Open files from directory...\n"; my @all_file_contents; #added following line my $pwd = cwd(); my $Directory = File::Spec->canonpath( $mw->chooseDirectory() ); my $shortpath = Win32::GetShortPathName( "$Directory" ); chdir($shortpath) or die "$!"; opendir (DIR, "$shortpath") or die "$!"; my @files = grep {/.*?\.txt/} readdir DIR; close DIR; #added following line #go back to original directory chdir($pwd); }

Comment on TK filehandle path
Select or Download Code
Re: TK filehandle path
by roboticus (Canon) on Jul 09, 2012 at 16:40 UTC

    welle:

    Since you're changing the current directory in open_directory_file(), the output directory is no longer where you think it is, relative to your current directory.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Thank you! You brought me to the right "path" ;)

      I have updated a working solution.

Re: TK filehandle path
by zentara (Archbishop) on Jul 09, 2012 at 16:51 UTC
    I don't have Win32, so I can't get shortpath, but I suspect it's because you chdir, and that path to file is no longer there.

    Also, when you create_file() you should check the open

    sub create_file{ open (MYFILE, ">$output_directory/token.txt") or die "$!\n"; print MYFILE "ciao"; close (MYFILE); }

    I get "no such file or directory". It would also help if you made a self-contained fully running example.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://980718]
Approved by tobyink
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2014-07-13 22:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (252 votes), past polls