For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > March 2004 > easier way for $scalar = /regexp/ ???









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author easier way for $scalar = /regexp/ ???
Rocky

2004-03-27, 12:01 am

if ($_ =~ /^Job\sserver:\s+(.*)$/) { $servername = $1;}
isn't it possible to just say
$servername = ???
I can't seem to find a good example.
Thank you
Rocky Allen

David K. Wall

2004-03-27, 12:01 am

Rocky <PerlGuRu2b@bobotheclown.org> wrote:

> if ($_ =~ /^Job\sserver:\s+(.*)$/) { $servername = $1;}
> isn't it possible to just say
> $servername = ???
> I can't seem to find a good example.


my $servername = $1 if /^Job\sserver:\s+(.*)$/;

Richard Morse

2004-03-27, 12:01 am

In article <pan.2004.03.25.16.08.59.588839@bobotheclown.org>,
Rocky <PerlGuRu2b@bobotheclown.org> wrote:

> if ($_ =~ /^Job\sserver:\s+(.*)$/) { $servername = $1;}
> isn't it possible to just say
> $servername = ???
> I can't seem to find a good example.


According to the docs, in a scalar context, m// returns the number of
matches, but in a list context it returns the actual matches.

So this is what you're after:

my ($servername) = m/^Job\sserver:\s+(.*)$/;

By putting the variable name into the parens, we force in into list
context.

HTH,
Ricky
Rocky

2004-03-27, 12:01 am

On Thu, 25 Mar 2004 16:08:15 +0000, David K. Wall wrote:
Thanks.


> Rocky <PerlGuRu2b@bobotheclown.org> wrote:
>
>
> my $servername = $1 if /^Job\sserver:\s+(.*)$/;


Anno Siegel

2004-03-27, 12:01 am

Richard Morse <remorse@partners.org> wrote in comp.lang.perl.misc:
> In article <pan.2004.03.25.16.08.59.588839@bobotheclown.org>,
> Rocky <PerlGuRu2b@bobotheclown.org> wrote:
>
>
> According to the docs, in a scalar context, m// returns the number of
> matches, but in a list context it returns the actual matches.
>
> So this is what you're after:
>
> my ($servername) = m/^Job\sserver:\s+(.*)$/;
>
> By putting the variable name into the parens, we force in into list
> context.


This is indeed often preferable to using $1 etc. The original code had
a conditional, which is essential with the use of regexes. To restore
it:

if ( my ( $servername) = /.../ ) {
# use $servername here
}

That limits the scope of $servername to the if-block(s).

Anno
Jay Tilton

2004-03-27, 12:01 am

"David K. Wall" <dwall@fastmail.fm> wrote:

: Rocky <PerlGuRu2b@bobotheclown.org> wrote:
:
: > if ($_ =~ /^Job\sserver:\s+(.*)$/) { $servername = $1;}
: > isn't it possible to just say
: > $servername = ???
: > I can't seem to find a good example.
:
: my $servername = $1 if /^Job\sserver:\s+(.*)$/;

Doing it like that isn't a very good idea. From perlsyn:

NOTE: The behaviour of a "my" statement modified with a
statement modifier conditional or loop construct (e.g. "my $x
if ...") is undefined. The value of the "my" variable may be
"undef", any previously assigned value, or possibly anything
else. Don't rely on it. Future versions of perl might do
something different from the version of perl you try it out
on. Here be dragons.

Some alternatives:

my($servername) = /^Job\sserver:\s+(.*)$/;

my $servername = ( /^Job\sserver:\s+(.*)$/ )[0];

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com