LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-15-2010, 02:21 AM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
No Prompt on Perl Command.


I am just stating to work with filehandles. I built a script openany.pl have a file names.txt which is just a list of common names.
openany.pl
#!/usr/bin/perl
while ( $nm = <> );
open IN , "$nm" ;
while (<IN>) {
print ;
}
close IN;

When I type in ./openany names.txt nothing happens.
If I type in ./openany.pl and then hit enter, it goes to a new line and then I type in the file name - names.txt and the the program prints out the list of names.

What I am trying to get it to do, is accept the name of the file upon execution, example ./openany names.txt.

So what I want is to type in ./openany.pl names.txt at the command line then hit enter instead of typing ./openay.pl hit <enter> then type in names.txt then hit <enter>.


Probably pretty easy,
 
Old 04-15-2010, 02:27 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by casperdaghost View Post
...
So what I want is to type in ./openany.pl names.txt at the command line then hit enter instead of typing ./openay.pl hit <enter> then type in names.txt then hit <enter>.


Probably pretty easy,
Read

Code:
perldoc perlvar
looking there for ARGV.

Use

Code:
use strict;
use warnings;
in your scripts.

Don't use file handles like IN - they are global.

Read

Code:
perldoc perlopentut
.
 
Old 04-15-2010, 02:30 AM   #3
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
1. How to read command line arguments in to the program.
2. How to read file
3. Merger them up.

Always use , use strict and use warnings at the beginning of your script.

Oops!! It seems I repeated the same suggested by Sergei !!
Delay leads to duplication

Last edited by PMP; 04-15-2010 at 02:33 AM.
 
Old 04-15-2010, 03:27 AM   #4
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
#!/usr/bin/perl
use strict;
use warnings;
my $nm = '';
$nm = <@ARGV>;
open NAM , "$nm";
while (<NAM>) {
print ;
}
close NAM;

thanks guys - I'll use the strict from now on.....
 
Old 04-15-2010, 04:34 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by casperdaghost View Post
#!/usr/bin/perl
use strict;
use warnings;
my $nm = '';
$nm = <@ARGV>;
open NAM , "$nm";
while (<NAM>) {
print ;
}
close NAM;

thanks guys - I'll use the strict from now on.....
You do not need quotes in the item in red.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Use of "Command line perl" in perl script using system command. aditya007 Linux - Newbie 4 11-29-2009 10:08 PM
Perl script: How to bypass prompt after issue a command? darak Linux - Newbie 1 05-01-2009 10:02 AM
Settion Prompt in Perl kzr_merchant Programming 1 12-08-2008 09:45 AM
startx command in FC7 reverts me back to the command prompt sriram87 Linux - Newbie 5 01-03-2008 11:18 PM
Key stroke/command to shut down x and go into the command prompt screen? Fear58 Linux - General 1 07-14-2004 07:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration