[31969] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3233 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Dec 12 11:09:27 2010

Date: Sun, 12 Dec 2010 08:09:09 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sun, 12 Dec 2010     Volume: 11 Number: 3233

Today's topics:
    Re: FAQ 1.9 How does Perl compare with other languages  <brian.d.foy@gmail.com>
        perl code to automate launching a program and entering  <bennett@peacefire.org>
    Re: perl code to automate launching a program and enter <sherm.pendley@gmail.com>
    Re: perl code to automate launching a program and enter <jurgenex@hotmail.com>
        Regex to match a numerical IP range sln@netherlands.com
        Variable Question   Dec. 12, 2010 <edgrsprj@ix.netcom.com>
    Re: Variable Question   Dec. 12, 2010 <tadmc@seesig.invalid>
    Re: Variable Question   Dec. 12, 2010 <hjp-usenet2@hjp.at>
        Windows Question  Dec. 12, 2010 <edgrsprj@ix.netcom.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Fri, 10 Dec 2010 21:02:31 -0800
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: FAQ 1.9 How does Perl compare with other languages like Java, Python, REXX, Scheme, or Tcl?
Message-Id: <101220102102318019%brian.d.foy@gmail.com>

In article
<c52e5d2c-8b00-4182-932d-628679bb0e7d@g19g2000yqg.googlegroups.com>,
Marc Girod <marc.girod@gmail.com> wrote:

> On Dec 10, 5:00 pm, PerlFAQ Server <br...@theperlreview.com> wrote:
> 
> >     Some comparison documents can be found at
> >    http://www.perl.com/doc/FMTEYEWTK/versus/
> 
> I clicked on that link, and was redirected to: http://perldoc.perl.org/
> where FMTEYEWTK doesn't match anything in the search engine.

I guess i need to go through all of the perl.com links to see which
ones didn't survive the migration.

Thanks,


------------------------------

Date: Sat, 11 Dec 2010 06:24:59 -0800 (PST)
From: Bennett Haselton <bennett@peacefire.org>
Subject: perl code to automate launching a program and entering responses
Message-Id: <e6e4b484-28c2-41be-8b86-b2d3a5ba3489@o11g2000prf.googlegroups.com>

If I have a program called foo that does something like:

Hello world.  Should I continue? (y/n)
[user must type 'y' and hit Enter before continuing]
Thanks!

then how would I write a perl script that launches foo, waits until it
sees the line "Hello world.  Should I continue? (y/n)", and
automatically enters the "y" response, all while printing to stdout
the output that foo is printing and the responses that the perl script
is entering?  Actually, how can I do it without downloading and
installing any additional perl modules that don't come with the
standard perl distribution?  (Even if there is a specialized class
that does it more elegantly, can I just do it with normal reads and
writes?)

I know this must be simple, and I tried to figure it out myself, but I
couldn't find any phrase to Google for that would give me the answer
(e.g. "perl automate entering input", etc.)

Is it just two lines of code?  I know asking people to write code for
you is frowned upon, but in this case wouldn't it be faster than
typing a response in English sentences? :)


------------------------------

Date: Sat, 11 Dec 2010 10:36:13 -0500
From: Sherm Pendley <sherm.pendley@gmail.com>
Subject: Re: perl code to automate launching a program and entering responses
Message-Id: <m2sjy4uxsi.fsf@sherm.shermpendley.com>

Bennett Haselton <bennett@peacefire.org> writes:

> If I have a program called foo that does something like:
>
> Hello world.  Should I continue? (y/n)
> [user must type 'y' and hit Enter before continuing]
> Thanks!
>
> then how would I write a perl script that launches foo, waits until it
> sees the line "Hello world.  Should I continue? (y/n)", and
> automatically enters the "y" response, all while printing to stdout
> the output that foo is printing and the responses that the perl script
> is entering?

Have a look at the Expect module:

  <http://search.cpan.org/perldoc?Expect.pm>

