[21931] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4153 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Nov 21 03:10:40 2002

Date: Thu, 21 Nov 2002 00:10:14 -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           Thu, 21 Nov 2002     Volume: 10 Number: 4153

Today's topics:
        Perl one-liners in Win32 <dave_at_hm@hotmail.com>
    Re: Perl one-liners in Win32 (Tad McClellan)
    Re: Perl one-liners in Win32 <fixerdave@hot-NoSpamPlease-mail.com>
        proxy authentication !! <aagarwal@sbcglobal.net>
        proxy authentication <aagarwal@covad.net>
    Re: regex confusion <jurgenex@hotmail.com>
    Re: Regexp: Clearing captured value <bernard.el-hagin@DODGE_THISlido-tech.net>
    Re: Using Getopts in a case-insensitive manner <skuo@mtwhitney.nsc.com>
        visible progress during loop? <doris@chris.com>
    Re: visible progress during loop? <wksmith@optonline.net>
        Way to print caller() info from first to last instead o <miscellaneousemail@yahoo.com>
    Re: Way to print caller() info from first to last inste (Walter Roberson)
    Re: Way to print caller() info from first to last inste <miscellaneousemail@yahoo.com>
    Re: Where do I learn about local web servers? <dswinarsky@attbi.com>
        Why can't code print an @array?? <miscellaneousemail@yahoo.com>
    Re: Why can't code print an @array?? (Tad McClellan)
    Re: Why can't code print an @array?? (Jay Tilton)
    Re: Why can't code print an @array?? <dave@dave.org.uk>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 20 Nov 2002 20:00:28 -0800
From: Dave E <dave_at_hm@hotmail.com>
Subject: Perl one-liners in Win32
Message-Id: <3DDC5A5C.35E4102F@hotmail.com>

I'm looking for some example Perl one-liners that work on Win32
platforms.  Any replies appreciated - well, nice ones anyway ;)

In the way of background, I'm an administrator (NT to be specific)
that's done a reasonable amount of work in Perl - mostly admin stuff,
strangely enough.  I'm intrigued by the power of some Perl one-liners
I've seen but frustrated that most don't work in the Win32 environment.
As I learn best by twiddling with examples, I figured I'd ask for some.

Of course, I found this one:
    perl -ne "BEGIN{@ARGV=map{glob}@ARGV}/string_to_find/&&print\"$_\""
*.*
which searches for a string in the files specified by the wildcards -
very useful, but I still can't figure out how it loops through each line
of every file :)

    David...



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

Date: Wed, 20 Nov 2002 22:31:29 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Perl one-liners in Win32
Message-Id: <slrnatood1.3ds.tadmc@magna.augustmail.com>

Dave E <dave_at_hm@hotmail.com> wrote:

>     perl -ne "BEGIN{@ARGV=map{glob}@ARGV}/string_to_find/&&print\"$_\""

> I still can't figure out how it loops through each line
> of every file :)


Looping over the lines is done by the -n switch.

perl's command line switches are documented in:

   perldoc perlrun


The -n switch uses the <> diamond operator, so it will read
lines from all of the files named in @ARGV.

The map/glob puts file names into @ARGV.

The BEGIN block ensures that @ARGV will be loaded before
-n invokes its while-diamond loop.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Wed, 20 Nov 2002 21:52:57 -0800
From: Dave E <fixerdave@hot-NoSpamPlease-mail.com>
Subject: Re: Perl one-liners in Win32
Message-Id: <3DDC74B9.51B475A9@hot-NoSpamPlease-mail.com>

Yes, now I get it!  I knew about the -n or -p switches but something in my
head just didn't make the connection between the filelist in @ARGV and the
loop.  I guess the begin thing threw me off.

so, now:
    perl -e "BEGIN{@ARGV=map{glob}@ARGV}foreach (@ARGV) {print \"$_\n\"}" *.*

lets me work on the list of filenames and
    perl -ne "BEGIN{@ARGV=map{glob}@ARGV}print\"$_\"" *.*
lets me work on each line of the files.

I might get this yet :)

Thanks for the insight,

    David...

Tad McClellan wrote:

