bofinken has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am more or less new to Perl, so my question is propably really bad, but I couldn't find a solution when I google, so: I want to get two arguments from the command line to my program and get that to two strings. Seems doable. But when I also want to get input within the program it doesn't work. What have I missed?
*************
($input1, $input2) = @ARGV; print "Save '$input1' and '$input2'\n"; print "and another string within the program!: "; chomp ($inputinside = <>);
********* This seemed like a good idea, but it not... apperently. Thanks in advance you wonderful person out there!

Replies are listed 'Best First'.
Re: mix arguments with normal input
by choroba (Cardinal) on Sep 27, 2018 at 21:43 UTC
    The diamond operator reads from STDIN only when there are no parameters to the script, otherwise it reads line by line the files specified as parameters one by one. That's the behaviour of the default magic ARGV filehandle.

    You have to options:

    1. Remove the two filenames from the array
      my ($input1, $input2) = splice @ARGV, 0, 2; # Make sure no arguments +are left.
    2. Specify that you don't want to read from the magic ARGV handle but standard input:
      $inputinside = <STDIN>;

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Another way to remove from @ARGV is

      my ($input1, $input2) = (shift,shift);

      I realize you know this but since this is a new to Perl type question I'll throw in this option.

        What?! No splice!? :P

        my ( $one, $two ) = splice @ARGV, 0, 2;
      Cheers! Pat yourself on your head. Wonderful. So easy, yet so very helpful. Enjoy life and thanks again!
Re: mix arguments with normal input
by hippo (Archbishop) on Sep 27, 2018 at 21:01 UTC

    Read explicitly from STDIN and you'll be fine:

    use strict; use warnings; my ($input1, $input2) = @ARGV; print "Save '$input1' and '$input2'\n"; print "and another string within the program!: "; my $inputinside = <STDIN>; print $inputinside;