> Is it just two lines of code?  I know asking people to write code for
> you is frowned upon

Not at all! I'm looking for paid work - email me and I'll be happy to
bid on your project.

sherm--

-- 
Sherm Pendley
                                   <http://camelbones.sourceforge.net>
Cocoa Developer


------------------------------

Date: Sat, 11 Dec 2010 08:20:57 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: perl code to automate launching a program and entering responses
Message-Id: <aq87g6p5m01atmfvpq4kdi881n2vkoramg@4ax.com>

Bennett Haselton <bennett@peacefire.org> wrote:
>If I have a program called foo that does something like:
>
>Hello world.  Should I continue? (y/n)
>[user must type 'y' and hit Enter before continuing]
>Thanks!
>
>then how would I write a perl script that launches foo, waits until it
>sees the line "Hello world.  Should I continue? (y/n)", and
>automatically enters the "y" response, all while printing to stdout
>the output that foo is printing and the responses that the perl script
>is entering? 

You are looking for the Expect module.

>Actually, how can I do it without downloading and
>installing any additional perl modules that don't come with the
>standard perl distribution? 

By re-implementing the code from Expect.pm

> (Even if there is a specialized class
>that does it more elegantly, can I just do it with normal reads and
>writes?)
>
>I know this must be simple, and I tried to figure it out myself, but I
>couldn't find any phrase to Google for that would give me the answer
>(e.g. "perl automate entering input", etc.)
>
>Is it just two lines of code? 

Basically you would have to redirect the input and output of the foo
program to be controlled by your program. Only if foo reads/writes
directly from the terminal/to the monitor instead of from/to
stdin/stdout, then it becomes difficult.

jue


------------------------------

Date: Sat, 11 Dec 2010 14:26:17 -0800
From: sln@netherlands.com
Subject: Regex to match a numerical IP range
Message-Id: <m9t7g6plb01d3f1ivfa5dfjmba9blg7ffu@4ax.com>

Somebody posted recently on perl.beginners this topic
(was: "Regex to match a numerical range")
The person was trying to match, using a regex, a range of IP's
like 127.0.0.[0-255] or something like that.
A bunch of posters replied with a textual solution.

