[28983] in Perl-Users-Digest
Perl-Users Digest, Issue: 227 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Mar 15 16:10:20 2007
Date: Thu, 15 Mar 2007 13:09:20 -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: 227
Today's topics:
Re: anyone could explain this to me? <hjp-usenet2@hjp.at>
Re: anyone could explain this to me? anno4000@radom.zrz.tu-berlin.de
Re: anyone could explain this to me? <hjp-usenet2@hjp.at>
getting bash errors <usaims@yahoo.com>
Re: Getting the list of groups given a user id <hjp-usenet2@hjp.at>
Re: Getting the list of groups given a user id <hjp-usenet2@hjp.at>
Re: help for PostScript::Simple <paduille.4060.mumia.w+nospam@earthlink.net>
Re: help for PostScript::Simple <prawnMUNG@prawn.me.uk>
LWP credentials help <nalli718@gmail.com>
Re: LWP credentials help (Jamie)
Parse website that requires login <nalli718@gmail.com>
Re: Parse website that requires login <mark.clementsREMOVETHIS@wanadoo.fr>
Re: Perl DBI/XML processing versus PHP ? <hjp-usenet2@hjp.at>
what's wrong with this OR statement syntax <levinepw@yahoo.com>
what's wrong with this OR statement syntax <levinepw@yahoo.com>
Re: what's wrong with this OR statement syntax <jeremy@chaos.org.uk>
Re: what's wrong with this OR statement syntax <marora@gmail.com>
Re: what's wrong with this OR statement syntax anno4000@radom.zrz.tu-berlin.de
Re: what's wrong with this OR statement syntax <mark.clementsREMOVETHIS@wanadoo.fr>
Re: what's wrong with this OR statement syntax <damercer@comcast.net>
Re: what's wrong with this OR statement syntax <marora@gmail.com>
Re: what's wrong with this OR statement syntax <wahab-mail@gmx.de>
Re: what's wrong with this OR statement syntax <jurgenex@hotmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 15 Mar 2007 19:36:12 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: anyone could explain this to me?
Message-Id: <slrnevj4gs.sp0.hjp-usenet2@yoyo.hjp.at>
On 2007-03-15 17:51, Ted Zlatanov <tzz@lifelogs.com> wrote:
> 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
While I would have written that as:
for (`. /etc/Tivoli/setup_env.sh; env`) {
$ENV{$1} = $2 if (/^([^=]+)=(.*)$/);
}
that's not less complex, just more readable (IMHO). Can you think of a
less complex way to do that? (assuming that /etc/Tivoli/setup_env.sh may
contain non-trivial /bin/sh code)
hp
--
_ | Peter J. Holzer | Blaming Perl for the inability of programmers
|_|_) | Sysadmin WSR | to write clearly is like blaming English for
| | | hjp@hjp.at | the circumlocutions of bureaucrats.
__/ | http://www.hjp.at/ | -- Charlton Wilbur in clpm
------------------------------
Date: 15 Mar 2007 19:05:29 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: anyone could explain this to me?
Message-Id: <55tjnpF26hj09U1@mid.dfncis.de>
Peter J. Holzer <hjp-usenet2@hjp.at> wrote in comp.lang.perl.misc:
> On 2007-03-15 17:51, Ted Zlatanov <tzz@lifelogs.com> wrote:
> > 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
>
> While I would have written that as:
>
> for (`. /etc/Tivoli/setup_env.sh; env`) {
> $ENV{$1} = $2 if (/^([^=]+)=(.*)$/);
> }
>
> that's not less complex, just more readable (IMHO). Can you think of a
> less complex way to do that? (assuming that /etc/Tivoli/setup_env.sh may
> contain non-trivial /bin/sh code)
The regex could be simplified: /(.*)=(.*)/ would capture the same
things, given greediness and the structure of env output. I'd
probably use
my ( $name, $value) = split /=/, $_, 2;
$ENV{ $name} = $value;
Anno
------------------------------
Date: Thu, 15 Mar 2007 20:58:43 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: anyone could explain this to me?
Message-Id: <slrnevj9bj.vh5.hjp-usenet2@yoyo.hjp.at>
On 2007-03-15 19:05, anno4000@radom.zrz.tu-berlin.de <anno4000@radom.zrz.tu-berlin.de> wrote:
> Peter J. Holzer <hjp-usenet2@hjp.at> wrote in comp.lang.perl.misc:
>> On 2007-03-15 17:51, Ted Zlatanov <tzz@lifelogs.com> wrote:
>> > On 15 Mar 2007 08:28:39 -0700 "robertchen117@gmail.com"
>> > <robertchen117@gmail.com> wrote:
>> > rc> grep (do { chop; s/^([^=]+)=(.*)$/$ENV{$1}=$2/e},`.
>> > rc> /etc/Tivoli/setup_env.sh;env`);
[...]
>> > So basically it's an unnecessarily complex way to grab the environment
>> > resulting from /etc/Tivoli/setup_env.sh
>>
>> While I would have written that as:
>>
>> for (`. /etc/Tivoli/setup_env.sh; env`) {
>> $ENV{$1} = $2 if (/^([^=]+)=(.*)$/);
>> }
>>
>> that's not less complex, just more readable (IMHO). Can you think of a
>> less complex way to do that? (assuming that /etc/Tivoli/setup_env.sh may
>> contain non-trivial /bin/sh code)
>
> The regex could be simplified: /(.*)=(.*)/ would capture the same
> things, given greediness and the structure of env output.
Nope, but /(.*?)=(.*)/ would (and should work without excessive
backtracking). I admit that I haven't looked at the regex that much.
It seems pretty straightforward and rather simple, and I don't find
/(.*?)=(.*)/ less complex than /^([^=]+)=(.*)$/ (it's shorter, but we
aren't playing golf here).
> I'd probably use
>
> my ( $name, $value) = split /=/, $_, 2;
> $ENV{ $name} = $value;
Ok, split may be considered less complex than a capturing regex, even
with the 3rd parameter.
hp
--
_ | Peter J. Holzer | Blaming Perl for the inability of programmers
|_|_) | Sysadmin WSR | to write clearly is like blaming English for
| | | hjp@hjp.at | the circumlocutions of bureaucrats.
__/ | http://www.hjp.at/ | -- Charlton Wilbur in clpm
------------------------------
Date: 15 Mar 2007 13:03:06 -0700
From: "usaims" <usaims@yahoo.com>
Subject: getting bash errors
Message-Id: <1173988986.099034.304040@d57g2000hsg.googlegroups.com>
Hello:
I'm using the Net::SSH module. Its really a simple script. It supposed
to ssh into a node and do a 'df -k', but I'm getting:
bash: -c: line 1: syntax error near unexpected token `0x925c4c8'
bash: -c: line 1: `CODE(0x925c4c8)'
The above is a bash error, so I'm thinking PERL is not even invoked in
the subroutine, any help will be greatly appreciated.
################################################
#!/usr/bin/perl -w
use warnings;
use strict;
use Net::SSH qw(ssh issh sshopen2 sshopen3);
my $variable = \&TEST;
ssh('control@10.150.130.1', $variable);
sub TEST {
`df -h`;
}
------------------------------
Date: Thu, 15 Mar 2007 19:17:44 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Getting the list of groups given a user id
Message-Id: <slrnevj3e8.sp0.hjp-usenet2@yoyo.hjp.at>
On 2007-03-15 14:35, Josef Moellers <josef.moellers@fujitsu-siemens.com> wrote:
> arshad.tanveer@gmail.com wrote:
>> On Mar 15, 6:54 pm, Josef Moellers <josef.moell...@fujitsu-
>> siemens.com> wrote:
>>>arshad.tanv...@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?
[...]
>> 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).
Solaris likes to use NIS. LDAP is also quite popular these days.
So the text file case may be less usual than you think.
However, regardless on how the information is actually stored, using the
getpw* and getgr* functions should always work. To find all groups a
user belongs to, you have to get the primary group with getpw(uid|nam)
and then loop over all groups returned by getgrent and check for each
group whether the user is a member. AFAIK there is no faster portable
way to do this.
hp
--
_ | Peter J. Holzer | Blaming Perl for the inability of programmers
|_|_) | Sysadmin WSR | to write clearly is like blaming English for
| | | hjp@hjp.at | the circumlocutions of bureaucrats.
__/ | http://www.hjp.at/ | -- Charlton Wilbur in clpm
------------------------------
Date: Thu, 15 Mar 2007 19:19:33 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Getting the list of groups given a user id
Message-Id: <slrnevj3hl.sp0.hjp-usenet2@yoyo.hjp.at>
On 2007-03-15 14:40, Mumia W. <paduille.4060.mumia.w+nospam@earthlink.net> wrote:
> On 03/15/2007 08:47 AM, 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?
>
> Read about the "getgrent" function:
>
> Start->Run->"perldoc -f getgrent"
It's been some time that I've used Solaris, but I think the
"Start->Run->" part of your advice doesn't work there :-).
hp
--
_ | Peter J. Holzer | Blaming Perl for the inability of programmers
|_|_) | Sysadmin WSR | to write clearly is like blaming English for
| | | hjp@hjp.at | the circumlocutions of bureaucrats.
__/ | http://www.hjp.at/ | -- Charlton Wilbur in clpm
------------------------------
Date: Thu, 15 Mar 2007 19:00:53 GMT
From: "Mumia W." <paduille.4060.mumia.w+nospam@earthlink.net>
Subject: Re: help for PostScript::Simple
Message-Id: <FJgKh.127786$_73.98763@newsread2.news.pas.earthlink.net>
On 03/15/2007 10:52 AM, Huub wrote:
> Mumia W. wrote:
>> [...]
>> 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.
You're welcome. That's what usenet is for.
Also try this:
Start->Run->"perldoc perl"
------------------------------
Date: Thu, 15 Mar 2007 19:09:10 +0000
From: prawn <prawnMUNG@prawn.me.uk>
Subject: Re: help for PostScript::Simple
Message-Id: <m7ioc4-f0c.ln1@eddie.ryet.co.uk>
On Thu, 15 Mar 2007 19:00:53 +0000, Mumia W. wrote:
> On 03/15/2007 10:52 AM, Huub wrote:
>> Mumia W. wrote:
>>> [...]
>>> 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.
>
> You're welcome. That's what usenet is for.
>
> Also try this:
>
> Start->Run->"perldoc perl"
What is this "Start->Run" of which you speak? :-)
--
p BotM#1 LotR#9
------------------------------
Date: 15 Mar 2007 11:52:48 -0700
From: "Nalli" <nalli718@gmail.com>
Subject: LWP credentials help
Message-Id: <1173984768.503775.59350@n76g2000hsh.googlegroups.com>
use LWP:
$url = 'https://www.something.com/somethingelse/whatever' ;
$ua = LWP::UserAgent->new;
$ua->timeout(45);
$ua->env_proxy;
$ua->agent("Mozilla/5.01 (Windows; U; NT4.0; en-us) Gecko/
25250101");
$ua->credentials( $url, $url, "username", "password" );
$response = $ua->get($url);
if ($response->is_success)
{
print $html;
}
In order to access $url a username and password are required. The
problem is that I dont know hwo to pass the credentials in order to
successfully access the page. Can someone please tell me how to pass
my login information?
Thank you in advance.
------------------------------
Date: Thu, 15 Mar 2007 19:27:36 GMT
From: nospam@geniegate.com (Jamie)
Subject: Re: LWP credentials help
Message-Id: <Lc117396687755490x8bf8f5c@pong.podro.com>
In <1173984768.503775.59350@n76g2000hsh.googlegroups.com>,
"Nalli" <nalli718@gmail.com> mentions:
>use LWP:
> $url = 'https://www.something.com/somethingelse/whatever' ;
>
> $ua = LWP::UserAgent->new;
> $ua->timeout(45);
> $ua->env_proxy;
> $ua->agent("Mozilla/5.01 (Windows; U; NT4.0; en-us) Gecko/
>25250101");
> $ua->credentials( $url, $url, "username", "password" );
> $response = $ua->get($url);
>
> if ($response->is_success)
> {
> print $html;
> }
>
>In order to access $url a username and password are required. The
>problem is that I dont know hwo to pass the credentials in order to
>successfully access the page. Can someone please tell me how to pass
>my login information?
The way I've done this in the past was to override LWP::UserAgent
and supply my own get_basic_credentials method. Then, from there
you can either just return them, prompt the user for them, look them
up in a database, etc.. Seems a little involved at first, but it's
not that bad.
Jamie
--
http://www.geniegate.com Custom web programming
Perl * Java * UNIX User Management Solutions
------------------------------
Date: 15 Mar 2007 12:05:46 -0700
From: "Nalli" <nalli718@gmail.com>
Subject: Parse website that requires login
Message-Id: <1173985546.549048.203710@e65g2000hsc.googlegroups.com>
I need to know how to login to a website using perl and then be able
to parse any page on the site, once logged in. I would appreciate it
very much if someone could post a working example. The website uses a
form to login. Below is some html from the site.
div id="loginarea">
<div class="top_margin" align="center"><div class="main_box"
style="width: 450px;">
<form action="/loginauth" method="post" name="login" onsubmit="return
submit_it();">
<input type="hidden" name="rurl" value="">
<input type="hidden" name="m" value="">
<span class="main_title">Welcome </span><br />
<br />
<table>
<tr><td>Username:</td><td><input type="text" name="username"
style="width: 200px;" maxlength="50" value=""></td></tr>
<tr><td>Password:</td><td><input type="password" name="password"
value="" style="width: 200px;" maxlength="25"></td></tr>
<tr><td></td><td><p style="font-size: 11px;"><input type="checkbox"
name="keep_me_logged_in" value="1" /> Remember me on this Computer</p>
<p style="padding-bottom: 15px; "><input type="submit" id="signin"
value="» Sign In" class="button"></p>
<a href="forgotpw?rurl=">Forget your Username or Password?</a><br />
------------------------------
Date: Thu, 15 Mar 2007 20:25:47 +0100
From: Mark Clements <mark.clementsREMOVETHIS@wanadoo.fr>
Subject: Re: Parse website that requires login
Message-Id: <45f99dbb$0$5067$ba4acef3@news.orange.fr>
Nalli wrote:
> I need to know how to login to a website using perl and then be able
> to parse any page on the site, once logged in. I would appreciate it
> very much if someone could post a working example. The website uses a
> form to login. Below is some html from the site.
>
<snip>
Check out
WWW::Mechanize
Mark
------------------------------
Date: Thu, 15 Mar 2007 19:54:25 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Perl DBI/XML processing versus PHP ?
Message-Id: <slrnevj5j1.sp0.hjp-usenet2@yoyo.hjp.at>
On 2007-03-15 11:00, Jamie <nospam@geniegate.com> wrote:
> Seems to me, perl DBI is a bit more intuitive. Last time I looked,
> php didn't really have placeholders (well, it did, depending on which
> version of php was available where)
>
> This was awhile ago, but.. in mysql, placeholders didn't help much.
That depends on what they should help you with. They always help making
your code more robust and secure. "SQL injection" just isn't an issue
if you use placeholders. They may or may not help improving performance.
(Recent versions of MySQL do support placeholders, BTW, and DBD::mysql
can use them (the default is still to interpolate them in the DBD)).
hp
--
_ | Peter J. Holzer | Blaming Perl for the inability of programmers
|_|_) | Sysadmin WSR | to write clearly is like blaming English for
| | | hjp@hjp.at | the circumlocutions of bureaucrats.
__/ | http://www.hjp.at/ | -- Charlton Wilbur in clpm
------------------------------
Date: 15 Mar 2007 11:34:56 -0700
From: "levinepw@yahoo.com" <levinepw@yahoo.com>
Subject: what's wrong with this OR statement syntax
Message-Id: <1173983696.037565.321420@y80g2000hsf.googlegroups.com>
Hi,
This code does not work:
----------------------------------------------
my $color='red';
if($color eq ('blue' || 'red' || 'green'))
{
print "cool\n";
}
----------------------------------------------
I wanted a shorthand to
my $color='red';
if($color eq 'blue' || $color eq 'red' || $color eq 'green'))
{
print "cool\n";
}
Can someone tell me why my attempt doesn't work & if there is a
simpler way to write the statement.
Thanks
------------------------------
Date: 15 Mar 2007 11:34:58 -0700
From: "levinepw@yahoo.com" <levinepw@yahoo.com>
Subject: what's wrong with this OR statement syntax
Message-Id: <1173983698.876873.172910@l75g2000hse.googlegroups.com>
Hi,
This code does not work:
----------------------------------------------
my $color='red';
if($color eq ('blue' || 'red' || 'green'))
{
print "cool\n";
}
----------------------------------------------
I wanted a shorthand to
my $color='red';
if($color eq 'blue' || $color eq 'red' || $color eq 'green'))
{
print "cool\n";
}
Can someone tell me why my attempt doesn't work & if there is a
simpler way to write the statement.
Thanks
------------------------------
Date: Thu, 15 Mar 2007 13:45:24 -0500
From: Jeremy Henty <jeremy@chaos.org.uk>
Subject: Re: what's wrong with this OR statement syntax
Message-Id: <slrnevj4v0.2du.jeremy@omphalos.onepoint>
On 2007-03-15, levinepw@yahoo.com <levinepw@yahoo.com> wrote:
> ----------------------------------------------
> my $color='red';
>
> if($color eq ('blue' || 'red' || 'green'))
> {
> print "cool\n";
> }
> ----------------------------------------------
> [...]
> Can someone tell me why my attempt doesn't work ...
Because 'blue' || 'red' || 'green' is an expression whose value is
'blue', so your code is equivalent to "if($color eq 'blue') { ...".
> & if there is a simpler way to write the statement.
My first thought would be to put the values in a hash and look them
up.
my %colors = ( blue => 1, red => 1, green => 1 );
if ($colors{$color}) { ...
There's probably a cooler way to do it but I wouldn't know, I'm not
even JAPH.
Regards,
Jeremy Henty
------------------------------
Date: 15 Mar 2007 11:49:07 -0700
From: "Manish" <marora@gmail.com>
Subject: Re: what's wrong with this OR statement syntax
Message-Id: <1173984547.083775.182090@p15g2000hsd.googlegroups.com>
[snip]
You may use "if ($color =~/[red|blue|green]/)" as a shorthand instead.
Regards,
Manish
------------------------------
Date: 15 Mar 2007 18:50:35 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: what's wrong with this OR statement syntax
Message-Id: <55tirrF25obqnU1@mid.dfncis.de>
levinepw@yahoo.com <levinepw@yahoo.com> wrote in comp.lang.perl.misc:
> Hi,
>
> This code does not work:
>
> ----------------------------------------------
> my $color='red';
>
> if($color eq ('blue' || 'red' || 'green'))
> {
> print "cool\n";
> }
> ----------------------------------------------
>
> I wanted a shorthand to
>
> my $color='red';
>
> if($color eq 'blue' || $color eq 'red' || $color eq 'green'))
> {
> print "cool\n";
> }
>
> Can someone tell me why my attempt doesn't work & if there is a
> simpler way to write the statement.
What makes you think it should work?
Just because a short-hand is common in everyday mathematical notation
doesn't mean it is common in programming languages. In fact, there
are few, if any, languages that implement that. Perl's approximation
to such a notation is in the module Quantum::Superpositions, available
on CPAN.
For an ad-hoc approach you could set up a hash, like this:
my %is_cool_color = map +( $_ => 1), qw( blue red green);
Then you can say
if ( $is_cool_color{ $color} ) {
print "cool\n";
}
and similar.
Anno
------------------------------
Date: Thu, 15 Mar 2007 19:56:36 +0100
From: Mark Clements <mark.clementsREMOVETHIS@wanadoo.fr>
Subject: Re: what's wrong with this OR statement syntax
Message-Id: <45f996e4$0$25917$ba4acef3@news.orange.fr>
Manish wrote:
> [snip]
>
> You may use "if ($color =~/[red|blue|green]/)" as a shorthand instead.
>
You haven't tested this.
mark@owl:~$ cat testre2.pl
#!/usr/bin/perl
use strict;
use warnings;
while(my $item = <DATA>){
chomp $item;
my $match = $item =~ /[red|blue|green]/?"yes":"no";
print "$item => $match\n";
}
__END__
blue
red
green
white
yellow
mark@owl:~$ perl testre2.pl
blue => yes
red => yes
green => yes
white => yes
yellow => yes
/^(red|blue|green)$/
works as the regex.
Mark
------------------------------
Date: Thu, 15 Mar 2007 14:06:03 -0500
From: "Dan Mercer" <damercer@comcast.net>
Subject: Re: what's wrong with this OR statement syntax
Message-Id: <GYOdnSv5IfaABGTYnZ2dnUVZ_h6vnZ2d@comcast.com>
"Manish" <marora@gmail.com> wrote in message news:1173984547.083775.182090@p15g2000hsd.googlegroups.com...
: [snip]
:
: You may use "if ($color =~/[red|blue|green]/)" as a shorthand instead.
That doesn't do what you think it does. Oh, it matches red, blue and
green alright. It also matches black and '|'. You have set up a
character class so that $color matches if it contains any
character in the class. What you want is:
if ($color =~ /^(red|blue|green)$/)
You have to anchor the expression so it only matches red or blue or green.
The choices have to be grouped by parentheses, because /^red|blue|green$/
matches any string(e.g. redolent) that begins with red, any string that
contains blue and any string that ends in green.
Dan Mercer
:
: Regards,
: Manish
:
------------------------------
Date: 15 Mar 2007 12:15:26 -0700
From: "Manish" <marora@gmail.com>
Subject: Re: what's wrong with this OR statement syntax
Message-Id: <1173986125.972590.250540@y66g2000hsf.googlegroups.com>
On Mar 15, 2:06 pm, "Dan Mercer" <damer...@comcast.net> wrote:
> "Manish" <mar...@gmail.com> wrote in messagenews:1173984547.083775.182090@p15g2000hsd.googlegroups.com...
>
> : [snip]
> :
> : You may use "if ($color =~/[red|blue|green]/)" as a shorthand instead.
>
> That doesn't do what you think it does. Oh, it matches red, blue and
> green alright. It also matches black and '|'. You have set up a
> character class so that $color matches if it contains any
> character in the class. What you want is:
>
> if ($color =~ /^(red|blue|green)$/)
>
> You have to anchor the expression so it only matches red or blue or green.
> The choices have to be grouped by parentheses, because /^red|blue|green$/
> matches any string(e.g. redolent) that begins with red, any string that
> contains blue and any string that ends in green.
>
> Dan Mercer
>
> :
> : Regards,
> : Manish
> :
Yeap, my mistake. Thanks for pointing it out.
------------------------------
Date: Thu, 15 Mar 2007 20:14:50 +0100
From: Mirco Wahab <wahab-mail@gmx.de>
Subject: Re: what's wrong with this OR statement syntax
Message-Id: <etc68t$c58$1@mlucom4.urz.uni-halle.de>
levinepw@yahoo.com wrote:
> I wanted a shorthand to
> my $color='red';
> if($color eq 'blue' || $color eq 'red' || $color eq 'green'))
> {
> print "cool\n";
> }
> Can someone tell me why my attempt doesn't work & if there is a
> simpler way to write the statement.
There have been some examples,
so I'll add another one:
...
sub IS { grep /\b$_[0]\b/, @{$_[1]} }
sub IN { [@_] }
my $color='red';
if( IS $color, IN qw'blue red green' ) {
print "cool\n";
}
...
Regards
Mirco
------------------------------
Date: Thu, 15 Mar 2007 19:27:41 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: what's wrong with this OR statement syntax
Message-Id: <N6hKh.11259$Hb6.7056@trndny03>
levinepw@yahoo.com wrote:
> This code does not work:
It does work. It just doesn't do what you seem to expect it do to.
> ----------------------------------------------
> my $color='red';
> if($color eq ('blue' || 'red' || 'green'))
> {
> print "cool\n";
> }
>
> Can someone tell me why my attempt doesn't work
Trivial. You compute the boolean or of some string values.
The boolean value of each of your strings is a logical true. That means your
'or()' returns a true and basically you are testing
if ($color eq '1') {...
because 1 is the textual representation of the logical value true.
> & if there is a
> simpler way to write the statement.
Please define simpler. If you put your 'blue', 'red', and 'green' in an
array, then you can use grep() to test if $color is in that array. A much
better approach is explained in the FAQ: "How can I tell whether a list or
array contains a certain element?"
jue
------------------------------
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 227
**************************************