> Dave E <dave_at_hm@hotmail.com> wrote:
>
> >     perl -ne "BEGIN{@ARGV=map{glob}@ARGV}/string_to_find/&&print\"$_\""
>
> > I still can't figure out how it loops through each line
> > of every file :)
>
> Looping over the lines is done by the -n switch.
>
> perl's command line switches are documented in:
>
>    perldoc perlrun
>
> The -n switch uses the <> diamond operator, so it will read
> lines from all of the files named in @ARGV.
>
> The map/glob puts file names into @ARGV.
>
> The BEGIN block ensures that @ARGV will be loaded before
> -n invokes its while-diamond loop.
>
> --
>     Tad McClellan                          SGML consulting
>     tadmc@augustmail.com                   Perl programming
>     Fort Worth, Texas



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

Date: Wed, 20 Nov 2002 23:18:33 -0800
From: "Ashish A" <aagarwal@sbcglobal.net>
Subject: proxy authentication !!
Message-Id: <ari66o$ulc$1@news.laserlink.net>

Hello,
This is the program i m using to get out of my corp firewall but somehow it
does not work. We have firewall running which has authentication proxy.


If any one of you have any ideas about what is that i m doing wrong please
let me know. Any help on this topic will be highly appreciated.

use LWP::UserAgent;

 $ua = LWP::UserAgent->new;
 $ua->proxy(['http', 'ftp'] => 'http://proxy.corp.com');

 $req = HTTP::Request->new('GET',"http://www.google.com");
 $req->proxy_authorization_basic("proxy_user", "proxy_password");

 $res = $ua->request($req);
 print $res->content if $res->is_success;


Thanks
Ashish.




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

Date: Wed, 20 Nov 2002 23:38:00 -0800
From: "Ashish A" <aagarwal@covad.net>
Subject: proxy authentication
Message-Id: <ari692$un9$1@news.laserlink.net>

Hello,
This is the program i m using to get out of my corp firewall but somehow it
does not work. We have firewall running which has authentication proxy.


If any one of you have any ideas about what is that i m doing wrong please
let me know. Any help on this topic will be highly appreciated.

use LWP::UserAgent;

 $ua = LWP::UserAgent->new;
 $ua->proxy(['http', 'ftp'] => 'http://proxy.corp.com');

 $req = HTTP::Request->new('GET',"http://www.google.com");
 $req->proxy_authorization_basic("proxy_user", "proxy_password");

 $res = $ua->request($req);
 print $res->content if $res->is_success;


Thanks
Ashish.






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

Date: Thu, 21 Nov 2002 03:25:59 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: regex confusion
Message-Id: <bnYC9.12221$We5.3771@nwrddc04.gnilink.net>

Canucklehead wrote:
> Say I have a string containing
> "usr/local/home/someWebsite/foo/htdocs/pdr/xmlData/test/test4.xml" I
> would like to parse out everything except the ending file name (which
> could be any length of characters). In my head I know what needs to
> be done but translating that into perl is another thing. =)

The easiest way is to use
    File::Basename
and simply benefit from the efforts other people have invested already.
Of course that doesn't fullfill your requirement of using a RE, so I don't
know if this simple solution is appropriate for your problem.

jue




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

Date: Thu, 21 Nov 2002 06:11:03 +0000 (UTC)
From: Bernard El-Hagin <bernard.el-hagin@DODGE_THISlido-tech.net>
Subject: Re: Regexp: Clearing captured value
Message-Id: <arhtdn$1ah$1@korweta.task.gda.pl>

In article <4f3otu06p3r54l3cdcgvknpro8kgi75eud@4ax.com>, nibl wrote:
> On Wed, 20 Nov 2002 11:44:58 +0000 (UTC), Bernard El-Hagin
><bernard.el-hagin@DODGE_THISlido-tech.net> wrote:
> 
>>> $1 = "";
>>
>>Did you try that? Well, you should have.
> 
> No I will, but afaik those variables are read-only. Certainly cannot
> undef them, tried that.


My response was to the guy who suggested $1 = ""; as a solution to
your problem, not to you.


Cheers,
Bernard
--
echo 42|perl -pe '$#="Just another Perl hacker,"'


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

Date: Wed, 20 Nov 2002 18:09:58 -0800
From: Steven Kuo <skuo@mtwhitney.nsc.com>
Subject: Re: Using Getopts in a case-insensitive manner
Message-Id: <Pine.GSO.4.21.0211201805070.1955-100000@mtwhitney.nsc.com>

On Wed, 20 Nov 2002, John W. Krahn wrote:

> Okey Laboratory wrote:
> > 
> > I would like to accept cmd-line arguments using Getopts::Std,
> > which is passing the results into a hash.  The problem for me is,
> > I want to ensure that the user *can* enter arguments in either
> > case, but that they only enter an argument once.  How can I test
> > this for any number of flags?
> > ...
> > Is there an easy way to automate this, instead of using a very large
> > if-elsif ladder?
> > 
> > Any ideas/suggestions very much welcome!



Perhaps add:

> use warnings;
> use strict;
> use Getopts::Std;


my (%count,$duplicates);


> @ARGV = map { s/^(-.*)/\L$1/; $_ } @ARGV;
> # OR
> # @ARGV = grep [ s/^(-.*)/\L$1/ ], @ARGV;


change to:

@ARGV = map { s/^(-.*)/\L$1/; ++$count{lc($1)} if ($1); $_ } @ARGV;


> my %args;
> getopts( 'c:', \%args );
> 
> unless ( exists $args{'c'} ) {
>     die "A command-line argument is missing\n";
>     }
> 
> 
> John
> 



and then add:

while (my ($k,$v) = each %count) {
    if ($v > 1) {
        print "Duplicate entries for $k option\n";
        $duplicates = 'found';
    }
}

die "Blah" if ($duplicates);

-- 
Regards,
Steven



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

Date: Thu, 21 Nov 2002 03:31:47 GMT
From: D <doris@chris.com>
Subject: visible progress during loop?
Message-Id: <3DDC538A.2CB77A8D@chris.com>

Is it possible for Perl to print a "progress report," in my telnet
window while it's executing a very long loop?

To clarify, I have thousands of strings to process. My Perl code has
loops nesting about 4 levels deep. It is the innermost level that takes
the longest, because it is processing the thousands of strings.

Is there any way to have it print something to my screen like, "100
strings processed".... and then a few minutes later, "200 strings
processed"....and then a few minutes later, "300t strings processed"...
you get the idea.

Here's my rough draft (which obviously needs help):

$counter = 0;
print "starting code";
  if blah { do this
            if this { do that
                      if this { do stuff
                              $counter = $counter + 1;
                              if ($counter = 100) { print "100";
                              $counter = 0;
                              }
                      }
            }
  }
print "finished."

 ...But, here's the problem: I think all the code processes the strings
in memory, and then when it's all finished, appends results to a file.
Therefore, since it's all happening in memory, can I even GET it to
print to  my telnet screen?

Thanks




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

Date: Thu, 21 Nov 2002 05:14:49 GMT
From: "Bill Smith" <wksmith@optonline.net>
Subject: Re: visible progress during loop?
Message-Id: <dZZC9.1624$2v4.730@news4.srv.hcvlny.cv.net>


"D" <doris@chris.com> wrote in message news:3DDC538A.2CB77A8D@chris.com...
> Is it possible for Perl to print a "progress report," in my telnet

--snip--
>
> Here's my rough draft (which obviously needs help):
>
> $counter = 0;

$|  = 1;

> print "starting code";
>   if blah { do this
>             if this { do that
>                       if this { do stuff
>                              #### $counter = $counter + 1;
>                               ####if ($counter = 100) { print "100";

                                if (++$counter = 100) { print "100";

>                               $counter = 0;
>                               }
>                       }
>             }
>   }
> print "finished."
>
> ...But, here's the problem: I think all the code processes the strings
> in memory, and then when it's all finished, appends results to a file.
> Therefore, since it's all happening in memory, can I even GET it to
> print to  my telnet screen?
>
> Thanks
>
>


OUTPUT_AUTOFLUSH ($|) must be set to true or you probably
will not get the output until the program finishes.
refer:  perldoc perlvar.

Your counter logic should work, I suggested a minor simplification.

Bill






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

Date: Thu, 21 Nov 2002 04:56:05 GMT
From: Carlos C. Gonzalez <miscellaneousemail@yahoo.com>
Subject: Way to print caller() info from first to last instead of last to first?
Message-Id: <MPG.18461527c64df6479898e3@news.edmonton.telusplanet.net>

Hi everyone,  

Normally caller() can be called with a number indicating how far back in 
the call trace one wants to acquire information for.  As in the following 
snippet of code...

my $i = 1; # The previous call - NOT the very first one.
while (@a = caller($i++))
{
  my ($package,$file,$line,$sub) = @a;
  printf "%s $s $d $s\n", $package, $file, $line, $sub;
}

The first call might have been 4 functions back. 

That's all well and good but the last line of info printed out 
corresponds to the first call.  Meaning that one must traverse one's way 
back up the output to find the info for the last call.  

