[31258] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2503 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jul 6 00:09:43 2009

Date: Sun, 5 Jul 2009 21:09:07 -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           Sun, 5 Jul 2009     Volume: 11 Number: 2503

Today's topics:
    Re: Best way to do server side tasks with new ISP sln@netherlands.com
    Re: Best way to do server side tasks with new ISP <paul@peschoen.com>
    Re: FAQ 4.57 What happens if I add or remove keys from  <whynot@pozharski.name>
        formatting a number of elsif statements <cartercc@gmail.com>
    Re: formatting a number of elsif statements <tony_curtis32@yahoo.com>
    Re: formatting a number of elsif statements <ben@morrow.me.uk>
    Re: formatting a number of elsif statements <tony_curtis32@yahoo.com>
    Re: formatting a number of elsif statements <jurgenex@hotmail.com>
    Re: formatting a number of elsif statements <cartercc@gmail.com>
    Re: how to write a script to only process one depth dir <m@rtij.nl.invlalid>
    Re: how to write a script to only process one depth dir <tim@tim-landscheidt.de>
    Re: how to write a script to only process one depth dir <m@rtij.nl.invlalid>
    Re: how to write a script to only process one depth dir (Randal L. Schwartz)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sat, 04 Jul 2009 18:30:56 -0700
From: sln@netherlands.com
Subject: Re: Best way to do server side tasks with new ISP
Message-Id: <trvv455m441keqht10c2nb68fn161e9e1o@4ax.com>

On Sat, 04 Jul 2009 09:59:12 -0400, Charlton Wilbur <cwilbur@chromatico.net> wrote:

>>>>>> "R" == Ruud  <rvtol+usenet@xs4all.nl> writes:
>
>    R> Charlton Wilbur wrote:
>
>    >> [problem specification, with instructions to try solving it in C

	Given the following line:
 
	    foo=1;bar=yes;baz=c;baz=b;baz=c;quux=seven;quux=;submit=true
 
 	Produce the following output:
 
 	* An alphabetically sorted list of the values assigned to each variable
 	* The count of values assigned to each variable
 	* The result of foo * 27 + 3

>    >> and in Perl]
>
>    R> [code to solve the problem]
>
>Well, yes, it isn't a particularly difficult problem, either in Perl or
>in C. The point is that it's a couple dozen lines of Perl at most, and
>probably closer to a couple hundred lines of C.
>
>Charlton

In no way, shape or form, is this a  problem specification, nor anything of
any use to anything or anybody, et all.

You can't parse c/c++ with this simplistic approach.
You can't get from HERE -->"foo=1;" to HERE --> "bar=yes;".
You can't even get to HERE -->"foo=1;"

Alpha sorted list of "values" assigned to each variable? Whats that tell ya?
Count of values, here I assume you are adressing re-assignment, as if scope is not an issue.
Finally, the result of foo's multiplicity.

Are you desperately trying to be funny?

-sln



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

Date: Sun, 05 Jul 2009 19:25:09 GMT
From: "Paul E. Schoen" <paul@peschoen.com>
Subject: Re: Best way to do server side tasks with new ISP
Message-Id: <p674m.2457$9l4.2302@nwrddc01.gnilink.net>


<sln@netherlands.com> wrote in message 
news:trvv455m441keqht10c2nb68fn161e9e1o@4ax.com...
>
> In no way, shape or form, is this a  problem specification, nor anything 
> of
> any use to anything or anybody, et all.
>
> You can't parse c/c++ with this simplistic approach.
> You can't get from HERE -->"foo=1;" to HERE --> "bar=yes;".
> You can't even get to HERE -->"foo=1;"
>
> Alpha sorted list of "values" assigned to each variable? Whats that tell 
> ya?
> Count of values, here I assume you are adressing re-assignment, as if 
> scope is not an issue.
> Finally, the result of foo's multiplicity.
>
> Are you desperately trying to be funny?

Yes, I was very confused about what the point was. I have no idea why the 
following line would ever need to be parsed in C, or in any other language:

    foo=1;bar=yes;baz=c;baz=b;baz=c;quux=seven;quux=;submit=true

