Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

use Switch + __DATA__ = bye bye

14 réponses
Avatar
luc2
#!/usr/bin/perl

# bonjour a tous, le script suivant n'affichera rien
# pouvez-vous m'expliquer pourquoi ?

use Switch;

while( my $l = <DATA> )
{
print $l;
}

__DATA__
1
2
3
4
5

10 réponses

1 2
Avatar
Jean-Louis Morel
Le 06/09/2011 11:02, luc2 a écrit :
#!/usr/bin/perl

# bonjour a tous, le script suivant n'affichera rien
# pouvez-vous m'expliquer pourquoi ?

use Switch;

while( my $l =<DATA> )
{
print $l;
}

__DATA__
1
2
3
4
5



Le module Switch surcharge '__' (voir la doc).
Pour éviter ça, utiliser :

use Switch 'noimport';

(ce n'est pas dans la doc mais dans le code du module :-) )

HTH

J-L
--
http://www.bribes.org/perl/
Avatar
luc2
merci mon pote.
Avatar
Emmanuel Florac
Le Tue, 06 Sep 2011 09:02:37 +0000, luc2 a écrit:


use Switch;



Une remarque, pourquoi utiliser switch? son usage est déconseillé, car
dangereux et fragile, et à partir de perl 5.10 (qui est dispo même sur
les distros les plus conservatrices genre RedHat) il y a given/when qui
lui est sûr, sans danger et sans effets de bords merdiques.

tl,dr; ne pas utiliser switch, mais given/when.

--
Most software today is very much like an Egyptian pyramid with millions
of bricks piled on top of each other, with no structural integrity, but
just done by brute force and thousands of slaves.
Alan Kay
Avatar
luc2
merci pour vos conseils
Avatar
Klaus
On 6 sep, 21:11, Emmanuel Florac wrote:
Le Tue, 06 Sep 2011 09:02:37 +0000, luc2 a écrit:

> use Switch;

Une remarque, pourquoi utiliser switch? son usage est déconseillé, ca r
dangereux et fragile, et à partir de perl 5.10 (qui est dispo même su r
les distros les plus conservatrices genre RedHat) il y a given/when qui
lui est sûr, sans danger et sans effets de bords merdiques.

tl,dr; ne pas utiliser switch, mais given/when.



given/when n'est pas non plus sans problèmes.

Voici un cas de test:

use strict;
use warnings;
use 5.010;
use Test::More tests => 2;
for my $thing ('3', ' 3') {
my $test = sprintf '%d', $thing;
my $result = '';
given ($thing) {
when ('3') { $result .= "[3]"; continue; }
when (' 3') { $result .= "[ 3]"; continue; }
}
is ($result, "[$thing]", "Test [$thing]");

}

le test échoue car le smart match entre '3' et ' 3' est positif (il
devrait être négatif car '3' ne ' 3').
Avatar
Emmanuel Florac
Le Tue, 01 Nov 2011 06:47:36 -0700, Klaus a écrit:


le test échoue car le smart match entre '3' et ' 3' est positif (il
devrait être négatif car '3' ne ' 3').



Non, il doit être positif parce que c'est un smart match ~~, pas un
simple match eq ou ne. Vu que " 3" =~ "3", il n'y a pas d'erreur. C'est
plutôt parce que tu ne sais pas utiliser les regexp :)

Tu peux vérifier :

perl -e "print qq(matchn) if ( ' 3' =~ '3' )"


--
I love deadlines, I love the whooshing noise they make as they go by.
Douglas Adams
Avatar
espie
Le blog de chromatics dit d'ailleurs que finalement, given...when risque
plus d'etre une experience temporaire qu'une fonctionnalite qui va rester...
au contraire d'autres bouts plus triviaux comme say...
Avatar
Klaus
On 1 nov, 15:23, Emmanuel Florac wrote:
Le Tue, 01 Nov 2011 06:47:36 -0700, Klaus a écrit:

> le test échoue car le smart match entre '3' et ' 3' est positif (il
> devrait être négatif car '3' ne ' 3').

Non, il doit être positif parce que c'est un smart match ~~, pas un
simple match eq ou ne. Vu que " 3" =~ "3", il n'y a pas d'erreur. C'est
plutôt parce que tu ne sais pas utiliser les regexp :)

