in reply to appending file name
Simply to get it to run, I adjusted your code as follows:
Which produced the following:#!/usr/bin/perl -w use strict; my $filename = <>; chomp $filename; my @filenam = split ('\.', $filename); my $fi; my $fff=($filenam[0],"_header",".txt"); open($fi,">", $fff) or die "Open failed for $fff: $!"; __END__
C:\Steve\Dev\PerlMonks\P-2013-07-17@0950-Filename-Transmogrification>p +erl filenameTransmogrification.pl Useless use of array element in void context at filenameTransmogrifica +tion.pl line 9. Useless use of a constant in void context at filenameTransmogrificatio +n.pl line 9. abc.dat C:\Steve\Dev\PerlMonks\P-2013-07-17@0950-Filename-Transmogrification>d +ir Volume in drive C has no label. Volume Serial Number is E88F-F456 Directory of C:\Steve\Dev\PerlMonks\P-2013-07-17@0950-Filename-Transm +ogrification 07/17/2013 09:52 AM <DIR> . 07/17/2013 09:52 AM <DIR> .. 07/17/2013 09:52 AM 0 .txt 07/17/2013 09:51 AM 232 filenameTransmogrification.pl 3 File(s) 466 bytes 2 Dir(s) 78,355,476,480 bytes free
Interestingly, I was expecting it to create 3.txt, but it merely created .txt. I will have to look into why; the clue is in the warning messages, I'm sure.
Anyway, I changed one line:
And got the following results:my $fff=($filenam[0],"_header",".txt"); to my $fff=$filenam[0] . "_header" . ".txt";
This seems to be closer to what you were trying to accomplish.C:\Steve\Dev\PerlMonks\P-2013-07-17@0950-Filename-Transmogrification>p +erl filenameTransmogrification2.pl abc.dat C:\Steve\Dev\PerlMonks\P-2013-07-17@0950-Filename-Transmogrification>d +ir Volume in drive C has no label. Volume Serial Number is E88F-F456 Directory of C:\Steve\Dev\PerlMonks\P-2013-07-17@0950-Filename-Transm +ogrification 07/17/2013 09:53 AM <DIR> . 07/17/2013 09:53 AM <DIR> .. 07/17/2013 09:52 AM 0 .txt 07/17/2013 09:53 AM 0 abc_header.txt 07/17/2013 09:51 AM 232 filenameTransmogrification.pl 07/17/2013 09:52 AM 234 filenameTransmogrification2.pl 4 File(s) 466 bytes 2 Dir(s) 78,354,427,904 bytes free
So, some observations:
1) Simple string contantenation in Perl is done using the . operator.
2) You might benefit from thinking in terms of using reciprocal functions. You used split to break apart the name; it would be prudent to use join to put it back together. split and join are essentially reciprocal functions.
3) There are modules which do a pretty good job of handling most filename related functions, and often honor the rules and trends of the OS you're running on, but this looks like a learn-Perl kind of exercise so there is much to be gained from learning how to do the basic work by hand. Just be aware that modules, especially ones published to CPAN, usually handle a lot of the corner cases you will overlook, so learning to leverage the publicly-shared work of others is a valuable skill in this profession.
Good luck!
|
|---|