Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: Copy rows of file to new document

by choroba (Abbot)
on Jan 25, 2013 at 09:25 UTC ( #1015295=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Copy rows of file to new document
in thread Copy rows of file to new document

  • my $dir_path="\Incoming\Temp";
    This is not doing what you think. The backslash has a special meaning in double quotes. Fortunately, you can use a normal slash /.
  • opendir (Incoming\Temp, $tmp_dir) or die $!;
    Where is $tmp_dir coming from? Do you mean $dir_path? Moreover, you cannot use a non-alphanumeric character in a dirhandle name. Remove the backslash. Even better - use a lexical dirhandle:
    opendir my $DIR, $dir_path or die $!;
  • open (MYFILE, '>>vehicles.txt');
    Not really an error, but can be improved: use a lexical filehandle, 3-argument version of open, and test for failure:
    open my $OUT, '>>', 'vehicles.txt' or die $!;
  • Also note that $file_content is empty and undeclared. You probably want to use it inside the while loop to populate the output file:
    while (my $filename = readdir $DIR) { open my $FILE, '<', "$dir_path/$filename" or die $!; while (my $content = <$FILE>) { print $OUT $content; } }
    BTW, have you noticed the directory path must be prefixed to the filename?
  • CLOSEDIR;
    Perl is case sensitive. The function is in lowercase: closedir. Also, give it an argument.
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re^3: Copy rows of file to new document
Select or Download Code
Re^4: Copy rows of file to new document
by phineas629 (Novice) on Jan 25, 2013 at 21:32 UTC

    I've given this a couple attempts.I'm really trying to read over the notes but am just plain lost. This is the error I got with the code:

    "my" variable $dir_path masks earlier declaration in same statement at parse.pl line 5. syntax error at parse.pl line 5, near "opendir" Can't use global $! in "my" at parse.pl line 5, near "die $!" syntax error at parse.pl line 7, near ") {" Global symbol "$file_content" requires explicit package name at parse.pl line 8. Execution of parse.pl aborted due to compilation errors.

    #!/usr/bin/perl use strict; use warnings; my $dir_path="c:/incoming/temp;" opendir my $DIR, $dir_path or die $!; open my $OUT, '>>', 'c:/vehicles.txt' or die $!; while (my $file_name = readdir($DIR) {print "$file_name\n";} print MYFILE $file_content; close (MYFILE)
      On line 4, exchange the double quote and the semicolon. Then, reread my comments.
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (13)
As of 2014-10-30 23:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (211 votes), past polls