[28982] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 226 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Mar 15 14:10:31 2007

Date: Thu, 15 Mar 2007 11:09: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           Thu, 15 Mar 2007     Volume: 11 Number: 226

Today's topics:
        anyone could explain this to me? <robertchen117@gmail.com>
    Re: anyone could explain this to me? <tzz@lifelogs.com>
    Re: anyone could explain this to me? <bik.mido@tiscalinet.it>
        Getting the list of groups given a user id arshad.tanveer@gmail.com
    Re: Getting the list of groups given a user id <josef.moellers@fujitsu-siemens.com>
    Re: Getting the list of groups given a user id arshad.tanveer@gmail.com
    Re: Getting the list of groups given a user id <josef.moellers@fujitsu-siemens.com>
    Re: Getting the list of groups given a user id <paduille.4060.mumia.w+nospam@earthlink.net>
    Re: Getting the list of groups given a user id xhoster@gmail.com
    Re: Getting the list of groups given a user id <someone@example.com>
    Re: Getting the list of groups given a user id <tzz@lifelogs.com>
        help for PostScript::Simple <"v.niekerk at hccnet.nl">
    Re: help for PostScript::Simple <paduille.4060.mumia.w+nospam@earthlink.net>
    Re: help for PostScript::Simple <"v.niekerk at hccnet.nl">
    Re: Importing an hash in a lexical scope <stefano.sabatini-lala@poste.it>
    Re: Perl DBI/XML processing versus PHP ? (Jamie)
    Re: tr/ last char x$ <glennj@ncf.ca>
    Re: tr/ last char x$ <aisling.cronin@gmail.com>
    Re: tr/ last char x$ anno4000@radom.zrz.tu-berlin.de
    Re: Use different modules based on variable <uri@stemsystems.com>
    Re: Use different modules based on variable <uri@stemsystems.com>
    Re: Use different modules based on variable <uri@stemsystems.com>
    Re: Use different modules based on variable anno4000@radom.zrz.tu-berlin.de
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 15 Mar 2007 08:28:39 -0700
From: "robertchen117@gmail.com" <robertchen117@gmail.com>
Subject: anyone could explain this to me?
Message-Id: <1173972519.782261.59600@l77g2000hsb.googlegroups.com>

what the following sentence in a perl do? Please explain with detail.
Thanks.

grep (do { chop; s/^([^=]+)=(.*)$/$ENV{$1}=$2/e},`. /etc/Tivoli/
setup_env.sh;env`);



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

Date: Thu, 15 Mar 2007 13:51:56 -0400
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: anyone could explain this to me?
Message-Id: <g69zm6egzsz.fsf@dhcp-65-162.kendall.corp.akamai.com>

On 15 Mar 2007 08:28:39 -0700 "robertchen117@gmail.com" <robertchen117@gmail.com> wrote: 

rc> what the following sentence in a perl do? Please explain with detail.

rc> grep (do { chop; s/^([^=]+)=(.*)$/$ENV{$1}=$2/e},`. /etc/Tivoli/setup_env.sh;env`);

1) `. /etc/Tivoli/setup_env.sh;env`

source that .sh file and print the environment

2) s/^([^=]+)=(.*)$/$ENV{$1}=$2/e

take all the environment variables from (1) and put them into the
current environment (using grep for side effects, which is naughty)

So basically it's an unnecessarily complex way to grab the environment
resulting from /etc/Tivoli/setup_env.sh

Ted


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

Date: Thu, 15 Mar 2007 19:07:07 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: anyone could explain this to me?
Message-Id: <k12jv217gloj351f3f0ch2cnd4ren5al0j@4ax.com>

On 15 Mar 2007 08:28:39 -0700, "robertchen117@gmail.com"
<robertchen117@gmail.com> wrote:

>Subject: anyone could explain this to me?

What? Please put the subject of your post in the Subject.

>what the following sentence in a perl do? Please explain with detail.
>
>grep (do { chop; s/^([^=]+)=(.*)$/$ENV{$1}=$2/e},`. /etc/Tivoli/
>setup_env.sh;env`);

Fire up a shell and run these commands,

  . /etc/Tivoli/setup_env.sh

then

  env

get the output of the last command, which is a list of lines and for
all of them remove the last charachter with the chop() function. If
the rest of the string consists of one or more charachters none of
which a '=' followed by an '=' and then something else, then assign
that "something else" to the environment variable with the same name
as what's before the equal sign.

