Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: appending file name

by marinersk (Priest)
on Jul 17, 2013 at 16:07 UTC ( #1044856=note: print w/replies, xml ) Need Help??


in reply to appending file name

Summary of Problem: You are creating a dynamic array instead of contatenating strings.

Simply to get it to run, I adjusted your code as follows:

#!/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__
Which produced the following:
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:

my $fff=($filenam[0],"_header",".txt"); to my $fff=$filenam[0] . "_header" . ".txt";
And got the following results:
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
This seems to be closer to what you were trying to accomplish.

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!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2019-10-23 03:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?