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

I am required to come up with a Perl script that does LDAP lookups.The target system's Perl script will send the following name/value pairs via STDIN to my LDAP lookup script:

1. txtpatid= is the user ID number. 2. txtpatpw= is the user password. 3. txtpatserver= indicates the user's choice of "guest" or "user"

1. How do I make my script to accept these three (3) name/value pairs via STDIN? 2. How do I access these values from within my script? 3. How do I test such a call on Windows?

  • Comment on perl script that accepts parameters via STDIN

Replies are listed 'Best First'.
Re: perl script that accepts parameters via STDIN (updated)
by LanX (Saint) on Nov 29, 2016 at 13:14 UTC
    Welcome to the monastery!

    This will give you a start (from memory and untested)

    > 1. How do I make my script to accept these three (3) name/value pairs via STDIN?

    while (my $line = <STDIN> ) { chomp $line; ... }

    > 2. How do I access these values from within my script?

     %args = split /\s*=\s*/, $line;

    > 3. How do I test such a call on Windows?

    C:>echo a = 1 |perl -e " print split /=/, <STDIN>" a 1

    or

     C:>echo a = 1 |perl myScript.pl

    or (update)

    you can put the lines after __DATA__ at the of your code and replace <STDIN> with <DATA> while testing.

    while (my $line = <DATA> ) { ... __DATA__ a=1 b=2 c=3

    You've been a unclear about the exact format of your arguments * , depending on this it might be a bit more complicated.

    But we expect you to try and are happy to help if you give decent feedback ...

    HTH! :)

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

    *) like

    • All in one line?
    • Whitespace separated?
    • Vaules quoted?
    • Do you control the format?
    • ...
Re: perl script that accepts parameters via STDIN
by hippo (Archbishop) on Nov 29, 2016 at 14:23 UTC

    You could always abuse a module which already parses STDIN for key=value pairs:

    $ cat sin.pl #!/usr/bin/env perl use strict; use warnings; use CGI::Lite 3.00; my $cgi = CGI::Lite->new; my $params = $cgi->parse_form_data; $cgi->print_data; $ cat myin.txt txtpatid=foo txtpatpw=bar txtpatserver=guest $ ./sin.pl < myin.txt [ Reading query from standard input. Press ^D to stop! ] txtpatid = foo txtpatpw = bar txtpatserver = guest $

    This isn't necessarily recommended - just showing that it's possible. YMMV.

Re: perl script that accepts parameters via STDIN
by stevieb (Canon) on Nov 29, 2016 at 13:23 UTC

    Welcome to the Monastery, mbravismore!

    Normally, I'd skip such a question due to no effort shown whatsoever (please do so in the future), but I thought I'd at least show a basic skeleton that you can at least get started with. See the documentation of Getopt::Long to get an understanding of what is happening. You'll want/need to add in some parameter checking to ensure all required arguments are passed in.

    use strict; use warnings; use Getopt::Long; my %args; GetOptions ( 'i|txtpatid=s' => \$args{txtpatid}, 'p|txtpatpw=s' => \$args{txtpatpw}, 's|txtpatserver=s' => \$args{txtpatserver}, ); for (keys %args){ print "$_: $args{$_}\n"; }

    Run it:

    perl script.pl -i stevieb -p secret -s user

    Output:

    txtpatid: stevieb txtpatpw: secret txtpatserver: user
      I checked Getopt::Long before posting, but couldn't find a mention of parsing STDIN, like the OP wanted.

      How do you fix that?

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        Hi LanX,

        Getopt::Long has the functions GetOptionsFromArray and GetOptionsFromString - I only know this because a while back I hacked together a solution to use Getopt::Std on arbitrary strings...

        Regards,
        -- Hauke D

        I think I may have misinterpreted the question ;)