appending extension to filename

Is there a way to do this:

        chomp($out_file = <>);
        $out_file = "${out_file}.csv";

in one line?
frenomulaxAsked:
Who is Participating?
 
ozoConnect With a Mentor Commented:
($out_file = <>) =~ s/\n?$/.csv/;
0
 
maneshrCommented:
frenomulax,

".Is there a way to do this:.."

What exactly are you trying to do?

Please explain in plain english, without posting any code.
0
 
frenomulaxAuthor Commented:
I have a user enter a filename w/out an extension. I change the file name to include a .csv extension, so if they enter 'data_1' it becomes data_1.csv.

The above works fine, I just was wondering if there's a more concise way to do it.
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
maneshrCommented:
frenomulax,

"..so if they enter 'data_1' it becomes data_1.csv. ..."

Try this....

NOTE: Run this program from the command line & let me know what output you can see.

$fn=<STDIN>;

$fn=~ s/\s+$/\.csv/;

print $fn;
0
 
frenomulaxAuthor Commented:
When I run it as a stand alone script it works fine, I get filename.csv printed out. But essentially this is the same as my original code, since there is one line to read from stdin and one line to cat the extension.

I was wondering if there is a way to do it all on one line, reading from stdin, chomp the \n, and cat the extension to the filename.

BTW, this is just for my curiosity, as what I have is working well. No need to spend a lot of your time on it.
0
 
maneshrCommented:
frenomulax,

"..g if there is a way to do it all on one line.."

then i think this is what you are looking for.....

$fn=eval($fn=<STDIN>).'.csv';

print $fn;
0
 
frenomulaxAuthor Commented:
maneshr,
$out_file = eval($out_file = <STDIN>).'.csv';
returns the error Bareword "out" not allowed while "strict subs" in use, although when I comment out 'use strict;' it does work fine.

ozo, your solution works as is, I'll call that the answer.
0
 
frenomulaxAuthor Commented:
one quick question for ozo, can you explain the meaning of the regexp you supplied? I see that it is replacing the first occourance of a new line with '.csv' but what does the ?$ represent ?

I'm slowly getting an understanding of these things. It is literally like learning a new language.

Thanks
0
 
ozoCommented:
? makes the \n optional,and $ anchors it to the end of the string
0
All Courses

From novice to tech pro — start learning today.