473,386 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Getting argument as filename or STDIN

89
I am trying to read a file. The file contents can be either STDIN or a file name.

When it is from a file, I open as follows.
Expand|Select|Wrap|Line Numbers
  1. Open filename ($filename)
  2. While(<$filename>){
  3. #process each line
  4. }
  5.  
Otherwise if it is from STDIN, I always use
Expand|Select|Wrap|Line Numbers
  1. while(<>){
  2. #process each line
  3. }
  4.  
Now my constraint is the user can either specify the file name or pipe as STDIN from another program. How to handle that situation in the same script?


My perl program has the usage as below.

Perl converter.pl --in file <filename or STDIN> --out file <out filename>

Please let me knowhow to handle this. Thanks.
Nov 26 '10 #1

✓ answered by numberwhun

First, when someone pipes output to your perl script, it will probably be similar to this:

Expand|Select|Wrap|Line Numbers
  1. cat file.txt | script.pl
  2.  
In this case, the input to the perl script can actually be caught using the STDIN filehandle. In other words, if the user cats the file they wish to process with your script and pipes it to your file, you could simply:

Expand|Select|Wrap|Line Numbers
  1. while(<STDIN>){
  2.     some code;
  3. }
  4.  
To handle both I would test to see if the command line options are set and if not, then reference STDIN instead.

Regards,

Jeff

3 3738
numberwhun
3,509 Expert Mod 2GB
First, when someone pipes output to your perl script, it will probably be similar to this:

Expand|Select|Wrap|Line Numbers
  1. cat file.txt | script.pl
  2.  
In this case, the input to the perl script can actually be caught using the STDIN filehandle. In other words, if the user cats the file they wish to process with your script and pipes it to your file, you could simply:

Expand|Select|Wrap|Line Numbers
  1. while(<STDIN>){
  2.     some code;
  3. }
  4.  
To handle both I would test to see if the command line options are set and if not, then reference STDIN instead.

Regards,

Jeff
Nov 27 '10 #2
lilly07
89
Thanks forr your help Jeff.
Nov 30 '10 #3
rovf
41
Well, your "open" statement isn't even valid Perl, but anyway:

You could set up a file handle like this (warning, code not tested):

Expand|Select|Wrap|Line Numbers
  1. my $input = *STDIN;
  2. if($read_from_file) {
  3.   open(FH,...);
  4.   $input = *FH;
  5. }
  6.  
and read from <$input>.
Dec 8 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Steve Lefevre | last post by:
Hey folks - Working with vba in Access 2k, I'm making a form to automatically import a spreadsheet. Everything works great when I hardcode the name of the spreadsheet file; however, I want the...
1
by: Andres Romero | last post by:
Using the FielDialog class and it's FileName propertie I can know the Path and file name selected by the user, but now I need to know only the file name without the full path (eg....
2
by: Strubel Gregory | last post by:
I want to get the real name of my .exe and the current directory if the user change it, but i haven't find a function that do the job. Please help Thanks
2
by: Markus Pitha | last post by:
Hello, Some time ago I tried to find a solution for preventing buffer overflows in stdin. I thought getc was the solution but today I came to a problem. I wanted to use my cognitions for a...
4
by: SFlpm | last post by:
Hello, I was wondering, how do I get the actual file name of a file, I tried using Dim ofd As New OpenFileDialog ... If ofd.ShowDialog = Windows.Forms.DialogResult.OK then 'problem is here!...
1
by: tito | last post by:
I have used use Spreadsheet::WriteExcel::FromDB::Query package to retrieve the values from database to Excel sheet using the following program. use Spreadsheet::WriteExcel::FromDB::Query; my...
1
by: psbasha | last post by:
Hi, I would like to get the path of the given file name. Say I/P: C:\Sample\Sample1.txt
4
by: jonniethecodeprince | last post by:
#This is a program from dummies.com $TheDB = 'edata.txt'; # Open the database file but quit if it doesn't exist open(INDB, $TheDB) or die "The database $TheDB could " . "not be found.\n"; ...
2
kaarthikeyapreyan
by: kaarthikeyapreyan | last post by:
I want to get the type of arguments being passed to a method for eg. def sample(num,string): print num print string sample(1,'hello') am expecting an output like : num -- type int...
1
by: zmohamed | last post by:
I'm passing an ofstream object to a function within my program. In that function, I'd like to extract the path and filename of the ofstream object, modify the filename only, create a new ofstream...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.