[21890] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4094 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Nov 10 14:06:29 2002

Date: Sun, 10 Nov 2002 11:05:08 -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, 10 Nov 2002     Volume: 10 Number: 4094

Today's topics:
    Re: Can you Simplify My Code? <admin@-NOSPAM-2host.com>
    Re: Can you Simplify My Code? <flavell@mail.cern.ch>
    Re: Can you Simplify My Code? (Tad McClellan)
        Convert form input by using a hash (J.C.Castro)
    Re: Editing Perl scripts in windows environment and run (Tad McClellan)
    Re: Editing Perl scripts in windows environment and run <tk@WINDOZEdigiserv.net>
    Re: Editing Perl scripts in windows environment and run <flavell@mail.cern.ch>
        hash references (Hans)
    Re: hash references <tassilo.parseval@post.rwth-aachen.de>
    Re: hash references (Mark Jason Dominus)
    Re: hash references (Tad McClellan)
    Re: if condition <admin@-NOSPAM-2host.com>
        looping through named arrays (p cooper)
    Re: looping through named arrays <tassilo.parseval@post.rwth-aachen.de>
    Re: looping through named arrays <dave@dave.org.uk>
    Re: newbie LWP redirect/cookie question (Ramana Mokkapati)
    Re: Why not PERL? (tî'pô)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sun, 10 Nov 2002 06:39:21 -0800
From: "2Host.com - Robert" <admin@-NOSPAM-2host.com>
Subject: Re: Can you Simplify My Code?
Message-Id: <3DCE6F99.4991299@-NOSPAM-2host.com>



yisraelharris wrote:
> 
> The following function strips away trailing blanks. What I'm wondering
> is whether it can be simplified?
> 
> sub trimBlanks { my $string = $_[0]; $string =~ s/ *$//; return
> $string; }
> 
> The direction I'm looking for is a one-line statement that says: return
> the parameter sent, only substitute all trailing blanks with nothing. I
> am new to Perl and am wondering if its syntax allows this sort of thing.
> 
> Thanks.
> 

my $string = 'Why bother with all the work in a subroutine?....   ';

my ($result) = trimBlanks($string);

sub trimBlanks { return $_=shift,s/ +$//; }

print "Was: $string\n";
print "Now: $result\n";

It _does_ work. :-)
-- 
Regards,
Robert McGregor - Email: admin@(remove)2host.com. Phone: 530-941-0690
Server admin, support & programing for shared & dedicated web servers
Secure, reliable hosting you expect and deserve! http://www.2host.com


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

Date: Sun, 10 Nov 2002 15:17:11 +0100
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: Can you Simplify My Code?
Message-Id: <Pine.LNX.4.40.0211101515590.27125-100000@lxplus073.cern.ch>

On Nov 10, yisraelharris posted nothing more than:

> 2 valiant efforts, and still no responses to my question!

I don't think you comprehend this usenet idea.  bye.



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

Date: Sun, 10 Nov 2002 10:19:44 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Can you Simplify My Code?
Message-Id: <slrnast1p0.bs6.tadmc@magna.augustmail.com>

yisraelharris <member@mainframeforum.com> wrote:

> still no responses to my question!


I see that there *were* responses to your question, so
I don't know what you're talking about.

Don't blame your poor newsfeed on the newsgroup.

You can go to groups.google.com to see the responses that
you (should have) got.


See also:

   http://perl.plover.com/Questions.html


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


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

Date: 10 Nov 2002 08:45:14 -0800
From: jccastro@osite.com.br (J.C.Castro)
Subject: Convert form input by using a hash
Message-Id: <9b76f2c0.0211100845.5769a37f@posting.google.com>

The script below is an example of what I want to do, although I will
not work with month names. But it has something wrong.
_______________________________________________________________

#!/usr/bin/perl

&hent_input;
&conversion;

%longMonthName = ("Jan", "January",
    "Feb", "February",
    "Mar", "March",
    "Apr", "April",
    "May", "May",
    "Jun", "June",
    "Jul", "July",
    "Aug", "August",
    "Sep", "September",
    "Oct", "October",
    "Nov", "November",
    "Dec", "December");

$test = $input{n};

sub hent_input {

	if ( $ENV{'REQUEST_METHOD'} eq "GET")  {
		$input = $ENV{'QUERY_STRING'};
	} elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
		read(STDIN, $input, $ENV{'CONTENT_LENGTH'});
	}

	@input = split (/&/, $input);
	foreach (@input) {
		s/\+/" "/g;
		($name, $value) = split (/=/,$_); 

		$name =~s/%(..)/pack("c",hex($1))/ge; 
		$value =~s/%(..)/pack("c",hex($1))/ge; 
		$input{$name} .= "\0" if (defined($input{$name}));
		$input{$name} .= $value;
	}
}

