Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

is this horrible?

by fionbarr (Friar)
on Feb 16, 2015 at 16:21 UTC ( [id://1116889]=perlquestion: print w/replies, xml ) Need Help??

fionbarr has asked for the wisdom of the Perl Monks concerning the following question:

$os = $1 if ( $description =~ /(windows)/i ); $os = $1 if ( $description =~ /(linux)/i ); $os = $1 if ( $description =~ /(solaris)/i );
I know it is inefficient but I'm not sure it's worth 'if/elseif' block. Comments?

Replies are listed 'Best First'.
Re: is this horrible?
by hippo (Bishop) on Feb 16, 2015 at 16:23 UTC
    $os = $1 if ( $description =~ /(windows|linux|solaris)/i );
      Better yet if $os has no previous value:
      my ($os) = $description =~ /(windows|linux|solaris)/i;
      of course! thanks
Re: is this horrible?
by Perlbotics (Archbishop) on Feb 16, 2015 at 16:32 UTC

    Alternative without statement modifier and with a default ($os always defined):

    ($os) = ($description =~ /(windows|linux|solaris)/i , 'n/a');
    Update: Default could be $^O instead of 'n/a', though.

Re: is this horrible?
by choroba (Cardinal) on Feb 16, 2015 at 16:25 UTC
    "Horrible" is probably a too strong word. I'd use an alternative in the regex:
    $os = $1 if $description =~ /(windows|linux|solaris)/i;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Nice, however it is not the same as the code of the OP. Consider the following code:

      my $description = "windows and linux and solaris"; $os = $1 if ( $description =~ /(windows)/i ); $os = $1 if ( $description =~ /(linux)/i ); $os = $1 if ( $description =~ /(solaris)/i ); print $os; $os = $1 if $description =~ /(windows|linux|solaris)/i; print " $os\n";
      Rata (knowing that this special case is probably excluded by an unwritten part of the spec)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1116889]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-16 19:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found