Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

G'day Dr Manhattan,

Here's a working script followed by some notes.

[Due to the presence of UTF-8 characters, I've used <pre> blocks instead of <code> blocks.]

#!/usr/bin/env perl -l

use strict;
use warnings;
use utf8;
use autodie qw{:all};

use Cwd;
use File::Spec;

my $base_dir = getcwd();
my $new_dir = 'New Files';
my $out_file = 'NewTextFile.txt';
my $new_dir_path = File::Spec->catdir($base_dir, $new_dir);
my $output_path = File::Spec->catfile($new_dir_path, $out_file);
my $test_string = 'Greek letters: α ... ω';

mkdir $new_dir_path;
open my $output_fh, '>:utf8', $output_path;
print $output_fh $test_string;
close $output_fh;
use utf8;

You should use the utf8 pragma if your source contains UTF-8 characters. With my code as it is, I get this output:

$ cat 'New Files/NewTextFile.txt'
Greek letters: α ... ω

Without "use utf8;", I get:

$ cat 'New Files/NewTextFile.txt'
Greek letters: α ... Ï
use autodie qw{:all};

There are multiple points of potential failure (mkdir, open, print, close). Rather than attempting to identify each one and then hand-crafting appropriate "... or die ..." code for them, let Perl do this for you with the autodie pragma.

use Cwd;

Unless given an absolute path, mkdir will create the directory relative to the current working directory; not relative to the script directory (e.g. calling "../script.pl" from "script_dir/sub_dir" will create the new directory in "script_dir/sub_dir"; not in "script_dir"). You can use Cwd if you want that behaviour; of course, you may want different behaviour.

use File::Spec;

I don't know what platform you're on. You may want to run this code on multiple platforms. File::Spec provides portable file name operations.

open my $output_fh, '>:utf8', $output_path;

I've retained the 3-argument form of open but I've used a lexical variable for the filehandle. This is to avoid collisions with global filehandles that might have been used elsewhere in your code (e.g. OUTPUT) or confusion with known, existing barewords (e.g. O).

-- Ken


In reply to Re: Print output file to a directory by kcott
in thread Print output file to a directory by Dr Manhattan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-25 11:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found