Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Rather than using the fully-qualified filename all the time you may prefer to use <code>chdir $today</code> before you open the file. This will change the current working directory and force a file specified using a relative path or no path at all to be opened relative to the new directory.</p> <p>In addition, using the <a href="https://metacpan.org/module/autodie" rel="nofollow"><code>autodie</code></a> pragma will avoid the need to check the status of <code>open</code>, <code>close</code> etc.; and using lexical filehandles is preferable for a number of reasons, including implicit closing of files when the filehandle variable goes out of scope.</p> <p>This is how your code would look.</p> <pre><code>use strict; use warnings; use autodie; my $today = 'today'; my $savepage = 'savepage'; my $data = 'data'; mkdir $today unless -d $today; { chdir $today; open my $fh, '&gt;&gt;', $savepage; print $fh $data; } </code></pre> <p>However, if your program deals with files in multiple directories then it is awkward to <code>chdir</code> backwards and forwards between them, and the original directory has to be explicitly saved otherwise it will be forgotten. In this case the <a href="https://metacpan.org/module/File%3a%3achdir" rel="nofollow"><code>File::chdir</code></a> module may be helpful. It provides the <code>$CWD</code> package variable which will change the current working directory if its value is changed. It can also be localized like any other package variabled so that the original value will be restored at the end of the localizing block.</p> <p>Here is an example.</p> <pre><code>use strict; use warnings; use File::chdir; use autodie; my $today = 'today'; my $savepage = 'savepage'; my $data = 'data'; mkdir $today unless -d $today; { local $CWD = $today; # Change working directory to $today open my $fh, '&gt;&gt;', $savepage; print $fh $data; } # Working directory restored to its previous value </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload