[25900] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 8128 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon May 30 06:05:29 2005

Date: Mon, 30 May 2005 03:05:06 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Mon, 30 May 2005     Volume: 10 Number: 8128

Today's topics:
        a simple question <raghavendra.ma@in.bosch.com>
    Re: a simple question <bernard.el-haginDODGE_THIS@lido-tech.net>
    Re: a simple question <nobull@mail.com>
        http auth logindialog box <hackeras@gmail.com>
    Re: http auth logindialog box <tadmc@augustmail.com>
    Re: http auth logindialog box <hackeras@gmail.com>
    Re: newbie Perl question <jurgenex@hotmail.com>
    Re: newbie Perl question (Anno Siegel)
    Re: newbie Perl question <nobull@mail.com>
    Re: Regex help: delete text only if not within quotatio <nobull@mail.com>
    Re: regexp replace problem (Kasper)
    Re: regexp replace problem <pilkowsk@informatik.uni-marburg.de>
    Re: Session retrieving data <apeiron+usenet@coitusmentis.info>
    Re: Session retrieving data <pilkowsk@informatik.uni-marburg.de>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Mon, 30 May 2005 15:02:56 +0530
From: "Raghavendra Mahuli" <raghavendra.ma@in.bosch.com>
Subject: a simple question
Message-Id: <d7emkc$r5n$1@ns2.fe.internet.bosch.com>

Hi all,
I have a simple question in perl... I have an array -

@a = (10,20,30,40);

Since '$' gives the scalar of a array , i expect "print $a"  to give the no.
of elements in the array...
But it dosent work.. However if i copy it to a scalar and then print it, it
works.. Can u pls explain why it is so?
Here is the program listing:

@a = (10,20,30,40);
$x= @a;
print $a;
print $x;

thanx in advance,
regards,
raghu




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

Date: Mon, 30 May 2005 11:44:22 +0200
From: "Bernard El-Hagin" <bernard.el-haginDODGE_THIS@lido-tech.net>
Subject: Re: a simple question
Message-Id: <Xns9666776BB300Belhber1lidotechnet@62.89.127.66>

"Raghavendra Mahuli" <raghavendra.ma@in.bosch.com> wrote:

> Hi all,
> I have a simple question in perl... I have an array -
> 
> @a = (10,20,30,40);
> 
> Since '$' gives the scalar of a array , i expect "print $a"  to
> give the no. of elements in the array...
> But it dosent work..


$a has nothing whatsoever to do with @a. They are completely different 
variables.



-- 
Cheers,
Bernard


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

Date: Mon, 30 May 2005 10:53:35 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: a simple question
Message-Id: <d7enr0$ml8$1@slavica.ukpost.com>



Raghavendra Mahuli wrote:

 > Subject: a simple question

Please put the subect of your post in the Subject of your post.
> Hi all,
> I have a simple question in perl... I have an array -
> 
> @a = (10,20,30,40);
> 
> Since '$' gives the scalar of a array ,

No it doesn't.  I'm not sure what you mean.  Do you mean $a is the same 
as scalar(@a) ? No, that's not true.

> i expect "print $a"  to give the no.
> of elements in the array...

No, @a and $a are two completely separate variables.

I have to confess this is rather messy in Perl5 - apparently it'll all 
be different in Perl6.

In Perl5 @a is the array called 'a' and $a is a separate scalar variable 
also called 'a'. Confusingly $a[0] is the first element of @a and has 
nothing to do with $a. $#a is the last subscript of @a.

This is all explained in some detail in the perldata manual.


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

Date: Mon, 30 May 2005 08:40:31 +0300
From: Nikos <hackeras@gmail.com>
Subject: http auth logindialog box
Message-Id: <d7e90e$dul$1@nic.grnet.gr>

Hello inside my games.pl script i want an http auth box to popup and ask 
for login credentials.
Only if its correct then games.pl will load else an eroor message popup.

So i made this after searching on the net and modifying some code:

#==========================CHECKING USERS 
CREDENTIALS===========================
my $browser = LWP::UserAgent->new;

$browser->credentials(
     'www.nikolas.tk:80',
     'Personal Folder',
     'nikos' => 'nikos'
);

my $url = 'http://www.nikolas.tk/cgi-bin/games.pl';
my $response = $browser->get($url);


die "Error: ", $response->header('Nikos Personal Folder') || 'Error 
accessing',
     "\n ", $response->status_line, "\n at $url\n Aborting"
     unless $response->is_success;

I dont know why, but a box an http auth box never appears so i can test 
this by entering nikos and nikos.

Whats am i doing wrong?


-- 
What is now proved was once only imagined!


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

Date: Mon, 30 May 2005 01:14:30 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: http auth logindialog box
Message-Id: <slrnd9lbq6.hsg.tadmc@magna.augustmail.com>

Nikos <hackeras@gmail.com> wrote:

> Hello inside my games.pl script i want an http auth box to popup and ask 
> for login credentials.


Then configure your web server so that it will do that for you.

If you don't know how to configure your web server, ask in a
newsgroup about webservers.


> my $browser = LWP::UserAgent->new;


LWP is the _client_ side of a WWW client-server exchange, so you are 
completly in left field trying to use it for your purpose above.

games.pl is the _server_ side of a WWW client-server exchange.


> Whats am i doing wrong?


Trying to do this without a proper mental model of how HTTP
and CGI work.


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


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

Date: Mon, 30 May 2005 09:21:32 +0300
From: Nikos <hackeras@gmail.com>
Subject: Re: http auth logindialog box
Message-Id: <d7ebdb$goq$1@nic.grnet.gr>

Tad McClellan wrote:
 ....{snip]

Well have you encounter something relevant in the past?
Perhaps if you did, could you paste some code to see how am i supposed 
to do what i want cause i am completely clueless.

I did it onw in PHP and was an easy atsk to do but in Perl although i 
googled didnt come up with something.


-- 
What is now proved was once only imagined!


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

Date: Mon, 30 May 2005 07:32:36 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: newbie Perl question
Message-Id: <okzme.6742$Vm4.5387@trnddc01>

[Please to not top-post, trying to repair]

Westcoast Sheri wrote:
>> It is no longer _my_ problem.
>>
>> *plonk*
>
> Would you repeat that, please?

Dear Westcoast

Your wish is my command:

***PLONK***

jue 




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

Date: 30 May 2005 08:35:02 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: newbie Perl question
Message-Id: <d7ej7m$6c$1@mamenchi.zrz.TU-Berlin.DE>

Westcoast Sheri  <westshestcostsheastsheri@westshestcostsheastsher.com> wrote in comp.lang.perl.misc:
> > Please put the subject of your post in the Subject of your post.
> > Be aware that the subject you have used will be read by mant people as
> > "Question I'm too lazy to figure out of myself. I'm too lazy to even
> > bother working out how what my question actually is".
> 
> That is their (your) problem!

Oh... a smartass.  So long.

Anno


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

Date: Mon, 30 May 2005 09:54:39 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: newbie Perl question
Message-Id: <d7ekcf$m1l$1@slavica.ukpost.com>



Jürgen Exner wrote:

> [Please to not top-post, trying to repair]
> 
> Westcoast Sheri wrote:
> 
>>>It is no longer _my_ problem.
>>>
>>>*plonk*
>>
>>Would you repeat that, please?
> 
> 
> Dear Westcoast
> 
> Your wish is my command:
> 
> ***PLONK***

Oh, the irony.  The OP lashes out at me for trying to help and Tad, 
Anno, Sinan and Jue all plonk[1] him!

And, of course, although I don't plonk I'll obviously be disinclined to 
be helpful towards the OP.  Gunnar also chimed in so that quite possibly 
goes for him too.

I wonder between the six of us what proportion of the quality answers we 
provide?  About half I'd hazzard.

[1] For those new to Usenet, plonking means to publically announce that 
one is configuring ones newsreader to ingore subsequent posts from a 
given poster.


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

Date: Mon, 30 May 2005 07:58:46 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Regex help: delete text only if not within quotation marks
Message-Id: <d7edj7$kus$1@slavica.ukpost.com>



Anno Siegel wrote:

> Brian McCauley  <nobull@mail.com> wrote in comp.lang.perl.misc:
> 
> [...]
> 
> 
>>However if I join it all together...
>>
>>   s/\G((?:[^"']*(["'])*[^\2]*\2)*[^"']*)(?:\/\*.*?\*\/|\*.*?)/$1/g;
>>
>>...it crashes the Perl compiler on this box (v5.8.4 built for 
>>MSWin32-x86-multi-thread).
> 
> 
> Same here (v5.8.6 built for darwin-2level).  It doesn't segfault when the 
> second appearance of \2 is wrapped in a character class:
> 
>     s/\G((?:[^"']*(["'])*[^\2]*[\2])*[^"']*)(?:\/\*.*?\*\/|\*.*?)/>$1</g;
> 
> That shouldn't change the semantics, but that's little comfort.

Maybe it's more comforting to get rid of the back reference:

     s/\G((?:[^"']*(?:"[^"]*"|'[^']*'))*[^"']*)(?:\/\*.*?\*\/|\*.*?;)/$1/g;

(Note: I've put back the missing semicolon that was mysteriously lost in 
my previous post).


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

Date: 30 May 2005 01:00:32 -0700
From: kcecek@gmail.com (Kasper)
Subject: Re: regexp replace problem
Message-Id: <5ee1860d.0505300000.1d6bc48b@posting.google.com>

Hi 
Thanks for reply but I think that my post wasn't enough precise.
I was thinking about only replace command.
It is quite easy when first element is const and it's known (aaa) 
then s/,/,aaa/ is enough.
I was thinking about gathering somehow the first element maybe like
this (?=^[^,]+) and use it in replace for ",".
Kasper


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

Date: Mon, 30 May 2005 10:24:15 +0200
From: Fabian Pilkowski <pilkowsk@informatik.uni-marburg.de>
Subject: Re: regexp replace problem
Message-Id: <3g00ruF9f60lU1@individual.net>

* Kasper schrieb:
> 
> Thanks for reply but I think that my post wasn't enough precise.
> I was thinking about only replace command.
> It is quite easy when first element is const and it's known (aaa) 
> then s/,/,aaa/ is enough.
> I was thinking about gathering somehow the first element maybe like
> this (?=^[^,]+) and use it in replace for ",".

Please execute those examples from Tad and me (they're almost the same)
to see what they do. They really do what you want. Assumedly, it is just
one point you miss: the absence of a *complex* regular expression. But
you don't need such one for this simple task, do you?

Be happy and do this one without a regex.

regards,
fabian


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

Date: 30 May 2005 04:23:33 GMT
From: Christopher Nehren <apeiron+usenet@coitusmentis.info>
Subject: Re: Session retrieving data
Message-Id: <slrnd9l5be.htm.apeiron+usenet@prophecy.dyndns.org>

On 2005-05-30, Alexandre Jaquet scribbled these
curious markings:
> Hi,
>
> I currently 've a trouble when I try to read stored session data.
>
> The $session->param("user_name") return an error can't call method param 
>   on an undefined value
>
> here is my code :

You get points for posting code and an error message (although a line
number for the error would have been appreciated), but see below.

> #!perl -w

No strict? And unless you're using an ancient version of perl (before
5.6.0 or so), "use warnings" is the preferred method of enabling
warnings.

> use CGI qw(:standard);

 ... and this but no taint checking? Ouch.

> use Switch;
> use Digest::MD5 qw(md5_hex);
> use CGI::Session qw/-ip-match/;
>
>
> my $query ;

It's generally better to declare a variable and immediately give it a
value, rather than distancing the declaration from the definition. Perl
is not like C where you have to start your functions with five-ten lines
of variable declarations. Thus:

my $query = new CGI;

>
> execute ();
>
>
> sub execute {
> $query = new CGI ;
>      my $action = $query->param('action');
>      if ($action) {
> 	switch ($action) {
> 	    case "login" {
> 		login();
> 		};
> 	    case "test" {
>                  test();
>                  };
>      }
>      }
> }

It's generally a good idea to handle unexpected CGI parameters
gracefully. If $action !~ /login|test/, it seems like you won't be
sending anything to the user whatsoever. I'm no expert on CGI, but I
imagine that the result won't be pretty.

> sub login {
>      $session = new CGI::Session ("driver:File", undef, {Directory => 
> 'C:/sessions' }) if (!$session);

Where's the error checking? The error message that you've described
above stems from the fact that for some reason or another,
CGI::Session's sub new (remember, in Perl, the "constructor" for an
object is a subroutine just like any other) returns undef. Maybe a typo
in your code, maybe some nefarious ancillary process moving things
around unknown to you. To quote Doug Linder: "A good programmer is
someone who looks both ways before crossing a one-way street." Wise
words indeed; some of the wisest I've read.

>      $session->param("user_name2", "alexj");
>      print "Content-type: text/html\n\n";
> }
>
>
> sub test {
>      my $test = $session->param("user_name2");
>      print "Content-type: text/html\n\n";
>      print "$test";
> }

Nitpickery: It seems that you're sending a content type of text/html,
yet you send plain text (unless the user_name2 CGI parameter is valid
HTML ... /me scratches head). Also, if you're doing CGI programming (and
are already using CGI and CGI::Session), why not just use the
appropriate methods in the CGI / CGI::Session classes for handling stuff
like content-type? They handle the double \n processing and all for you.
One typo and you can spend hours scratching your head and pulling out
your hair in search of a bug in every other part of your code. You'd be
correct in guessing that I speak from experience. It's a little silly,
IMO, to bring in large modules like CGI but not use their methods. If
you don't know they exist, that's okay. That's why CGI.pm probably has
the most POD documentation of any module in Perl's standard library.

One final thing: you don't need to double-quote a scalar if you're just
going to print the scalar itself. You're asking Perl to go through all
the hard work of scanning the double-quoted string for values that it
can interpolate and process, and then you only give it something that it
could print without any of that hard interpolation work. Go easy on
Perl; she's not as young as she used to be. :) ... and it's less typing
and thinking for you, too!

Best Regards,
Christopher Nehren
-- 
I abhor a system designed for the "user", if that word is a coded
pejorative meaning "stupid and unsophisticated". -- Ken Thompson
If you ask the wrong people questions, you get "Joel on Software".
Unix is user friendly. However, it isn't idiot friendly.


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

Date: Mon, 30 May 2005 10:01:59 +0200
From: Fabian Pilkowski <pilkowsk@informatik.uni-marburg.de>
Subject: Re: Session retrieving data
Message-Id: <3fvvicF9s5ovU1@individual.net>

* Alexandre Jaquet schrieb:
>> 
>> I currently 've a trouble when I try to read stored session data.
>> 
>> The $session->param("user_name") return an error can't call method param 
>>  on an undefined value
>> 
>> Any idea ?
>> 
>> here is my code :
>> 
>> #!perl -w
>> use CGI qw(:standard);
>> use Switch;
>> use Digest::MD5 qw(md5_hex);

Why do you use Digest::MD5?

>> 
>> use CGI::Session qw/-ip-match/;

Exemplary in using this -ip-match-switch. But not since you forget to
use strict. I know you have already subscribed to this newsgroup for a
longer time. Didn't you heard about using that always?

>> 
>> my $query ;

Yesterday I told you in <news:3fug48F9kn0oU1@individual.net> to not
declaring vars unless you want to assign them a value, didn't I? Change
this line to

    my $query = new CGI;
    
to make *you* (and your session handling) happy.

>> 
>> execute ();
>> 
>> sub execute {
>> $query = new CGI ;

As a result, this line should be deleted without replacement.

>>     my $action = $query->param('action');
>>     if ($action) {
>>     switch ($action) {
>>         case "login" {
>>         login();
>>         };
>>         case "test" {
>>                 test();
>>                 };
>>     }
>>     }
>> }

When your script is called with param "action=login" you assign a new
session object to $session (in login() below). But what happens in the
other case? Right, you simply call test() and do not initialize your
session object. Let's go fixing that.

>> 
>> sub login {
>>     $session = new CGI::Session ("driver:File", undef, {Directory => 
>> 'C:/sessions' }) if (!$session);

Since you're using strict (I hope you do it now) you have to declare all
your vars, e.g. with my(). Hence you have to write

    my $session = new CGI::Session( "driver:File", undef,
                                    {Directory => 'C:/sessions' } );

You see, there's no need to ask if $session is already true since we
declare it here. Before, $session doesn't exists, so it cannot be true.

>>     $session->param("user_name2", "alexj");
>>     print "Content-type: text/html\n\n";

This is not the HTTP header you want to print. You have to send the
session id back to the client. In case you want to use cookies, you have
to do something like

    print $session->header();

>> }
>> 
>> sub test {

Here you need to get a session object first. But if you do it the same
way as in login() you creates a new (and consequently empty) session
object. So, how to get the one created above? According to the docs you
have to set the second param (session id) of method new. Right, due to
calling $session->header() above you could easily use your CGI-object
$query to do this. CGI::Session is implicitly fetching the session id
from the CGI object for you.

    my $session = new CGI::Session( "driver:File", $query,
                                    {Directory => 'C:/sessions' } );

>>     my $test = $session->param("user_name2");
>>     print "Content-type: text/html\n\n";

Please avoid to print the HTTP header manually. Use always CGI::header()
instead, or in case you work with CGI::Session use its header() method
like above.

>>     print "$test";
>> }

> 
> finally I' do in test function a file open and then get stored values, 
> but I'm not sure if it's the best method

That's absolutely not the intent of CGI::Session. To work efficiently
with it you have to know much more about this technique. Please do not
continue unless you have read the documentation, esp. the tutorial
shipped with the module. Give it a shot!

    http://search.cpan.org/~sherzodr/CGI-Session-3.95/Session/Tutorial.pm

regards,
fabian


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

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.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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 8128
***************************************


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