sub conversion {
print "Content-type: text/html\n\n";
print "$longMonthName{$test}\n";
}
________________________________________________________________

I know that this would work:
$test = 'Nov';
print "$longMonthName{$test}\n";

And this also would work:
print "$input{n}\n";

But I don't know how to use $longMonthName{$test} when $test =
$input{n}

Thanks.


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

Date: Sun, 10 Nov 2002 07:51:49 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Editing Perl scripts in windows environment and running on linux
Message-Id: <slrnassp3l.blj.tadmc@magna.augustmail.com>

Peter Manderino <peter@manderino.net> wrote:

> I'm assuming that the
> problem exists somewhere in the line feeds.  


That is almost for sure the cause of your problem.


> Does anyone know a
> program that I can use in windows to be able to edit an existing Perl
> script to be run on a linux server??  


A crude 'dos2unix':

   perl -p -e "tr/\r//d" prog.dos >prog.linux


> Is there another way to address
> the problem? 


Transfer the files using FTP "ASCII" or "text" mode rather
than "binary" mode.


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


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

Date: Sun, 10 Nov 2002 14:42:37 GMT
From: tk <tk@WINDOZEdigiserv.net>
Subject: Re: Editing Perl scripts in windows environment and running on linux
Message-Id: <13sssuo81pnfgm515k517djlr55l0tq3a6@4ax.com>

In a fit of excitement on 9 Nov 2002 16:59:55 -0800, peter@manderino.net
(Peter Manderino) managed to scribble:

| Does anyone know a program that I can use in windows to be able to 
| edit an existing Perl script to be run on a linux server??


Try google for 'UEdit32' (UltraEdit32). You can swap between
DOS/UNIX/Mac file formats.


Regards,

  tk

--
+--------------------------+
|     digiServ Network     |
|      Web solutions       |
| http://www.digiserv.net/ |
+--------------------------+

  Remove WINDOZE to reply


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

Date: Sun, 10 Nov 2002 16:18:14 +0100
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: Editing Perl scripts in windows environment and running on linux
Message-Id: <Pine.LNX.4.40.0211101604140.27125-100000@lxplus073.cern.ch>

On Nov 9, Peter Manderino inscribed on the eternal scroll:

>  The problem that I am having is that it
> doesn't function when I download a working script from my site and
> edit it in windows (I've tried many apps),

I don't think there's any great shortage of plaintext editors for
Windows which can support unix newline conventions.

I was already a happy user of PFE32 (programmers file editor) before
it went unsupported.  It still works as well today as it did then, and
I'm not proposing to change; but I concede that it's not the only
choice, and you'd want to take into account the loss of support by its
original author in making your decision.

> then upload it again.

If you use FTP with Windows-style text files, then you should use text
(so-called ASCII) transfer mode to get the newlines converted.  But
note that FTP is insecure and is being increasingly blocked by
sysadmins, and (for reasons that are beyond me?) the secure
replacement called sftp doesn't support the distinction between binary
and text modes (so it's really more like scp with a user dialog on
top, IMHO).  This means that you really do still need some kind of
free-standing "dos-to-unix" utility.

Beware: some DOS-to-unix utilities do exactly what it says on the tin:
they also do their best to convert your 8-bit characters between DOS
coding (cp-437 or cp-850 as the case may be) and iso-8859-1 at the
same time as mapping the newlines.  If your 8-bit characters are
really Windows characters (i.e in windows-1252 coding) then that
wouldn't be such a neat trick, but in order to befuddle the users, the
real Windows-to-unix conversion utilities _also_ seem to call
themselves dos-to-unix (dos2unix etc.).

Tad's one-liner is fine, as long as you run it on the intended
platform ;-)

cheers



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

Date: 10 Nov 2002 08:38:20 -0800
From: baga@gmx.de (Hans)
Subject: hash references
Message-Id: <82c43464.0211100838.22fba1cb@posting.google.com>

Hello,

I want to put two hash references together, like

$hashref1 = { key_1=> value_1,
              key_2=> value_2,
              ...
             }

$hashref2 = { key_a=> value_a,
              key_b=> value_b,
              ...
             }

to like 
$hashref  = { key_1=> value_1,
              key_2=> value_2,
              ....
              key_a=> value_a,
              key_b=> value_b,
            }

(the keys are unique, so there is no problem)

Has someone an idea how to do that?


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

Date: 10 Nov 2002 16:56:40 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de>
Subject: Re: hash references
Message-Id: <aqm348$ocg$1@nets3.rz.RWTH-Aachen.DE>

Also sprach Hans:

> I want to put two hash references together, like
> 
> $hashref1 = { key_1=> value_1,
>               key_2=> value_2,
>               ...
>              }
> 
> $hashref2 = { key_a=> value_a,
>               key_b=> value_b,
>               ...
>              }
> 
> to like 
> $hashref  = { key_1=> value_1,
>               key_2=> value_2,
>               ....
>               key_a=> value_a,
>               key_b=> value_b,
>             }
> 
> (the keys are unique, so there is no problem)

Use a hash-slice:

    @$hashref{keys %$hashref1, keys %$hashref2} =
        values %$hashref1, values %$hashref2;

See 'perldoc perldata'.

Tassilo
-- 
$_=q!",}])(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus;})(rekcah{lrePbus;})(lreP{rehtonabus;})(rehtona{tsuJbus!;
$_=reverse;s/sub/(reverse"bus").chr(32)/xge;tr~\n~~d;eval;


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

Date: Sun, 10 Nov 2002 17:17:45 +0000 (UTC)
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: hash references
Message-Id: <aqm4bp$58k$1@plover.com>

In article <82c43464.0211100838.22fba1cb@posting.google.com>,
Hans <baga@gmx.de> wrote:
>Hello,
>
>I want to put two hash references together...

        $hashref = {%$hashref1, %$hashref2};



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

Date: Sun, 10 Nov 2002 11:34:10 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: hash references
Message-Id: <slrnast64i.c62.tadmc@magna.augustmail.com>

Hans <baga@gmx.de> wrote:
> 
> I want to put two hash references together, like

> Has someone an idea how to do that?


   my $hashref  = { %$hashref1, %$hashref2 };


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


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

Date: Sun, 10 Nov 2002 06:54:18 -0800
From: "2Host.com - Robert" <admin@-NOSPAM-2host.com>
Subject: Re: if condition
Message-Id: <3DCE731A.5B6961A3@-NOSPAM-2host.com>



magda muskala wrote:
> 
> hi,
> 
> first of all -
> much thanx for juergen for helping me.
> anyway, i am sorry for my last mail (it was misunderstood - i was
> going to send it to german group).
> 
> i got one more question.
> it is a part of cgi script.
> open (P, "bio.txt") or die "Cannot open bio.txt: $!\n";
> while (<P>)
> {
>         if ($_ =~ /^$uid/)
> 
>         {
>                 $ok = 1;
                  ^^^^^^^

You might want to do a last; once you get a successful match, or does
that not work for what you want?

>         }
> }
> close (P);
> if ($ok == 1)
> {
>         open (I, "<bio.txt") or die "Cannot open: 'bio.txt' $!\n";
>         open (O, ">tmp.txt") or die "Cannot open 'tmp.txt' $!\n";
>         while (<I>)
>         {
>                 print O unless (/^$benutzer/);
>         }
> 
>         close(I); close(O);
>         rename("tmp.txt","bio.txt");
> 
> }
> else
> {
>         print "This UID doesn't exist!";
>         print "<br><br><br><a href=\"http://\">Zurueck<br></a>\n";
> 
> }
> 
> if i run it from the page, it works fine.
> but if i run it from the console, the whole content of bio.txt is deleted.
> 
> shouldn't it jump to else {}? if i run it from console the $uid
> variable doesn't (it is read as query string)

Where/how are you getting the $uid variable? Is it being reset to a non
1 value before it is checked again?

It sounds to me, that the $uid variable isn't set, so $_ =~ /^$uid/ is
matching "" (nothing). /^/ and that will match each time. You are
therefore reading in no $uid, meaning there's no content, meaning it'll
write a blank file. I'm just guessing. I might have missed something
obvious. Also you might need to provide a little more of the code (only
the relevant parts).
-- 
Regards,
Robert McGregor - Email: admin@(remove)2host.com. Phone: 530-941-0690
Server admin, support & programing for shared & dedicated web servers
Secure, reliable hosting you expect and deserve! http://www.2host.com


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

Date: 10 Nov 2002 10:47:08 -0800
From: pdc124@yahoo.co.uk (p cooper)
Subject: looping through named arrays
Message-Id: <e742a0ce.0211101047.4831bbc6@posting.google.com>

ive got a series of array  storing data :
@filearray=qw(data1 data2 data3 data4 data5);

and want to draw graphs of the data:
drawgraph is a subroutine that draws the graph (!) using the array
'values'

 foreach $file(@filearray)
{@values=@file;
&drawgraph;
}

doesnt work :
1.why not
2.what does?
___________________________________________________________


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

Date: 10 Nov 2002 19:02:20 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de>
Subject: Re: looping through named arrays
Message-Id: <aqmafs$1sd$1@nets3.rz.RWTH-Aachen.DE>