Is there some way to find out how far back in the call trace caller() can 
return info for?  Such that one could output the call trace from the 
first to the last call?  Resulting in the last call showing up at the 
bottom of the output?  

Kinda like so....

my $i = [how far back in the trace stack the first call is];
while (@a = caller($i--))
{
  my ($package,$file,$line,$sub) = @a;
  printf "%s $s $d $s\n", $package, $file, $line, $sub;
}

Any suggestions would be appreciated. I thought of using the reverse 
function on the @a array but I think the resulting array would be a mess 
to try and figure out which value goes with what call. 

Thanks. 

-- 
Carlos 
www.internetsuccess.ca/best_web_hosting.html


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

Date: 21 Nov 2002 05:32:59 GMT
From: roberson@ibd.nrc.ca (Walter Roberson)
Subject: Re: Way to print caller() info from first to last instead of last to first?
Message-Id: <arhr6b$7fh$1@canopus.cc.umanitoba.ca>

In article <MPG.18461527c64df6479898e3@news.edmonton.telusplanet.net>,
Carlos C. Gonzalez  <miscellaneousemail@yahoo.com> wrote:
:Normally caller() can be called with a number indicating how far back in 
:the call trace one wants to acquire information for.

:Is there some way to find out how far back in the call trace caller() can 
:return info for?  Such that one could output the call trace from the 
:first to the last call?  Resulting in the last call showing up at the 
:bottom of the output?  

IANAPH (I Am Not A Perl Hacker), but when I look through the
5.6.1 code in pp_ctl.c, it looks to me as the answer is No. 
The code in pp_caller has to search for the right context, largely
because of debugger contexts it appears.

If you wanted a quick approximation, and don't mind
writing a small extension, then it appears that you could
use the internal perl variable cxstack_ix; if I read the code
correctly (and I likely don't), it would be the depth *including*
the debugger frames. You could thus grab that value and start
asking from there, understanding that you are going to get undef
until you decrement far enough to account for all of the debugger
frames.
--
   I've been working on a kernel
   All the livelong night.
   I've been working on a kernel
   And it still won't work quite right.      -- J. Benson & J. Doll


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

Date: Thu, 21 Nov 2002 05:53:48 GMT
From: Carlos C. Gonzalez <miscellaneousemail@yahoo.com>
Subject: Re: Way to print caller() info from first to last instead of last to first?
Message-Id: <MPG.184622cc91a937ea9898e5@news.edmonton.telusplanet.net>

In article <arhr6b$7fh$1@canopus.cc.umanitoba.ca>, roberson@ibd.nrc.ca 
says...

Hi Walter, 

> :Is there some way to find out how far back in the call trace caller() can 
> :return info for?  Such that one could output the call trace from the 
> :first to the last call?  Resulting in the last call showing up at the 
> :bottom of the output?  
> 
> IANAPH (I Am Not A Perl Hacker), but when I look through the
> 5.6.1 code in pp_ctl.c, it looks to me as the answer is No. 
[ some nipped ]

Thanks very much for your input Walter.  That's too bad.  As I was 
thinking about what you said it dawned on me that it might be possible to 
create an array of arrays.  Storing the return values from a call to 
caller() into each of the second tier arrays.  Then reversing the order 
of the first tier array (whose elements point to the second tier arrays)  
using reverse().  

I don't quite know how to code that but I will play around with it some 
and see what I can come up with. Sometimes my error files where I log 
stuff gets jammed full of various types of output and it would be handy 
to see the last call, last in the output.  Instead of having to read back 
through a bunch of output to find the last call somewhere inside it.  

> --
>    I've been working on a kernel
>    All the livelong night.
>    I've been working on a kernel
>    And it still won't work quite right.      -- J. Benson & J. Doll

Cute! :-)

-- 
Carlos 
www.internetsuccess.ca/best_web_hosting.html


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

Date: Thu, 21 Nov 2002 05:37:01 GMT
From: "Derrick Swinarsky" <dswinarsky@attbi.com>
Subject: Re: Where do I learn about local web servers?
Message-Id: <_h_C9.69616$%m4.32427@rwcrnsc52.ops.asp.att.net>

Go to http://popfile.sourceforge.net/ and look at the code for popfile.pl.
You will notice that the code starts a webserver and feeds web pages back.
This will probably be a good place to look for a perl webserver...?

Good luck,
Derrick






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