Perhaps in a loosely typed language, "foo" would be assumed to be numeric, 
and probably integer. "bar" might be assumed to be Boolean, although in "C" 
that is really just an integer where "yes" would need to be predefined as 
non-zero. "baz" would be undefined unless "b" and "c" were previously 
defined. Same with "quux", where "seven" also needs to be defined, and then 
it is assigned to nothing, which would be an error unless it was 
specifically assigned to NULL or nil. And finally "submit" may be 
predefined as a Boolean, and true must also have been defined.

I assume the given line is supposed to be a script which is to be 
interpreted and then some operations are supposed to be done on that 
process. Maybe in Perl and CGI there are reasons to do something like this, 
but it has just further complicated my understanding and avoidance of the 
language.

And the point of using fewer lines to accomplish the same thing is not very 
beneficial if the code is difficult to understand. Delphi (Pascal) does 
tend to be rather verbose, and I wish there were constructs like {} rather 
than "Begin" and "End", but well-written code with liberal use of 
indentation and whitespace make it easy to understand and maintain. "C" has 
some operators that make for more compact code, but IMHO at the cost of 
readability. Then Perl seems to be at another level of brevity that reminds 
me of command line switches as used in MSDOS and even CP/M utilities like 
the line editor "ed".

Paul 




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

Date: Sun, 05 Jul 2009 14:52:52 +0300
From: Eric Pozharski <whynot@pozharski.name>
Subject: Re: FAQ 4.57 What happens if I add or remove keys from a hash while iterating over it?
Message-Id: <slrnh5155f.20f.whynot@orphan.zombinet>

On 2009-07-04, Uri Guttman <uri@stemsystems.com> wrote:
>>>>>> "EP" == Eric Pozharski <whynot@pozharski.name> writes:
>
>  EP> 	$h{$_} = $_ foreach 0 .. 1E4;
>
> since i like slices:
>
> 	%h{ 0 .. 1e4 } = 0 .. 1E4 ;

Those who trash hashes know.  I don't.