FYI, it's very badly written code to do what it does, in particular
using grep() and s/// only for their side effects, chop() instead of
chomp() (but wouldn't even be required, with the $ anchor) and a
superflous do() block.


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: 15 Mar 2007 06:47:20 -0700
From: arshad.tanveer@gmail.com
Subject: Getting the list of groups given a user id
Message-Id: <1173966440.853373.37720@b75g2000hsg.googlegroups.com>

Hi,
I am working on a script that would run on Solaris and I need a Perl
script/function/module that would give me the list of groups that a
user belongs to- just like the Unix groups command. Could someone
please help?

Regards,
Tanveer



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

Date: Thu, 15 Mar 2007 14:54:24 +0100
From: Josef Moellers <josef.moellers@fujitsu-siemens.com>
Subject: Re: Getting the list of groups given a user id
Message-Id: <etbjdi$b0e$1@nntp.fujitsu-siemens.com>

arshad.tanveer@gmail.com wrote:
> Hi,
> I am working on a script that would run on Solaris and I need a Perl
> script/function/module that would give me the list of groups that a
> user belongs to- just like the Unix groups command. Could someone
> please help?

Please have a look at the posting guidelines.
What have you tried so far and where did your code not meet your=20
expectations?

--=20
These are my personal views and not those of Fujitsu Siemens Computers!
Josef M=F6llers (Pinguinpfleger bei FSC)
	If failure had no penalty success would not be a prize
						-- T.  Pratchett



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

Date: 15 Mar 2007 07:16:44 -0700
From: arshad.tanveer@gmail.com
Subject: Re: Getting the list of groups given a user id
Message-Id: <1173968204.569268.313180@n76g2000hsh.googlegroups.com>

On Mar 15, 6:54 pm, Josef Moellers <josef.moell...@fujitsu-
siemens.com> wrote:
> arshad.tanv...@gmail.com wrote:
> > Hi,
> > I am working on a script that would run on Solaris and I need a Perl
> > script/function/module that would give me the list of groups that a
> > user belongs to- just like the Unix groups command. Could someone
> > please help?
>
> Please have a look at the posting guidelines.
> What have you tried so far and where did your code not meet your
> expectations?
>
> --
> These are my personal views and not those of Fujitsu Siemens Computers!
> Josef M=F6llers (Pinguinpfleger bei FSC)
>         If failure had no penalty success would not be a prize
>                                                 -- T.  Pratchett


I haven't tried anything because I don't even know where to start.
Googl'ing for answers, I came across functions like getgrnam but they
give a list of users in a group. I want it the other way round. Given
a user id, how to determine the groups?

Thanks,
Tanveer



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

Date: Thu, 15 Mar 2007 15:35:45 +0100
From: Josef Moellers <josef.moellers@fujitsu-siemens.com>
Subject: Re: Getting the list of groups given a user id
Message-Id: <etblri$k30$1@nntp.fujitsu-siemens.com>

arshad.tanveer@gmail.com wrote:
> On Mar 15, 6:54 pm, Josef Moellers <josef.moell...@fujitsu-
> siemens.com> wrote:
>=20
>>arshad.tanv...@gmail.com wrote:
>>
>>>Hi,
>>>I am working on a script that would run on Solaris and I need a Perl
>>>script/function/module that would give me the list of groups that a
>>>user belongs to- just like the Unix groups command. Could someone
>>>please help?
>>
>>Please have a look at the posting guidelines.
>>What have you tried so far and where did your code not meet your
>>expectations?

>=20
> I haven't tried anything because I don't even know where to start.
> Googl'ing for answers, I came across functions like getgrnam but they
> give a list of users in a group. I want it the other way round. Given
> a user id, how to determine the groups?

You mean: you don't know anything about Perl? Or you don't know anything =

about where Solaris keeps this information?

One of the major features of Perl are its text processing abilities.
Usually Un*x base systems keep the information about users and groups in =

text files in /etc (I would be surprised if Solaris did otherwise).
So, adding 1(text processing features) and 1(information kept in text=20
files) together should get you started. If you have problems getting the =

first line of Perl code into the machine or onto a sheet of paper, this=20
group is definitely the wrong place to start. Better get a decent book,=20
e.g. the camel book, and start reading.

You wrote "I'm working os a script ...", so you must at least have=20
something to start with.

Eeven if you post a rudimentary program that shows that you at least=20
tried to do some work yourself, you'll find legions of people willing to =

help (well, maybe not legions).

This is not a group to ask for ready-to-use solutions.
--=20
These are my personal views and not those of Fujitsu Siemens Computers!
Josef M=F6llers (Pinguinpfleger bei FSC)
	If failure had no penalty success would not be a prize
						-- T.  Pratchett



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

Date: Thu, 15 Mar 2007 14:40:25 GMT
From: "Mumia W." <paduille.4060.mumia.w+nospam@earthlink.net>
Subject: Re: Getting the list of groups given a user id
Message-Id: <tVcKh.10040$PL.3285@newsread4.news.pas.earthlink.net>

On 03/15/2007 08:47 AM, arshad.tanveer@gmail.com wrote:
> Hi,
> I am working on a script that would run on Solaris and I need a Perl
> script/function/module that would give me the list of groups that a
> user belongs to- just like the Unix groups command. Could someone
> please help?
> 
> Regards,
> Tanveer
> 

Read about the "getgrent" function:

Start->Run->"perldoc -f getgrent"

-------
Posting Guidelines for comp.lang.perl.misc:
http://www.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html




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

Date: 15 Mar 2007 15:26:00 GMT
From: xhoster@gmail.com
Subject: Re: Getting the list of groups given a user id
Message-Id: <20070315112601.789$VF@newsreader.com>

arshad.tanveer@gmail.com wrote:
> Hi,
> I am working on a script that would run on Solaris and I need a Perl
> script/function/module that would give me the list of groups that a
> user belongs to- just like the Unix groups command. Could someone
> please help?

The "just like the unix groups command" sound makes this sound like a
self-answering question to me.

my @groups = split " ", `groups $user`;

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


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

Date: Thu, 15 Mar 2007 15:31:47 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: Getting the list of groups given a user id
Message-Id: <DFdKh.117357$cE3.10149@edtnps89>

arshad.tanveer@gmail.com wrote:
> 
> I am working on a script that would run on Solaris and I need a Perl
> script/function/module that would give me the list of groups that a
> user belongs to- just like the Unix groups command. Could someone
> please help?

my $user = 'tanveer';

my @groups = scalar getgrgid( ( getpwnam $user )[ 3 ] );

while ( my ( $name, $users ) = ( getgrent )[ 0, -1 ] ) {
    $users =~ /\b$user\b/ and push @groups, $name
    }

print "$user : @groups\n";




John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall


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

Date: Thu, 15 Mar 2007 14:00:06 -0400
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Getting the list of groups given a user id
Message-Id: <g69veh2gzfd.fsf@dhcp-65-162.kendall.corp.akamai.com>

On Thu, 15 Mar 2007 15:35:45 +0100 Josef Moellers <josef.moellers@fujitsu-siemens.com> wrote: 

JM> This is not a group to ask for ready-to-use solutions.

Why not?

I like to know if a ready-made solution exists for many common Perl
tasks all the time, so I don't write them myself.  

What's wrong is expecting people to write the solution for you, which
Arshad did not do.  He clearly asked for a module/function/solution to
help him in the course of writing a script, to get the list of groups
for a user.  Why does he have to attempt to write such a common task
himself, given that it's probably been done already a million times?

Let's be sensible.  I understand the Posting Guidelines are important,
but they should not override common sense.

Ted


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

Date: Thu, 15 Mar 2007 15:39:27 +0100
From: Huub <"v.niekerk at hccnet.nl">
Subject: help for PostScript::Simple
Message-Id: <45f95a9f$0$2185$e4fe514c@dreader26.news.xs4all.nl>

Hi,

I read that using PostScript::Simple would be handy for printing ps 
files from Perl. So I installed the module, but I fail to find any kind 
of manual of how to actually use it. Can someone provide me with (some) 
info?

Thanks,

Huub


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

Date: Thu, 15 Mar 2007 15:17:28 GMT
From: "Mumia W." <paduille.4060.mumia.w+nospam@earthlink.net>
Subject: Re: help for PostScript::Simple
Message-Id: <csdKh.127731$_73.118995@newsread2.news.pas.earthlink.net>

On 03/15/2007 09:39 AM, Huub wrote:
> Hi,
> 
> I read that using PostScript::Simple would be handy for printing ps 
> files from Perl. So I installed the module, but I fail to find any kind 
> of manual of how to actually use it. Can someone provide me with (some) 
> info?
> 
> Thanks,
> 
> Huub

Huh?

Is this all you're looking for?
http://search.cpan.org/~mcnewton/PostScript-Simple-0.07/lib/PostScript/Simple.pm

Depending upon how your Perl is set up, this should also get you the 
documentation:

Start->Run->"perldoc PostScript::Simple"




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

Date: Thu, 15 Mar 2007 16:52:09 +0100
From: Huub <"v.niekerk at hccnet.nl">
Subject: Re: help for PostScript::Simple
Message-Id: <45f96ba9$0$3593$e4fe514c@dreader31.news.xs4all.nl>

> 
> Huh?
> 
> Is this all you're looking for?
> http://search.cpan.org/~mcnewton/PostScript-Simple-0.07/lib/PostScript/Simple.pm 
> 
> 
> Depending upon how your Perl is set up, this should also get you the 
> documentation:
> 
> Start->Run->"perldoc PostScript::Simple"
> 
> 

Thank you. I just installed it from CPAN: "install PostScript::Simple", 
and didn't realize I could find it using perldoc that easy.


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

Date: Thu, 15 Mar 2007 15:49:21 +0100 (CET)
From: Stefano Sabatini <stefano.sabatini-lala@poste.it>
Subject: Re: Importing an hash in a lexical scope
Message-Id: <slrnevin1a.2q4.stefano.sabatini-lala@santefisi.caos.org>

On 2007-03-14, Uri Guttman <uri@stemsystems.com> wrote:
>>>>>> "SS" == Stefano Sabatini <stefano.sabatini-lala@poste.it> writes:
>
>  >> was use strict enabled? won't work then.
>
>  SS> well, I was using no strict there.
>
> that is the whole problem. strict is meant to stop you from doing evil
> things.
>
>  SS> is there a better way to import an hash in the main package (without
>  SS> disabling the strict pragma), or is this a bad practice to avoid?
>
> you haven't explained your bigger picture. why do you need to import the
> hash? can you pass it as an argument by reference? can you remotely
> access it via a sub or method? there are other ways to share data than
> just importing. you seem to be stuck on only import and i see no reason
> from you why that is so critical. 

There is no particular reason for wondering about to import an hash. I
only stepped on the problem, I considerd the basic hash access method
and I wondered if it was possible to use that other method, that
seemed to slightly simplify the user code. Maybe I should have asked
myself if that was a good thing to do, and as you say maybe it's not
(for the same reasons you explained).

But I thought that was an interesting problem, I meant good to stretch
my own knowledge and awareness of the language.

And if you look at the problem from another point of view maybe my
question doesn't appear so much dumb: it's possible to import some
symbols from a package (that is an hash) into the current package,
it's possible (although disabling strict) to import a regular hash in
the current package as I showed, so maybe it could be possible to
import an hash in a lexycal environment.

> your goal isn't worthy of being achieved as it is misguided. putting
> lexicals into a remote scope would be dangerous even it could be
> done. what if outside data set some other lexical that was used for some
> external operation? a security hole a mile wide. php is famous for this
> sort of thing (all cgi params automatically become variables, same nasty
> concept).
> as i have shown you here, you can use a templater. or have the users
> assign their own lexicals. and there is nothing wrong with your basic
> hash access as you know that works. why do you think your users need
> such a minor extra little helper like your lexical idea? the other
> solutions are almost as short and so much better in many ways.
>

OK, indeed it's doesn't seems that bad, and I think I'll stick with
the basic hash access.

Many thanks.

Cheers
-- 
Stefano Sabatini
Linux user number 337176 (see http://counter.li.org)


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

Date: Thu, 15 Mar 2007 13:38:59 GMT
From: nospam@geniegate.com (Jamie)
Subject: Re: Perl DBI/XML processing versus PHP ?
Message-Id: <Lc117394410333170x895cd90@pong.podro.com>

In <z5KdnSo5vd-qpmTYnZ2dnUVZ_ternZ2d@comcast.com>,
Jerry Stuckle <jstucklex@attglobal.net> mentions:
>True, if you're running 1K inserts per second it can make a difference. 
>  For most perl and PHP isn't doing nearly that much.  And if you need 
>the performance boost, a compiled language would provide a greater boost 
>than just using placeholders.

The thing is, this cache happens server-side (server relative to the DBM
server)

In the case of n-number of web server processes, it matters because you'll
likely have fewer DBM resources. Compiled web programs won't even help
you here as this stage happens on the database.

But, this is pretty much a moot point for mysql, unless mysql has implemented
placeholders and I haven't heard about it.

I will only add that placeholders also shield you from many SQL injection
bugs. Something that is nice to have around.

>Obviously you don't understand PHP very well.  I've been programming 
>since the early 4.0.x and currently am at 5.2.1.  I can't remember when 
>I've had to chance any code for a new release.  Use good programming 
>practices and upgrades are painless.

It depends on who installed the PHP binary server-side. I've smacked my
head into this wall a number of times. PHP 5.n looked really promising,
I ported my stuff to it.. and soon discovered almost all the ISP's out
there are still using PHP 4... and continue to use PHP 4.

>Now that doesn't mean I haven't rewritten code.  But it's been to take 
>advantage of new features rather than because a new version required a 
>change.

Like, if they had compiled php without POSIX support for example? Thats
the kind of stuff I'm talking about. One place has posix, the other doesn't
and you end up coding around whichever environment is available.

As far as actual code crashing.. I've had numerous problems with PHP reference
handling, looking in apache error logs reveal a segfault depending of
course on your particular copy of php. (PHP5 gets reference handling "right"
in my view, too bad it's flaky)

>This is not to say that sometimes changes may not be needed.  The PHP 
>developers are doing their best to clean up what has been a mess of a 
>language - poorly planned and executed.  It's changing, but sometimes 
>change is painful.

Oh, I agree. Not a slam against the PHP developers. PHP is great for small
things where you only need a quick mysql query or access to the include()
functions. 

Heckuva lot easier than installing perl template modules.

>Fortunately perl was better planned and implemented.

Yea, Larry Wall.. a linguist. You can tell by the way it was designed that
it actually /was/ designed.

>Wrong on both count.  I've seen both used in some quite large projects. 
>- one perl project I know about was over 250K LOC, for instance.  And 
>that's perl code - not html.

Spam assassin is another that is quite impressive. It's just that with perl (or
PHP) the rules aren't as "enforced". Perl6 I hear is trying to address this.

>Yep, exception handling was added to PHP, so like with any new addition, 
>you can use it in later versions but not in earlier ones.  Gee - you 
>know, the same thing is true in any product. It's hard to use something 
>before it was added.

True. It's just that with the large installed base of PHP4, you'll find
this fact is a real problem.

>No, that's different implementations.  But I've never seen this as a 
>problem in either PHP or perl.  The input document should be arriving 
>pretty much all at once, anyway.

As I recall (it's been awhile) doing something like, say, a PUT handler
involved first the PHP engine accepting the request, spooling the data 
to a temp file or memory or what have you, setting up it's environment
and THEN calling your script.

Perl on the other hand, not being web centric, gives you the opportunity
to read in from standard input directly if you want, so you could
build an XML tree from the source document /as/ the document is being
posted. 

Why not take advantage of the IO-bound time to build a data structure as data
arives? Especially considering you were going to read it anyway? 
(caveats about DOS attacks, existing frameworks and the like assumed)

>> The "heavy object sharing" lends itself really well to XSLT processors
>> or DOM objects that don't change, load once and re-use over and over.
>> This can really be crucial with XML processing, as XSLT transformers
>> tend to be kind of expensive to create.
>>
>
>True, but if you really have to, there are ways around it in PHP.  But 
>if you really want speed in processing, I suggest you implement it in 
>C/C++.  That is much faster.

C/C++ will always be faster than perl. (but you could just as well say
that if you need it fast, you should write it in assembly) 

One reason for doing it in perl (or PHP) is that you don't need to compile
it for the platform. 

Being able to store a tree in memory (with perl mod_perl/FCGI) is a balance
between cross-platform and performance. An option you don't really have 
with PHP. (and actually, don't have with perl either, unless FCGI or mod_perl
is available)

Shared heavy objects are a significant issue with XSLT transformations.

>OTOH, perl is older and therefore more stable and mature.  Newer is not 
>always better.  Each language needs to be evaluated in the context it 
>will be used.  A language which fits well into one project may not fit 
>well into another.

In the context of XML and databases, I think perl is the winner. In the context
of popularity contests and quick projects, PHP is the winner.

Popularity is significant if you need to tie in with other products, as they
will most likely be PHP.

Jamie
-- 
http://www.geniegate.com                    Custom web programming
Perl * Java * UNIX                        User Management Solutions


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

Date: 15 Mar 2007 13:31:44 GMT
From: Glenn Jackman <glennj@ncf.ca>
Subject: Re: tr/ last char x$
Message-Id: <slrneviim0.s5p.glennj@smeagol.ncf.ca>

At 2007-03-15 07:57AM, "ash" wrote:
>  I am trying to substitute the last char in each array of strings,
>  could some please explain why the tr/ function is not replacing just
>  the last char (see below)
>  
>  The following works but is very bulky
>  $ts_roundup[$x] = $ts_number[$x];
>  if ($ts_number[$x]=~ /0$/)
>  			{
>  	  			$ts_roundup[$x]=~ s/0$/1/;
>  	  		}elsif ($ts_number[$x]=~ /1$/)
>    			{
>  	  			$ts_roundup[$x]=~ s/1$/2/;
>  	  		} elsif ($ts_number[$x]=~ /2$/)
>    			{
>  	  			$ts_roundup[$x]=~ s/2$/3/;
>  	  		} etc etc
>  
>  
>  Correct Result:
>  ts number  = 0109  ts round up =010a
>  ts number  = 7402  ts round up =7403
>  ts number  = 5586  ts round up =5587

Are you just incrementing a list of hex numbers ?

    my @ts_roundup = map {sprintf '%04x', 1+hex} @ts_number;

-- 
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry


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

Date: 15 Mar 2007 06:51:14 -0700
From: "ash" <aisling.cronin@gmail.com>
Subject: Re: tr/ last char x$
Message-Id: <1173966674.772451.103540@l75g2000hse.googlegroups.com>

Thanks a million guys!

Both solutions are brilliant, yep that is what i was trying to do have
a neat way to just incrementing hex numbers




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

Date: 15 Mar 2007 14:06:30 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: tr/ last char x$
Message-Id: <55t276F26larcU1@mid.dfncis.de>

Glenn Jackman  <glennj@ncf.ca> wrote in comp.lang.perl.misc:
> At 2007-03-15 07:57AM, "ash" wrote:
> >  I am trying to substitute the last char in each array of strings,
> >  could some please explain why the tr/ function is not replacing just
> >  the last char (see below)
> >  
> >  The following works but is very bulky
> >  $ts_roundup[$x] = $ts_number[$x];
> >  if ($ts_number[$x]=~ /0$/)
> >  			{
> >  	  			$ts_roundup[$x]=~ s/0$/1/;
> >  	  		}elsif ($ts_number[$x]=~ /1$/)
> >    			{
> >  	  			$ts_roundup[$x]=~ s/1$/2/;
> >  	  		} elsif ($ts_number[$x]=~ /2$/)
> >    			{
> >  	  			$ts_roundup[$x]=~ s/2$/3/;
> >  	  		} etc etc
> >  
> >  
> >  Correct Result:
> >  ts number  = 0109  ts round up =010a
> >  ts number  = 7402  ts round up =7403
> >  ts number  = 5586  ts round up =5587
> 
> Are you just incrementing a list of hex numbers ?
> 
>     my @ts_roundup = map {sprintf '%04x', 1+hex} @ts_number;

Yes, but that's different.  None of the solutions so far did a carry.
No idea if that's intended that way or if it's an oversight.

Anno


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

Date: Thu, 15 Mar 2007 09:32:40 -0500
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Use different modules based on variable
Message-Id: <x7slc6d1br.fsf@mail.sysarch.com>


Prepare two modules Module1.pm and Module2.pm that implement the
different versions of your function(s).  Load both without importing
anything into your namespace.  Then, in your main program, you can
define a function switch_version that imports the necessary functions
at run time.  This is how it might look:

The main script:

    use Module_1 ();
    use Module_2 ();

    sub switch_version {
        my $version = shift;
        my @functions = qw( gick);
        for ( @functions ) {
            no strict 'refs';
            no warnings 'redefine';
            *$_ = \ &{ join '::', "Module_$version", $_ };
        }
    }

that is way to complex IMO for this task. once you have separated the
namespaces, it is trivial to handle the different calls with a dispatch
table or similar polymorphism:

	my %chooser = (

		v1	=> \&Module_1::gick,
		v2	=> \&Module_2::gick,
	) ;

if you need more than 1 function then a multilevel hash is perfect:

	my %chooser = (

		v1	=> {
				foo	=> \&Module_1::foo,
				bar	=> \&Module_1::bar,
		},
		v2	=> {
				foo	=> \&Module_2::foo,
				bar	=> \&Module_2::bar,
		},
	) ;


	my $vfunc = $chooser{$vers}{$func} or
		die "bad version/func $vers/$func" ;
	$vfunc->() ;

no strict issues, no symbol table hacking, etc.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: Thu, 15 Mar 2007 09:34:02 -0500
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Use different modules based on variable
Message-Id: <x7odmud19h.fsf@mail.sysarch.com>

>>>>> "fc" == fiedlert@gmail com <fiedlert@gmail.com> writes:

  fc> BEGIN
  fc> {
  fc>     eval "use x" if ...
  fc>     eval "use y" if ...
  fc> }

that also fails as he wants to do this decision multiple times in a
run. and your code is already done with the 'if' pragma (but that
doesn't solve the OP's problem either).

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: Thu, 15 Mar 2007 09:36:03 -0500
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Use different modules based on variable
Message-Id: <x7mz2ed164.fsf@mail.sysarch.com>

>>>>> "p" == perlguru  <manojkumargupta@gmail.com> writes:

  p> have you given a try ? ... Give a hit !!, and see the result.

have you tried it with multiple tries in a single process? if so, show
the code and results as i will be amazed if if works. require will not
reload a module as you think it does. and the modules will use the same
namespace so you can't have both loaded at one time.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: 15 Mar 2007 15:04:23 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: Use different modules based on variable
Message-Id: <55t5jnF26ht8gU1@mid.dfncis.de>

Uri Guttman  <uri@stemsystems.com> wrote in comp.lang.perl.misc:
> > 
> > Prepare two modules Module1.pm and Module2.pm that implement the
> > different versions of your function(s).  Load both without importing
> > anything into your namespace.  Then, in your main program, you can
> > define a function switch_version that imports the necessary functions
> > at run time.  This is how it might look:
> > 
> > The main script:
> > 
> >     use Module_1 ();
> >     use Module_2 ();
> > 
> >     sub switch_version {
> >         my $version = shift;
> >         my @functions = qw( gick);
> >         for ( @functions ) {
> >             no strict 'refs';
> >             no warnings 'redefine';
> >             *$_ = \ &{ join '::', "Module_$version", $_ };
> >         }
> >     }

Hey, what happened to your quote indicator? [fixed]

> that is way to complex IMO for this task. once you have separated the
> namespaces, it is trivial to handle the different calls with a dispatch
> table or similar polymorphism:
> 
> 	my %chooser = (
> 
> 		v1	=> \&Module_1::gick,
> 		v2	=> \&Module_2::gick,
> 	) ;
> 
> if you need more than 1 function then a multilevel hash is perfect:
> 
> 	my %chooser = (
> 
> 		v1	=> {
> 				foo	=> \&Module_1::foo,
> 				bar	=> \&Module_1::bar,
> 		},
> 		v2	=> {
> 				foo	=> \&Module_2::foo,
> 				bar	=> \&Module_2::bar,
> 		},
> 	) ;
> 
> 
> 	my $vfunc = $chooser{$vers}{$func} or
> 		die "bad version/func $vers/$func" ;
> 	$vfunc->() ;
> 
> no strict issues, no symbol table hacking, etc.

Well, the difference is really just whether you assign/reassign to
lexical coderefs (you) or package typeglobs (me).  Whether you use an
intermediate hash is immaterial.

I was going for a solution that does exactly what the OP had in mind:
Make it behave (for a while) as if "use Module_1" had been run, and
switch (at runtime) to behave as if it had been "use Module_2".  The
rest of the code can stay as it is.  Your solution requires changes at
the call site.  Cleanliness comes at a price :)

Anno


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

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


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