Also sprach p cooper:

> ive got a series of array  storing data :
> @filearray=qw(data1 data2 data3 data4 data5);
> 
> and want to draw graphs of the data:
> drawgraph is a subroutine that draws the graph (!) using the array
> 'values'
> 
>  foreach $file(@filearray)
> {@values=@file;
> &drawgraph;
> }

This is bad for a number of reasons. First, you are neither using strict
nor warnings. Both had told you why your code isn't working. Hint: $file
is not equal to @file. These are two totally unrelated variables.

Also, drawgraph() should not work on global data unless there is a good
reason for it. Last, the ampersand & should not be used with subroutine
calls. This is Perl4 style while in Perl5 the ampersand serves a
different purpose.

I suggest a slight rewrite:

    #! /usr/bin/perl -w
    #                ^^ do not forget!
    use strict;
    
    my @filearray = qw(data1 data2 data3 data4 data5);
    ...
    for my $file (@filearray) {
        drawgraph($file);
    }
    ...
    sub drawgraph {
        my @args = @_;
        # $file now stored in $args[0]
        ...
    }

Using strict will probably make a couple of changes in your script
necessary. Most importantly, each newly introduced variable needs to be
declared with my(). Afterwards, this variable is then a lexical and only
exists in the current scope (read: block). For instance, in the above
drawgraph() subroutine, @args is a private variable which is invisible
on the outside and will result in a compile time error if you try to use
it there. You are, however, free to declare another @args array outside
using my(). It is then a totally unrelated variable which just happens
to have the same name.
    
This migration from the habiot of using global variables (as you did)
towards restricting them to the smallest possible scope is a bit hard at
first. But you will soon start honoring it.

Tassilo
-- 
$_=q!",}])(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus;})(rekcah{lrePbus;})(lreP{rehtonabus;})(rehtona{tsuJbus!;
$_=reverse;s/sub/(reverse"bus").chr(32)/xge;tr~\n~~d;eval;


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

Date: Sun, 10 Nov 2002 19:04:45 +0000
From: "Dave Cross" <dave@dave.org.uk>
Subject: Re: looping through named arrays
Message-Id: <pan.2002.11.10.19.04.44.590292@dave.org.uk>

On Sun, 10 Nov 2002 10:47:08 +0000, p cooper wrote:

> ive got a series of array  storing data :
> @filearray=qw(data1 data2 data3 data4 data5);
> 
> and want to draw graphs of the data:
> drawgraph is a subroutine that draws the graph (!) using the array
> 'values'
> 
>  foreach $file(@filearray)
> {@values=@file;
> &drawgraph;
> }
> 
> doesnt work :
> 1.why not

It's very difficult to say without seeing more of your code, but here's
one possibility.

You assign each element of the array @filearray, in turn, to the scalar
variable $file. You then do nothing more with this variable.

You copy the array @file (which we haven't seen before, so we can't know
what it in it) into the array @values. &drawgraph then uses the elements of
@values (which are the same as the elements of the mystery array @file) to
draw the graph.

Perhaps you didn't mean to use the array @files and instead wanted to use
the scalar $file in some way.

You can ask Perl to help you find problems like this by adding the lines

use strict;
use warnings;

to all of your Perl programs.

hth,

Dave...

-- 
  And crawling on the planet's face, some insects called the human race
  Lost in time, and lost in space. And meaning.



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

Date: 10 Nov 2002 09:29:35 -0800
From: mvr707@yahoo.com (Ramana Mokkapati)
Subject: Re: newbie LWP redirect/cookie question
Message-Id: <7bc56448.0211100929.23c35cee@posting.google.com>

Neither of the alternatives:

a) push @{ua->requests_redirectable}, 'POST';

b)     my $ua= LWP::UserAgent->new(
	requests_redirectable => ['GET', 'HEAD', 'POST']

Never worked for me. I get "405" code whenever I use
anyof these two approaches.

Puzzles me whats going on...

--Ramana


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

Date: Sun, 10 Nov 2002 18:01:35 +0200
From: "Teh (tî'pô)" <teh@mindless.com>
Subject: Re: Why not PERL?
Message-Id: <te0tsu46ou2ure0ijm5n6mvj9h9gpgvhes@4ax.com>

Jeff 'japhy' Pinyan bravely attempted to attach 23 electrodes of
knowledge to the nipples of comp.lang.perl.misc by saying:
>
>The name "perl" came first. 

Does that mean that the next language will be called Swyne?

-- 
Get it? Perl before Swyne? It's a pun, you know, like in the saying
"pearls before swine", no? OK, no need to throw me out, I'll go
quietly.

<aside>
Well *I* thought it was funny...
</aside>


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

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


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