sub_chick has asked for the wisdom of the Perl Monks concerning the following question:

hey monks,
I was messing around with FILEHANDLE and tr/ / / earlier today and came up with this little script just for practice:
#! /usr/bin/perl print 'Enter path of file: '; chomp($path = <>); open(FH,"$path") || die "$!\n"; while (<FH>){ $_=~ tr/a-zA-Z/0-310/; print " $_\n";}
What I really wanted to do here was to add a few more lines of code that would make perl create a new file in the same directory and prompt me to enter the new name of the file but to not overwrite the file I opened above. I was looking around and do believe that I need to add:
use cwd; $dir = getcwd; #etc.. print 'Enter new file name: ';
I am looking around perlmonks and all that I have been able to come up with so far are:
Node#183899
and
mkpath()
but I am unsure if whether or not these pages will suffice for what I am trying to do.

"Es gibt mehr zu Leben als Bücher, kennen Sie. Aber nicht viel mehr " -(Der Smiths)

Replies are listed 'Best First'.
Re: Take file A , translate it, place in new file
by GrandFather (Saint) on Dec 12, 2005 at 18:52 UTC

    A couple of comments first. Standard comment 1 applies: use strict; use warnings;.

    More immportant for your future code: you should use the three parameter open open (FH, "<", $path) to avoid the possibility of nasty stuff happening due to bad (in a couple of senses) file names.

    Having dealt with that here is some code to start with:

    #! /usr/bin/perl use strict; use warnings; print 'Enter path of old file: '; chomp(my $inPath = <>); open (inFile, "<", "$inPath") || die "Failed to open in file: $!\n"; print 'Enter path of new file: '; chomp(my $outPath = <>); open (outFile, ">", "$outPath") || die "Failed to create out file: $!\ +n"; while (<inFile>){ $_=~ tr/a-zA-Z/0-310/; print outFile " $_\n"; } close inFile; close outFile;

    DWIM is Perl's answer to Gödel
      Thank you GrandFather, your script did exactly what I was looking for now I can tear it apart and hopefully understand the process better.

      "Es gibt mehr zu Leben als Bücher, kennen Sie. Aber nicht viel mehr " -(Der Smiths)
Re: Take file A , translate it, place in new file
by davidrw (Prior) on Dec 12, 2005 at 18:57 UTC
    might not exactly suit your purposes, but good to be aware of the command-line ability of perl to do this (see perlrun):
    perl -i.bak -pe 'tr/a-zA-Z/0-310/; $_ .= "\n"' /tmp/foo.txt
    (note: it modifies the original but saves a copy as /tmp/foo.txt.bak)
Re: Take file A , translate it, place in new file
by InfiniteSilence (Curate) on Dec 12, 2005 at 19:07 UTC
    Or if you really want to name the other file differently:
    perl -pe "tr/a-zA-Z/0-310/;" test2.txt > otherfile.txt

    Celebrate Intellectual Diversity

Re: Take file A , translate it, place in new file
by graff (Chancellor) on Dec 13, 2005 at 02:56 UTC
    When it's just a matter of giving perl a couple of file names, one for input and one for output, I really prefer having that stuff all on the command line, rather than requiring the perl script itself to prompt for an interactive response from the person running the script. There are a few ways to do this:
    • expect input and output file names as command line args, which show up in @ARGV:
      #!/usr/bin/perl my $Usage = "$0 input_file.name output_file.name\n"; ( @ARGV == 2 and -f $ARGV[0] ) or die $Usage; open( IN, "<", $ARGV[0] ) or die "$ARGV[0]: $!"; open( OUT, ">", $ARGV[1] ) or die "$ARGV[1]: $!"; while (<IN>) { tr/a-zA-Z/A-Za-z/; # invert case, just for fun print OUT; }
    • use output redirection in the shell command (and just an input file-name arg, or input redirection, or a pipe), which means the perl script does not need to explicitly open anything:
      #!/usr/bin/perl my $Usage = "Usage: $0 [<] input.file > output.file\n"; die $Usage if (( @ARGV > 0 ) ^ ( -t STDIN )); while (<>) { tr/a-zA-Z/A-Za-z/; # invert case, just for fun print; }
    (In the second example, the "if" condition will cause the script to die with with usage summary if you run it with no args and STDIN is not coming from a pipe or redirection.)

    As for your usage of "tr///", I wonder what you're trying to accomplish with  tr/a-zA-Z/0-310/ because that really means:

    a -> 0 \ b -> 1 \ "0-3" defines a continuous character range c -> 2 / starting at "0", ending at "3", incl. "1" & "2" d -> 3 / e -> 1 fg-zA-Z -> 0
    When I see a dash used between two numerics like that with the tr/// operator, my first impression is that the intent is probably different from the actual result...