Date: Thu, 21 Nov 2002 05:37:52 GMT
From: Carlos C. Gonzalez <miscellaneousemail@yahoo.com>
Subject: Why can't code print an @array??
Message-Id: <MPG.18461eeb2df25e629898e4@news.edmonton.telusplanet.net>

Hi everyone,  

This must be back to basics week in Perl for me.  I have been reworking 
this code for close to an hour trying to understand how to work with an 
array expression asignment inside a while loop and just don't get it.  

Why can I comment out line 1 in the code below and have it work great but 
when I comment out the other two lines in the loop and try to just print 
"@stack\n" I get a bunch of uninitialized errors?  Doing a print join("", 
@stack) doesn't help.

Does anyone have any insight they could pass along?  

CODE BEGIN 
#!/usr/bin/perl -W
use strict;
c();
sub c
{
  my $i;
  my @stack;
  my ($pack,$file,$line,$sub);

  while (@stack = caller($i++))
  {
# line 1: print "@stack\n";
    ($pack,$file,$line,$sub) = @stack;
    printf "%s %s %d %s\n", $pack, $file, $line, $sub;
  }
}
CODE END

I know that an array can be printed out by a print statement like so...

my @array = ("one", "two", "three");
print "@array\n";

So why doesn't the above print "@stack\n" work the same way???

Thanks.  

-- 
Carlos 
www.internetsuccess.ca/best_web_hosting.html


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

Date: Thu, 21 Nov 2002 00:22:58 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Why can't code print an @array??
Message-Id: <slrnatouu2.1l5.tadmc@magna.augustmail.com>

Carlos C. Gonzalez <miscellaneousemail@yahoo.com> wrote:

> Why can I comment out line 1 in the code below and have it work great but 
> when I comment out the other two lines in the loop and try to just print 
> "@stack\n" I get a bunch of uninitialized errors?

> Does anyone have any insight they could pass along?  


The same insight I passed on earlier today...


>   {
> # line 1: print "@stack\n";


Try this one instead:

    print "@stack[0..3]\n";

Viola! No warnings.


Wanna know which elements are undef? Just ask perl:

   foreach ( 0..$#stack ) {
      print "index $_ is uninitialized\n" unless defined $stack[$_];
   }


> So why doesn't the above print "@stack\n" work the same way???


It does work the same way.

What makes you think it doesn't?


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Thu, 21 Nov 2002 06:49:21 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Why can't code print an @array??
Message-Id: <3ddc7b57.351406670@news.erols.com>

Carlos C. Gonzalez <miscellaneousemail@yahoo.com> wrote:

: Why can I comment out line 1 in the code below and have it work great but 
: when I comment out the other two lines in the loop and try to just print 
: "@stack\n" I get a bunch of uninitialized errors?  Doing a print join("", 
: @stack) doesn't help.
:
: CODE BEGIN 
: #!/usr/bin/perl -W
: use strict;
: c();
: sub c
: {
:   my $i;
:   my @stack;
:   my ($pack,$file,$line,$sub);
: 
:   while (@stack = caller($i++))
:   {
: # line 1: print "@stack\n";
:     ($pack,$file,$line,$sub) = @stack;
:     printf "%s %s %d %s\n", $pack, $file, $line, $sub;
:   }
: }
: CODE END

caller() in list context returns more values than just the leading four
that you are interested in, and some of those values are undefined.  
Simple as that.

For testing, you can make the undefs evident.

    use Data::Dumper;
    while (my @stack = caller($i++)) {
        print Dumper(\@stack);
    }

You can crop the junk you don't care about with a list slice.

    sub c {
        my $i;
        while (my @stack = (caller($i++))[0..3] ) {
            print "@stack\n";
        }
    }



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

Date: Thu, 21 Nov 2002 07:34:00 +0000
From: "Dave Cross" <dave@dave.org.uk>
Subject: Re: Why can't code print an @array??
Message-Id: <pan.2002.11.21.07.33.59.86444@dave.org.uk>

On Thu, 21 Nov 2002 05:37:52 +0000, Carlos C. Gonzalez wrote:

>   while (@stack = caller($i++))

Later on you only use the first four items from this array. As others have
pointed out, later items can be undef. So why not just take the items that
you are interested in.

while (@stack = (caller($i++))[0 .. 3])

hth,

Dave...

-- 
  ...she opened strange doors that we'd never close again



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

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:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

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

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

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 V10 Issue 4153
***************************************


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