dsherohman's Profile User Rating: -----

Reputation: 226 Stalwart
Active Posts:
657 (0.28 per day)
29-March 09
Profile Views:
Last Active:
User is offline Jun 03 2014 02:27 AM

Previous Fields

OS Preference:
Favorite Browser:
Favorite Processor:
Who Cares
Favorite Gaming Platform:
Your Car:
Who Cares
Dream Kudos:
Expert In:

Latest Visitors

Icon   dsherohman has not set their status

Posts I've Made

  1. In Topic: Anyone familiar with Perl? (Grammar Rules)

    Posted 2 Nov 2013

    If you haven't already done so, I would recommend asking on http://www.perlmonks.org/ Grammars are a new feature in Perl 6, which I haven't personally used, but there are a lot of heavy Perl 6 users on PerlMonks, including some of the developers. I'm sure they can help you.
  2. In Topic: Reading input from the same line of executing

    Posted 16 Oct 2013

    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

    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).
  3. In Topic: Reading input from the same line of executing

    Posted 15 Oct 2013

    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.


    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.


    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?
  4. In Topic: Extracting information from an array

    Posted 15 Oct 2013

    Again, copying and pasting your posted code (and changing line 8 to set $input to a valid username instead of the string "username"), it works perfectly when I run it, printing out a list of login sessions for the user whose name is in $input.

    So I can only repeat my earlier question: In what way does it not work for you? What output do you get and how does that differ from the output that you want/expect?

    As a general debugging step, you might also want to try printing the contents of @all and $input to verify that they contain what you expect them to.
  5. In Topic: Extracting information from an array

    Posted 14 Oct 2013

    I can only suspect that the code you posted is not the same as the code you ran. I copied and pasted the posted code directly into a file and it ran perfectly with no modifications, producing the output:
    jdoe pt90 102.325.58.20jdoe pt92 102.325.58.20jdoe pt91 102.325.58.20

    In what way does it not work for you?

    A couple tips, though:

    1) If your actual program doesn't already use strict; and use warnings;, I would strongly advise adding them. They're the two best Perl debugging tools around. (Although, in this particular case, neither is relevant since your posted code runs cleanly with both in place. Just mentioning it as general good practice.)

    2) The first foreach loop is unnecessary. It causes your program to run the grep once for each item in @all, but grep examines the entire list each time, so you only need to run it once, period.

    3) When you print out the results, it might be easier to read if you added a \n after each item, so that they'll appear on separate lines rather than being all run together. Alternately, if you use 5.010; (and are using perl 5.10 or later), you can replace "print" with "say" and get the newlines automatically.

    Applying all those tips, and making use of a couple extra Perlisms (postfix flow controls and implicit use of $_), your program transforms into:

    #!/usr/bin/env perl    
    use strict;
    use warnings;
    use 5.010;
    my @all = ("jdoe pt90 102.325.58.20", "jdoe pt92 102.325.58.20", 
               "jdoe pt91 102.325.58.20", "jsmith pt93 102.325.58.20");
    my $input = "jdoe"; # user input
    # matching and copying matched elements
    my @selected =  grep (/\b^$input\b/, @all) ;
    say for @selected;

    Which is exactly equivalent to your posted code, aside from running a little faster (since it only does the grep once) and adding newlines after each item it prints out:
    jdoe pt90 102.325.58.20
    jdoe pt92 102.325.58.20
    jdoe pt91 102.325.58.20

My Information

Member Title:
Perl Parson
44 years old
December 1, 1970
Lund, Sweden
Forum Leader:
Full Name:
Dave Sherohman
Years Programming:
Programming Languages:
Perl, C, SQL, JavaScript/AJAX, Borland Delphi/Object Pascal, PHP, bash, HTML, CSS

Contact Information

Click here to e-mail me
Website URL:
Website URL  http://code.sherohman.org/


dsherohman hasn't added any friends yet.


Page 1 of 1
  1. Photo

    newclearner Icon

    23 Dec 2010 - 03:12
    Congrats!! :)
  2. Photo

    Alex6788 Icon

    15 Dec 2010 - 17:47
    Congratulations on the promotion to expert!
Page 1 of 1