Re: smart matching

2011-10-06 Thread John SJ Anderson
On Thu, Oct 6, 2011 at 03:10, Shlomi Fish  wrote:
> Furthermore, I think you can only put when inside given and not arbitrary 
> code,
> and that "when()" operates on the datum that was given to given().

I don't think there's any restriction on what code can be inside the
'given' block -- at least, there's nothing in 'perlsyn' saying you
can't have other code in there.

You are correct that 'given()' sets up a (lexically scoped, I guess?)
$_ equal to the argument that it is given -- which is a copy, not an
alias -- and the smart matches in when() will then use that $_ -- but
if you have a 'when()' that doesn't check $_, then you're fine. See
sample code below.

Note: that is not say that any of this is *advisable* -- it's not,
unless you really really need to. (You don't really really need to, in
case you're wondering.) You're probably better off only having
'when()' blocks inside a 'given()' block, and you're probably better
off keeping the conditions in your 'when()' tests nice and simple.
You'll thank yourself down the road.

Sample code:

#! /usr/bin/env perl

use strict;
use warnings;
use feature 'switch';

$_ = 'outside';

my $foo = 'foo';

given( $foo ) {
  when( 1 == 1 ) { print "INIT\n" ; continue }

  when( 'bar' )  { print "BAR: $foo $_\n" }

  $foo = 'bar';

  when( 'foo' )  { print "FOO: $foo $_\n" }

  $foo = 'baz';

  when( 'outside' ) { print "OUT: $foo $_\n" }

  default { print "DEF: $foo $_\n" }
}

print;

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: smart matching

2011-10-06 Thread timothy adigun
Hi Chris,
 I think you will need to work on your code to make it better or maybe this
is just a dirty codes for practice. If not, there are some stuff you may
have to weed out...some of which Shlomi Fish mentioned.

However, to make your code work as you suppose, please check this little
addition and you can take it up from there.


#!/usr/bin/perl

use warnings;
use strict;
use 5.010;

say "Checking the number <$ARGV[0]>";

my $favorite = 62;

given( $ARGV[0] ) {
   when( ! /^\d+$/ ) { say "Not a number!" }

   my @divisors = divisors( $ARGV[0] );
my $val=2;   #added test value
   when( @divisors ~~  /$val/) { # 2 is in @divisors ,/$val/ check it
   say "$_ is even";
   continue;
   }

   when( !( @divisors ~~/$val/ ) ) { # 2 isn't in @divisors /$val/ check
it
   say "$_ is odd!";
   continue;
   }

   when( @divisors ~~ /$favorite/ ) { # /$favorite/ check it
   say "$_ is divisible by my favorite number";
   continue;
   }

   when( /$favorite/ ) { # $_ ~~ $favorite
   say "$_ is my favorite number";
   continue;
   }

   my @empty;
   when( @divisors ~~ @empty ) { say "Number is prime" }

   default { say "$_ is divisible by @divisors" }
   }

sub divisors {
   my $number = shift;

   my @divisors = ();
   foreach my $divisor ( 2 .. ($ARGV[0]/2 + 1) ) {
   push @divisors, $divisor unless $number % $divisor;
   }

return @divisors;
}


Regards,
timothy


Re: smart matching

2011-10-06 Thread Shlomi Fish
On Wed, 5 Oct 2011 23:44:21 -0500
Chris Stinemetz  wrote:

> trying to learn smart matching in an exercise.
> 
> Why does this program output "odd" when I input an even number?
> 

A few comments on your code.

> Thank you,
> 
> Chris
> 
> #!/usr/bin/perl
> 
> use warnings;
> use strict;
> 
> use 5.010;
> 
> say "Checking the number <$ARGV[0]>";
> 

You're using $ARGV[0] several times (duplicate code) and it's a positional
argument:

http://perl-begin.org/tutorials/bad-elements/#subroutine-arguments

Better unpack it as:

my $number = shift(@ARGV);

> my $favorite = 62;
> 
> given( $ARGV[0] ) {
> when( ! /^\d+$/ ) { say "Not a number!" }
> 
> my @divisors = divisors( $ARGV[0] );
> 

Returns arrays as references is preferable.

Furthermore, I think you can only put when inside given and not arbitrary code,
and that "when()" operates on the datum that was given to given().

> when( @divisors ~~ 2 ) { # 2 is in @divisors
> say "$_ is even";
> continue;
> }
> 
> when( !( @divisors ~~ 2 ) ) { # 2 isn't in @divisors
> say "$_ is odd!";
> continue;
> }
> 
> when( @divisors ~~ $favorite ) {
> say "$_ is divisible by my favorite number";
> continue;
> }
> 
> when( $favorite ) { # $_ ~~ $favorite
> say "$_ is my favorite number";
> continue;
> }
> 
> my @empty;
> when( @divisors ~~ @empty ) { say "Number is prime" }
> 
> default { say "$_ is divisible by @divisors" }
> }
> 

Your indentation is erratic.

> sub divisors {
> my $number = shift;
> 
> my @divisors = ();

No need for the «= ()» here. An array is initialised as empty by default.

> foreach my $divisor ( 2 .. ($ARGV[0]/2 + 1) ) {

Do you want to operate on "$number" or on "$ARGV[0]"?

> push @divisors, $divisor unless $number % $divisor;
> }
>

I got a "WTF?" moment when I saw this unless construct. I'd write it as:

if ($number % $divisor == 0)
{
push @divisors, $divisor;
}

Regards,

Shlomi Fish
 
> return @divisors;
> 



-- 
-
Shlomi Fish   http://www.shlomifish.org/
Stop Using MSIE - http://www.shlomifish.org/no-ie/

I used to be arrogant. Now I’m simply Perfect.
— one of Shlomi Fish’s relatives.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Smart matching

2011-01-18 Thread Vladimir D Belousov

19.01.2011 0:43, Brian Fraser пишет:

The smart match is no longer commutative - That was removed after 5.10.1, I
think.

http://www.learning-perl.com/?p=32
http://perldoc.perl.org/perlsyn.html#Smart-matching-in-detail

Brian.


Thank you!


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Smart matching

2011-01-18 Thread Brian Fraser
The smart match is no longer commutative - That was removed after 5.10.1, I
think.

http://www.learning-perl.com/?p=32
http://perldoc.perl.org/perlsyn.html#Smart-matching-in-detail

Brian.


Re: Smart matching

2011-01-18 Thread Vladimir D Belousov

19.01.2011 0:09, Uri Guttman пишет:
but why don't you just call exists on the hash key? there is no win to 
using smart matching for that. it is included for consistancy but it 
isn't needed for hash keys.




I just want to understand how does it work :)


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Smart matching

2011-01-18 Thread Uri Guttman
> "VDB" == Vladimir D Belousov  writes:

  VDB> I'm trying to check whether the given key exists in the hash.

smart matching is powerful and cool but why don't you just call exists
on the hash key? there is no win to using smart matching for that. it is
included for consistancy but it isn't needed for hash keys.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/