3 Replies - 2807 Views - Last Post: 16 October 2013 - 12:45 AM

#1 amture106  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 154
  • Joined: 08-September 11

Reading input from the same line of executing

Posted 14 October 2013 - 07:50 PM

So I do Perl programming on Linux to learn the habit, but it's killing me because I don't know where to post my questions. Anyhow, I'm trying to read a user input, but from the same line of execution on the same line. ex.
user@service:~sampleprogram.pl userinput 

and so when I use this method:
my $input = <>;
chop ($input);


as the first line of my program that doesn't do the job and waits for the user to input again after in a new line, so any suggestion?

Is This A Good Question/Topic? 0
  • +

Replies To: Reading input from the same line of executing

#2 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Reading input from the same line of executing

Posted 15 October 2013 - 01:32 AM

View Postamture106, on 15 October 2013 - 03:50 AM, said:

So I do Perl programming on Linux to learn the habit, but it's killing me because I don't know where to post my questions.

I check for Perl questions here more-or-less daily, but it's mostly just me on this forum.

The primary Perl-specific forum is PerlMonks.org and you can learn a lot of Perl just by hanging out there and reading other people's questions. The other place I'd recommend is StackOverflow.com; it's a general programming Q&A site, but Perl is well-represented there. You can usually get answers pretty quickly on either site, but StackOverflow tends to be a bit faster (because it has a lot more users) and PerlMonks gets much more in-depth (because it's a discussion site full of people who truly love Perl). While you can cross-post questions to both sites, it's considered polite to include a note saying that you're doing so.

Quote

Anyhow, I'm trying to read a user input, but from the same line of execution on the same line. ex.
user@service:~sampleprogram.pl userinput 

In most *nix shells, this command will find the program sampleprogram.pl in your home directory and run it, passing the string "userinput" to it in ARGV (the list of command-line ARGument Values). In Perl, you can then either use @ARGV directly or use the "<>" operator to treat the contents of ARGV as a list of filenames which <> will open and return the contents of.

Either way, it does not read any input from the user.

Quote

and so when I use this method:
my $input = <>;
chop ($input);


as the first line of my program that doesn't do the job and waits for the user to input again after in a new line, so any suggestion?

This code would read one line of input from a file named in ARGV (or from STDIN (the STanDard INput stream) if ARGV is empty) and remove its last character. (You might want to use "chomp" instead of "chop" - "chomp" will only remove the last character if it's a linebreak; "chop" will remove it no matter what it is.) And that's all it does. It doesn't print the result, it doesn't read another line of input, nothing.

To properly identify the problem, can you provide the complete source of sampleprogram.pl (or, better, a condensed version of it which is just big enough to be runnable and to demonstrate the issue, without any extra stuff that's not directly relevant) along with some sample input and the expected output?
Was This Post Helpful? 0
  • +
  • -

#3 amture106  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 154
  • Joined: 08-September 11

Re: Reading input from the same line of executing

Posted 15 October 2013 - 02:12 AM

what I'm trying to so can be as simple as
user@server:~sample.pl text
my $input = <>;
chomp ($input);
print "$input"

but the most important is how to get the user to put input parameters in the same line in execution.
what I have above will require the be like:
user@server:~sample.pl
text

and the result going to be just text both times.
Was This Post Helpful? 0
  • +
  • -

#4 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Reading input from the same line of executing

Posted 16 October 2013 - 12:45 AM

Ah, OK. Now I get what you're trying to do.

The bit of Perl magic I mentioned earlier, where <> causes it to treat @ARGV as a list of filenames and reads from those files, is usually The Right Thing To Do, but, in this case, it isn't. Instead, to get the behavior you want, you'll need to look at @ARGV directly:

#!/usr/bin/env perl    

use strict;
use warnings;

if (@ARGV) {
  for my $item (@ARGV) {
    print "$item\n";
  }
} else {
  while (my $item = <STDIN>) {
    print $item;
  }
}


If this is run as ./argv foo bar baz, the if branch will print
foo
bar
baz


If it's run as just ./argv, @ARGV will be empty, so it falls through to the else branch and echoes whatever input is received on STDIN (which will normally be what the user types on the console, but could also be input from a file if you do something like ./argv < myfile.txt or the output of another command if you date | ./argv).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1