>  EP> 	while( $x = each %h ) {
>  EP> 	  
>  EP> 	  delete $h{$x + 1};
>  EP> 	  push @z, $x + 1;
>
> and very few seem to know that delete returns its value and since your
> keys and values are the same:
>
> 	push @z, delete $h{$x + 1} ;

Those who B<delete> know.  I don't.


-- 
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom


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

Date: Sun, 5 Jul 2009 12:07:41 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: formatting a number of elsif statements
Message-Id: <eadc74e1-4f48-4fcd-aac3-fb72c234ec90@c1g2000yqi.googlegroups.com>

Using 5.8 in a web app, I have about 18 elsif statements which look
similar to this:

elsif ($action eq 'AddAttorney')
{
	print qq(<h4>Add Attorney</h4>);
	HTML::add_professional($oprid, $role, 'Attorney');
	HTML::print_button('setup.cgi','Return To Setup',$oprid, $role);
}
elsif($action eq 'AddInsurer')
 ...

I now fact the prospect of adding some more, hopefully not many, but
I'm getting tired of looking at all of this.

Is there any way to mimic the case/switch statement? Ideally, I'd like
to have just the matching term for each block, like this:

'AddAttorney'
        print qq(<h4>Add Attorney</h4>);
	HTML::add_professional($oprid, $role, 'Attorney');
	HTML::print_button('setup.cgi','Return To Setup',$oprid, $role);
'AddInsurer'
       ...

It's not critical, but I'm just getting tired of looking at all the
repetition.

CC.



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

Date: Sun, 05 Jul 2009 14:14:40 -0500
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: formatting a number of elsif statements
Message-Id: <87fxdbhrhb.fsf@yahoo.com>

>> On Sun, 5 Jul 2009 12:07:41 -0700 (PDT),
>> ccc31807 <cartercc@gmail.com> said:

> Using 5.8 in a web app, I have about 18 elsif statements
> which look similar to this:

> elsif ($action eq 'AddAttorney') { print qq(<h4>Add
> Attorney</h4>); HTML::add_professional($oprid, $role,
> 'Attorney'); HTML::print_button('setup.cgi','Return To
> Setup',$oprid, $role);
> }
> elsif($action eq 'AddInsurer') ...

> I now fact the prospect of adding some more, hopefully
> not many, but I'm getting tired of looking at all of
> this.

> Is there any way to mimic the case/switch statement?
> Ideally, I'd like to have just the matching term for
> each block, like this:

If each block of code is the same, you could use a hash
table to map all of the labels you want to match to the
replacement text in the HTML.  If the key's defined,
output the text with the replaced label (e.g. AddAttorney
-> "Attorney").

If it's more heterogeneous hie thee to
http://search.cpan.org/ and see what "switch" gets you...

hth
t


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

Date: Sun, 5 Jul 2009 21:00:57 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: formatting a number of elsif statements
Message-Id: <pcd7i6-6mc2.ln1@osiris.mauzo.dyndns.org>


Quoth Tony Curtis <tony_curtis32@yahoo.com>:
> >> On Sun, 5 Jul 2009 12:07:41 -0700 (PDT),
> >> ccc31807 <cartercc@gmail.com> said:
> 
> > Using 5.8 in a web app, I have about 18 elsif statements
> > which look similar to this:
> 
> > elsif ($action eq 'AddAttorney') { print qq(<h4>Add
> > Attorney</h4>); HTML::add_professional($oprid, $role,
> > 'Attorney'); HTML::print_button('setup.cgi','Return To
> > Setup',$oprid, $role);
> > }
> > elsif($action eq 'AddInsurer') ...
> 
> > I now fact the prospect of adding some more, hopefully
> > not many, but I'm getting tired of looking at all of
> > this.
> 
> > Is there any way to mimic the case/switch statement?
> > Ideally, I'd like to have just the matching term for
> > each block, like this:
> 
> If each block of code is the same, you could use a hash
> table to map all of the labels you want to match to the
> replacement text in the HTML.  If the key's defined,
> output the text with the replaced label (e.g. AddAttorney
> -> "Attorney").

That's a good suggestion. Another might be to use a hash table of
subrefs, like

    my %dispatch = (
        AddAttorney => sub { 
            print qq(<h4>Add Attorney</h4>); 
            HTML::add_professional($oprid, $role, 'Attorney');
            HTML::print_button('setup.cgi','Return To Setup',
                $oprid, $role);
        },
        AddInsurer => sub { ... },
    );

    $dispatch{$action}();

or even a class with a method for each action.

> If it's more heterogeneous hie thee to
> http://search.cpan.org/ and see what "switch" gets you...

Nonono, don't do that! Switch.pm was Not A Good Idea. It uses a source
filter built on Text::Balanced to do its work, and there are more than a
few constructions that cause it to parse your source incorrectly. The
resulting errors are very hard to track down.

If you can afford to require 5.10, you can use given/when.

Ben



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

Date: Sun, 05 Jul 2009 15:25:04 -0500
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: formatting a number of elsif statements
Message-Id: <87bpnyj2sf.fsf@yahoo.com>

>> On Sun, 5 Jul 2009 21:00:57 +0100,
>> Ben Morrow <ben@morrow.me.uk> said:

> Nonono, don't do that! Switch.pm was Not A Good Idea. It
> uses a source filter built on Text::Balanced to do its
> work, and there are more than a few constructions that
> cause it to parse your source incorrectly. The resulting
> errors are very hard to track down.

Oh.  Didn't know that.  Actually I've never used it myself
so I wasn't aware of any shortcomings.

The data->dispatch approach is more aesthetically pleasing
anyway.

t


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

Date: Sun, 05 Jul 2009 13:25:26 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: formatting a number of elsif statements
Message-Id: <s92255hhlunj6figh4c5v8jbqa4ctidr9l@4ax.com>

ccc31807 <cartercc@gmail.com> wrote:
>Using 5.8 in a web app, I have about 18 elsif statements which look
>similar to this:
>
>elsif ($action eq 'AddAttorney')
>{
>	print qq(<h4>Add Attorney</h4>);
>	HTML::add_professional($oprid, $role, 'Attorney');
>	HTML::print_button('setup.cgi','Return To Setup',$oprid, $role);
>}
>elsif($action eq 'AddInsurer')
>...
>
>I now fact the prospect of adding some more, hopefully not many, but
>I'm getting tired of looking at all of this.
[...]
>It's not critical, but I'm just getting tired of looking at all the
>repetition.

Yeah, repetition is what computers are good at.

Step 1: identify which pieces of your repetitions are constant and which
pieces change. I can only guess but it appears like 

	print qq(<h4>Add #####</h4>);
	HTML::add_professional($oprid, $role, '#####');
	HTML::print_button('setup.cgi','Return To Setup',$oprid, $role);

is the constant part with the variable/changing part blocked out by
#####. 

Step 2: Find a way to compute the variable parts from a suitable
parameter. In this case you got $action as the parameter and you want to
map it into a simple string. A hash is an ideal and very simple
datastructure for that.

my %param= {'AddAttorney' -> 'Attorney',
		'AddInsurer' -> 'Insurer',
		.....
		}

Step 3: Replace the placeholder for the variable part ##### with actual
parameterized code that retrieves the variable part at runtime:

 	print qq(<h4>Add $param{$action}</h4>);
	HTML::add_professional($oprid, $role, $param{$action});
	HTML::print_button('setup.cgi','Return To Setup',$oprid, $role);

That should do it.

jue




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

Date: Sun, 5 Jul 2009 15:27:37 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: formatting a number of elsif statements
Message-Id: <b7cdfff9-9974-4b17-b834-7064d2f95e2d@c1g2000yqi.googlegroups.com>

Thanks, Ben.

I went ahead and bit the bullet.

I wrote a function that takes, among other arguments, an entity type
and an action type, so now I just call one procedure. It did require a
translation table as you suggested (which I hard coded for now but
will probably put into a database), but that's a lot easier on the
eyes than a couple hundred lines of elsif statements.

As a bonus, I am able to abstract my SQL as well. In my code, I have
procedures to:
 - add
 - insert
 - view all
 - view one
 - edit
 - update
 - delete

for a dozen different categories, Insurers, Employers, Attorneys,
Agents, etc. It started out pretty small, but man, scaling becomes an
issue when different people start adding their wants to the list!
Saying, 'Yes, I can do a little app to track the players,' turns into
a Sunday at the keyboard when the office gets a crack at it.

On Jul 5, 4:00=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> That's a good suggestion. Another might be to use a hash table of
> subrefs, like
>
> =A0 =A0 my %dispatch =3D (
> =A0 =A0 =A0 =A0 AddAttorney =3D> sub {
> =A0 =A0 =A0 =A0 =A0 =A0 print qq(<h4>Add Attorney</h4>);
> =A0 =A0 =A0 =A0 =A0 =A0 HTML::add_professional($oprid, $role, 'Attorney')=
;
> =A0 =A0 =A0 =A0 =A0 =A0 HTML::print_button('setup.cgi','Return To Setup',
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 $oprid, $role);
> =A0 =A0 =A0 =A0 },
> =A0 =A0 =A0 =A0 AddInsurer =3D> sub { ... },
> =A0 =A0 );
>
> =A0 =A0 $dispatch{$action}();
>
> or even a class with a method for each action.

One of these days, I'm going to learn OO Perl. I've got two books on
my reading list, 'Object Oriented Perl' and 'Higher Order Perl', but
I've never been able timewise to do more than just browse through
them.

Thanks for your suggestion.

CC


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

Date: Sun, 5 Jul 2009 14:02:14 +0200
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: how to write a script to only process one depth directories
Message-Id: <6bh6i6-fgt.ln1@news.rtij.nl>

On Thu, 02 Jul 2009 01:14:47 -0700, robertchen117@gmail.com wrote:

> my directory /server has many servers under it, each /server/server1/ is
> a directory. I only want to find files like and add/change some
> contents:
> 
> /server/server1/Defaults
> /server/server2/Defaults
> ......
> /server/servern/Defaults
> 
> and process it, for any other sub directories just ignore them. How to
> do this in perl?
> Please help me, thanks.

Not a completely Perl based solution, but I would do:

echo /server/server*/Defaults | xargs perl -pi <script>

(or use some other flags to perl, depending on your requirements)

(btw, using (build in!) echo avoids command line length limits, don't use 
ls. Gnu find is another good solution, but echo is wayyy simpler in this 
case.)

(note: does NOT work if you have spaces or other whitespace characters in 
file names!)

M4


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

Date: Sun, 05 Jul 2009 13:01:31 +0000
From: Tim Landscheidt <tim@tim-landscheidt.de>
Subject: Re: how to write a script to only process one depth directories
Message-Id: <m3hbxruvv8.fsf@passepartout.tim-landscheidt.de>

Martijn Lievaart <m@rtij.nl.invlalid> wrote:

>> my directory /server has many servers under it, each /server/server1/ is
>> a directory. I only want to find files like and add/change some
>> contents:

>> /server/server1/Defaults
>> /server/server2/Defaults
>> ......
>> /server/servern/Defaults

>> and process it, for any other sub directories just ignore them. How to
>> do this in perl?
>> Please help me, thanks.

> Not a completely Perl based solution, but I would do:

> echo /server/server*/Defaults | xargs perl -pi <script>

> (or use some other flags to perl, depending on your requirements)

> (btw, using (build in!) echo avoids command line length limits, don't use
> ls. Gnu find is another good solution, but echo is wayyy simpler in this
> case.)

> (note: does NOT work if you have spaces or other whitespace characters in
> file names!)

So: Use "find -print0" and "xargs -0r". Or, in this particu-
lar case, "perl -pi $SCRIPT /server/server*/Defaults". Oh,
and always use "-i.bak" while you're at it.

Tim


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

Date: Mon, 6 Jul 2009 05:00:05 +0200
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: how to write a script to only process one depth directories
Message-Id: <lu58i6-fgt.ln1@news.rtij.nl>

On Sun, 05 Jul 2009 13:01:31 +0000, Tim Landscheidt wrote:

>> (btw, using (build in!) echo avoids command line length limits, don't
>> use ls. Gnu find is another good solution, but echo is wayyy simpler in
>> this case.)
> 
>> (note: does NOT work if you have spaces or other whitespace characters
>> in file names!)
> 
> So: Use "find -print0" and "xargs -0r". Or, in this particu- lar case,
> "perl -pi $SCRIPT /server/server*/Defaults". Oh, and always use "-i.bak"
> while you're at it.

This does not do what the OP wants without additional flags, and is Gnu 
specific as well. It can work with something like (untested):

find /server -path '/server/server*/' -midepth 2 -maxdepth 2 -name 
Default -print0 | xargs -0r ......

Otherwise you may run into command line limits again. A non Gnu solution 
would be something like (again untested):

echo /server/server*/Defaults | while read f; do perl -pi $SCRIPT; done

This still does not account for file names with embedded newlines, but 
those are rare enough not to worry about them in most cases.

M4



M4


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

Date: Sun, 05 Jul 2009 20:10:46 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: how to write a script to only process one depth directories
Message-Id: <86prcea4ll.fsf@blue.stonehenge.com>

>>>>> "Martijn" == Martijn Lievaart <m@rtij.nl.invlalid> writes:

Martijn> echo /server/server*/Defaults | while read f; do perl -pi $SCRIPT; done

I don't think that'll do it.

echo will generate something like:

  /server/server1/Defaults /server/server2/Defaults /server/server3/Defaults

which "read f" will read all at once making $f to be

  "/server/server1/Defaults /server/server2/Defaults /server/server3/Defaults"

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion


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

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 V11 Issue 2503
***************************************


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