Tu peux vérifier :

perl -e "print qq(matchn) if ( ' 3' =~ '3' )"



En ce qui concerne les expression régulières, tu aurais raison si
j'avais utilisé ' 3' ~~ qr{3}. Dans ce cas le smart-match utilise les
expressions régulières.

Par contre, j'ai utilisé ' 3' ~~ '3' ce qui traduit dans le cas 32.
"Any Any" ==> string equality $a eq $b du tableau
http://perldoc.perl.org/perlsyn.html#Smart-matching-in-detail

1. $a $b Type of Match Implied Matching Code
2. ====== ===== =========== ========== =============
3. Any undef undefined !defined $a
4.
5. Any Object invokes ~~ overloading on $object, or dies
6.
7. Hash CodeRef sub truth for each key[1] !grep { !$b->($_) } keys %$a
8. Array CodeRef sub truth for each elt[1] !grep { !$b->($_) } @$a
9. Any CodeRef scalar sub truth $b->($a)
10.
11. Hash Hash hash keys identical (every key is found in both hashes)
12. Array Hash hash keys intersection grep { exists $b->{$_} } @$a
13. Regex Hash hash key grep grep /$a/, keys %$b
14. undef Hash always false (undef can't be a key)
15. Any Hash hash entry existence exists $b->{$a}
16.
17. Hash Array hash keys intersection grep { exists $a->{$_} } @$b
18. Array Array arrays are comparable[2]
19. Regex Array array grep grep /$a/, @$b
20. undef Array array contains undef grep !defined, @$b
21. Any Array match against an array element[3]
22. grep $a ~~ $_, @$b
23.
24. Hash Regex hash key grep grep /$b/, keys %$a
25. Array Regex array grep grep /$b/, @$a
26. Any Regex pattern match $a =~ /$b/
27.
28. Object Any invokes ~~ overloading on $object, or falls back:
29. Any Num numeric equality $a == $b
30. Num numish[4] numeric equality $a == $b
31. undef Any undefined !defined($b)
32. Any Any string equality $a eq $b
33.
34. 1 - empty hashes or arrays will match.
35. 2 - that is, each element smart-matches the element of same index in the
36. other array. [3]
37. 3 - If a circular reference is found, we fall back to referential eq uality.
38. 4 - either a real number, or a string that looks like a number
Avatar
Klaus
On 1 nov, 15:35, (Marc Espie) wrote:
Le blog de chromatics  dit d'ailleurs que finalement, given...when risq ue
plus d'etre une experience temporaire qu'une fonctionnalite qui va rester ...
au contraire d'autres bouts plus triviaux comme say...



Actuellement, il y a un développement en cours (Perl 5.14 / Perl 5.15)
qui essaie de corriger les problèmes de given/when par un nouveau
module:
voir https://groups.google.com/group/perl.perl5.porters/msg/c869d4e8b7fe5d8 8?hl=fr

> Jesse Vincent wrote:
> > I'd argue that yes, there is some progress. Jesse Luehrs did an initi al
> > pass at extracting smartmatch into a module and then implementing a
> > "less-smart" smartmatch designed by rjbs that could be swapped out
> > lexically.
[...]
Jesse Luehrs wrote:
> I posted links to p5p when i wrote it - the code currently lives at:
> https://github.com/doy/smartmatch
> https://github.com/doy/smartmatch-engine-core
> https://github.com/doy/smartmatch-engine-rjbs
> (and there are development releases of the first two on CPAN already).
Avatar
Emmanuel Florac
Le Tue, 01 Nov 2011 08:30:12 -0700, Klaus a écrit:


Actuellement, il y a un développement en cours (Perl 5.14 / Perl 5.15)
qui essaie de corriger les problèmes de given/when par un nouveau
module:
voir
https://groups.google.com/group/perl.perl5.porters/msg/c869d4e8b7fe5d88?


hl=fr

De ce que j'en comprends c'est plutôt le smart match qui pose problème.

--
Le commissaire : Comment vous appelez-vous?
Garance : Moi je ne m'appelle jamais, je suis toujours là. J'ai pas
besoin de m'appeler. Mais les autres m'appellent Garance, si ça peut
vous intéresser.
Prévert,"les enfants du Paradis".
1 2