The group is a list and I don't really have an email or know
how it works. I was going to reply with something like below.
It heavily uses eval, and has some moderate level regexs'.
The principle is that dec number becomes hex in \x{#} utf8 char.

Its workings are not at all that obvious and its pretty slow
comparitively, not only because of the evals' but because of
the runover past bytes, when it becomes utf8 characters in the
regexs'.

For example, a test case is to itterate a numeric range of
0-255, where for instance 255 is asumed to be a hex number
\x255 not a decimal. So a range of continuous decimal numbers
has a different output range used as hex numbers.
However, any check in the range of \x0 - \x255 utf8 characters
apparently works, where \x0 < \x127 < \x255, so it is deduced
that in decimal, 127 is greater than 0 and less than 255.

Inserting these as characters in a regex had me concerned for
a while. But, I tested it enough to be satisfied.

Here is an excerpt of the post from perl.beginners:
"   For a reason i don't understand:
    127.0.0.1 doesn't match as expected...
    Everything between 127.0.0.2 and 127.0.0.299 matches...
    127.0.0.230 doesn't match... 

    What am I doing wrong??
"
And there were many good replies.

-sln

# Regex IP Range matching
# where dec number becomes hex in \x{#} utf8 char
# ------------------------------------------------

use strict;
use warnings;


#### Test cases

 # testQuadAndPortRange();
 print "\n";

 my $pattern = makeUIpRegex('178. [10-45] .[180-200] . [223-254]: [190-195] ');
 print "Testing Ip range ...\npattern =\n$pattern\n\n";

 my ($count, $matched, $nomatch) = (0,0,0);

 for my $q2 (20 .. 22) {
    for my $q3 (0 .. 255) {
        for my $q4 (0 .. 255)
        {
            my $curip = "178 .$q2. $q3 .$q4 :$q3";
            if ( makeUIp( $curip ) =~ /$pattern/ ) {
                print "Matched! ($curip)\n";
                $matched++;
            }
            else {
               # print "No match! ($curip)\n";
               $nomatch++;
            }
            $count++;
         }
     }
 }
print <<EOM;

 Checked      $count
 Matched      $matched
 Not-matched  $nomatch
EOM

exit;


#### subs

 my ( $rx_Ip, $rx_IpRange );

 ## Constructs a utf8 char string from a decimal notation IP
 #  (where input = dec number, becomes hex in \x{#} utf8 char)
 #  Input ->  '#.#.#.# (optional-> : [#-#] )'
 
 sub makeUIp
 {
    my ($ip) = @_;

    BEGIN { $rx_Ip = qr/
      ^ \s*
        (\d{1,3}) \s* \. \s*   # q1 (1-3 digits)
        (\d{1,3}) \s* \. \s*   # q2  "
        (\d{1,3}) \s* \. \s*   # q3  "
        (\d{1,3}) \s*          # q4  "
        (?: : \s* (\d{1,5}) \s*)?  # optional port num (1-5 digits)
      $ /x;
    }
    if ($ip =~ / $rx_Ip /x ) {
        if (defined $5) {
            eval " \$ip = \"\\x{$1}\.\\x{$2}\.\\x{$3}\.\\x{$4} : \\x{$5}\" ";
        }         
        else {
            eval " \$ip = \"\\x{$1}\.\\x{$2}\.\\x{$3}\.\\x{$4}\" ";
        }
        if ($@) { warn $@; return ''; }
        return $ip;
    }
    # not the correct ip form
    return '';
 }

 ## Constructs a regex utf8 pattern from a decimal notation IP template
 #  (where input = dec number, becomes hex in \x{#} utf8 char)
 #  Input->  '#.#.#.#'  to  '[#-#].[#-#].[#-#].[#-#] : [#-#]'
 
 sub makeUIpRegex
 {
    my ($ip) = @_;
    my $res = '';

    BEGIN { $rx_IpRange = qr/
      ^ \s*
        (?: (\d{1,3}) | \[\s*(\d{1,3})\s* - \s*(\d{1,3})\s*\] ) \s*\.\s*
        (?: (\d{1,3}) | \[\s*(\d{1,3})\s* - \s*(\d{1,3})\s*\] ) \s*\.\s*
        (?: (\d{1,3}) | \[\s*(\d{1,3})\s* - \s*(\d{1,3})\s*\] ) \s*\.\s*
        (?: (\d{1,3}) | \[\s*(\d{1,3})\s* - \s*(\d{1,3})\s*\] ) \s*
        (?: 
           : \s* (?: (\d{1,5}) | \[\s*(\d{1,5})\s*-\s*(\d{1,5})\s*\] ) \s*
        )?
      $ /x;
    }
    if ($ip =~ / $rx_IpRange /x ) {
        if (defined $1)  { $res .= qq(\\x{$1}\\\\.) }
         else            { $res .= qq([\\x{$2}-\\x{$3}]\\\\.) }
        if (defined $4)  { $res .= qq(\\x{$4}\\\\.) }
         else            { $res .= qq([\\x{$5}-\\x{$6}]\\\\.) }
        if (defined $7)  { $res .= qq(\\x{$7}\\\\.) }
         else            { $res .= qq([\\x{$8}-\\x{$9}]\\\\.) }
        if (defined $10) { $res .= qq(\\x{$10}) }
         else            { $res .= qq([\\x{$11}-\\x{$12}]) }

        if (defined $13) {
            $res .= qq(\\\\ :\\\\ \\x{$13});
        }
        elsif (defined $14) {
            $res .= qq(\\\\ :\\\\ [\\x{$14}-\\x{$15}]);
        }
        eval "\$ip = \"$res\" ";
        if ($@) { warn $@; return ''; }
        return qr/$ip/x;
    }
    # not the correct form
    return '';
 }

 ## Constructs and runs utf8 regex /[$i-$i]/,
 #  (where $i = dec number, becomes hex in \x{$i} utf8 char)
 #  Tests for conflicts in character class syntax

 sub testQuadAndPortRange 
 {
    print "Testing quad and port range for conflicts ...\n";
    for my $i (0 .. 99999)
    {
        my ($rx,$src);
        eval " \$rx = '^'.\"\[\\x{$i}\\-\\x{$i}\]\".'\$' ";
        if ($@) { warn $@; next; }

        eval " \$src = \"\\x{$i}\" ";
        if ($@) { warn $@; next; }

        if ($src =~ / $rx /x) {
          # print "OK!  $i\n";
        }
        else {
            print "***** BAD $i $rx\n";
          # sleep (1);
        }	
    }
 }

__END__



------------------------------

Date: Sun, 12 Dec 2010 08:32:56 -0600
From: "E.D.G." <edgrsprj@ix.netcom.com>
Subject: Variable Question   Dec. 12, 2010
Message-Id: <4eadnbUuYKGZQJnQnZ2dnUVZ_smdnZ2d@earthlink.com>

Windows and ActiveState Perl are being used.

A Windows startup link file being used to start a Perl program running might 
have an internal command line that says:

Prog.pl Option-A
"Option-A" would be a word that is to be passed to the Perl program when it 
starts.


Q: Inside the Perl program, what code can be used to store that "Option-A" 
word in a variable such as $startupword so that it can be used in the 
program.

Q: If the Perl program is started through the use of a Windows batch file 
(.bat), could that same procedure be used?

I checked the FAQ files but could not find anything related to those topics.




------------------------------

Date: Sun, 12 Dec 2010 09:54:55 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Variable Question   Dec. 12, 2010
Message-Id: <slrnig9sqc.g2a.tadmc@tadbox.sbcglobal.net>

E.D.G. <edgrsprj@ix.netcom.com> wrote:


> Prog.pl Option-A
> "Option-A" would be a word that is to be passed to the Perl program when it 
> starts.
>
>
> Q: Inside the Perl program, what code can be used to store that "Option-A" 
> word in a variable such as $startupword so that it can be used in the 
> program.


    my $startupword = $ARGV[0];
or
    my $startupword = shift;


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


------------------------------

Date: Sun, 12 Dec 2010 16:59:20 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Variable Question   Dec. 12, 2010
Message-Id: <slrnig9sao.51j.hjp-usenet2@hrunkner.hjp.at>

On 2010-12-12 14:32, E.D.G. <edgrsprj@ix.netcom.com> wrote:
> Windows and ActiveState Perl are being used.
>
> A Windows startup link file being used to start a Perl program running might 
> have an internal command line that says:
>
> Prog.pl Option-A
> "Option-A" would be a word that is to be passed to the Perl program when it 
> starts.
>
>
> Q: Inside the Perl program, what code can be used to store that "Option-A" 
> word in a variable such as $startupword so that it can be used in the 
> program.

perldoc perlvar

Search for @ARGV

	hp


------------------------------

Date: Sun, 12 Dec 2010 08:43:30 -0600
From: "E.D.G." <edgrsprj@ix.netcom.com>
Subject: Windows Question  Dec. 12, 2010
Message-Id: <vpKdncyPz-_jQpnQnZ2dnUVZ_jCdnZ2d@earthlink.com>

Windows and ActiveState Perl are being used.

When a Perl program is running, what code can be used to determine if a 
Windows program is also running such as Notepad?

The FAQ and Perl commands were checked.  But I could not find the answer for 
that question.



------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 3233
***************************************


home help back first fref pref prev next nref lref last post