Rocksolid Light

groups  faq  privacy  How to post  login

Message-ID:  

You are confused; but this is your normal state.


rocksolid / de.comp.lang.perl / Re: use constant, qr// und Pattern-Matching

SubjectAuthor
* use constant, qr// und Pattern-MatchingChristian Garbs
`* Re: use constant, qr// und Pattern-MatchingThomas Dorner
 `- Re: use constant, qr// und Pattern-MatchingChristian Garbs

1
Subject: use constant, qr// und Pattern-Matching
From: Christian Garbs
Newsgroups: de.comp.lang.perl
Organization: ya mine owne server
Date: Thu, 28 Sep 2023 07:32 UTC
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.cgarbs.de!yggdrasil.dn.cgarbs.de!.POSTED.localhost!not-for-mail
From: mit...@cgarbs.de (Christian Garbs)
Newsgroups: de.comp.lang.perl
Subject: use constant, qr// und Pattern-Matching
Date: Thu, 28 Sep 2023 07:32:52 -0000 (UTC)
Organization: ya mine owne server
Message-ID: <uf3a74$ntvi$1@yggdrasil.dn.cgarbs.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 28 Sep 2023 07:32:52 -0000 (UTC)
Injection-Info: yggdrasil.dn.cgarbs.de; posting-host="localhost:127.0.0.1";
logging-data="784370"; mail-complaints-to="mitch@cgarbs.de"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.1.0-12-amd64 (x86_64))
Cancel-Lock: sha1:UYCKaKT/ON+ylN3p7RbZ+hikqy0=
View all headers

Mahlzeit!

Ich benutzte gerne 'use constant', um übersichtlich am Anfang meiner
Skripte Dinge zu deklarieren. Gerade habe ich das das erste Mal für
ein Pattern benutzt.

Grundsätzlich funktioniert das wie erwartet:

#!usr/bin/perl
use constant PATTERN => qr/foo/;
print "matched\n" if "foobarfoobarfoo" =~ PATTERN;

Wenn ich allerdings //g zum Matchen benutzen will, weiß ich nicht, wie
ich das g dort unterkriegen soll:

- PATTERNg ist offensichtlicher Unfug
- /PATTERN/g sucht nach dem Vorkommen von 'PATTERN', statt die
Konstante aufzulösen.

- Die üblichen Varianten für Konstanten wie PATTERN() (als
Methodenaufruf) und +PATTERN (um es als Hash-Key zu benutzen)
funktionieren hier auch nicht.

In allen Fällen wird der String PATTERN als RegExp interpretiert,
das kann ich Perl nicht mal verübeln.

Als Umgehungslösung kann ich die Konstante in die eine Variable
$pattern kopieren, dann funktioniert es:

#!/usr/bin/perl
use constant PATTERN => qr/foo/;
my $pattern = PATTERN;
my $found = 0;
$found++ while "foobarfoobarfoo" =~ /$pattern/g;
print "found $found matches\n"

Geht das irgendwie eleganter, so dass ich das PATTERN direkt nutzen
kann, ohne es vorher nochmal als $pattern zu doppeln?

Gruß
Christian
--
.....Christian.Garbs....................................https://www.cgarbs.de
Booting vmemacs20... done.
Emacs v20.99: loading linux.el

Subject: Re: use constant, qr// und Pattern-Matching
From: Thomas Dorner
Newsgroups: de.comp.lang.perl
Date: Thu, 28 Sep 2023 11:19 UTC
References: 1
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!reader5.news.weretis.net!news.solani.org!.POSTED!umbra.opacus!not-for-mail
From: dclp2309...@spamgourmet.com (Thomas Dorner)
Newsgroups: de.comp.lang.perl
Subject: Re: use constant, qr// und Pattern-Matching
Date: Thu, 28 Sep 2023 13:19:04 +0200
Message-ID: <6ev8buo89j.fsf@th-dorner.de>
References: <uf3a74$ntvi$1@yggdrasil.dn.cgarbs.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: solani.org;
logging-data="758797"; mail-complaints-to="abuse@news.solani.org"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:GLqHLOSFHLIp5YFxy5AoCBT+HZo= sha1:4SSRD65Yn/0EhJ8aTJgd5EaZVIw=
X-User-ID: eJwNyMERwDAIA7CVasAk67Qm3n+E5qcTs9Fa1eyiaa+JnT6abUC6HqdQ8fHcOsVJdAjvEyX/NXARug==
View all headers

Christian Garbs <mitch@cgarbs.de> writes:
> Geht das irgendwie eleganter, so dass ich das PATTERN direkt nutzen
> kann, ohne es vorher nochmal als $pattern zu doppeln?

Sehr gute Frage, die Antwort(en) kannte ich bis eben auch noch nicht.
Es gibt sogar drei Möglichkeiten (typisch Perl: "There's more than one
way to do it." ;-):

#v+

#!usr/bin/perl
use constant PATTERN => qr/foo/;
my $pattern = PATTERN;
print "#1 matched\n" if "foobarfoobarfoo" =~ PATTERN;
print "#2 matched\n" if "foobarfoobarfoo" =~ m/$pattern/g;
print "#3 matched\n" if "foobarfoobarfoo" =~ m/${\PATTERN}/g;
print "#4 matched\n" if "foobarfoobarfoo" =~ m/(?{PATTERN})/g;
print "#5 matched\n" if "foobarfoobarfoo" =~ m/@{[PATTERN]}/g;

#v-

Viele Grüße, Thomas
--
Adresse gilt nur kurzzeitig!

Subject: Re: use constant, qr// und Pattern-Matching
From: Christian Garbs
Newsgroups: de.comp.lang.perl
Organization: ya mine owne server
Date: Fri, 29 Sep 2023 19:40 UTC
References: 1 2
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.cgarbs.de!yggdrasil.dn.cgarbs.de!.POSTED.localhost!not-for-mail
From: mit...@cgarbs.de (Christian Garbs)
Newsgroups: de.comp.lang.perl
Subject: Re: use constant, qr// und Pattern-Matching
Date: Fri, 29 Sep 2023 19:40:11 -0000 (UTC)
Organization: ya mine owne server
Message-ID: <uf796r$lq7h$1@yggdrasil.dn.cgarbs.de>
References: <uf3a74$ntvi$1@yggdrasil.dn.cgarbs.de> <6ev8buo89j.fsf@th-dorner.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 29 Sep 2023 19:40:11 -0000 (UTC)
Injection-Info: yggdrasil.dn.cgarbs.de; posting-host="localhost:127.0.0.1";
logging-data="714993"; mail-complaints-to="mitch@cgarbs.de"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.1.0-12-amd64 (x86_64))
Cancel-Lock: sha1:QsVf8I3qmhz0hvcxvxIOVWBk2KA=
View all headers

Mahlzeit!

Thomas Dorner <dclp230928.dorner@spamgourmet.com> wrote:
> Christian Garbs <mitch@cgarbs.de> writes:

>> Geht das irgendwie eleganter, so dass ich das PATTERN direkt nutzen
>> kann, ohne es vorher nochmal als $pattern zu doppeln?
>
> Sehr gute Frage, die Antwort(en) kannte ich bis eben auch noch nicht.
> Es gibt sogar drei Möglichkeiten (typisch Perl: "There's more than one
> way to do it." ;-):
>
> #v+
>
> #!usr/bin/perl
> use constant PATTERN => qr/foo/;
> my $pattern = PATTERN;
> print "#1 matched\n" if "foobarfoobarfoo" =~ PATTERN;
> print "#2 matched\n" if "foobarfoobarfoo" =~ m/$pattern/g;
> print "#3 matched\n" if "foobarfoobarfoo" =~ m/${\PATTERN}/g;
> print "#4 matched\n" if "foobarfoobarfoo" =~ m/(?{PATTERN})/g;
> print "#5 matched\n" if "foobarfoobarfoo" =~ m/@{[PATTERN]}/g;
>
> #v-

Da haben wir alle was gelernt, großartig :)

Die Varianten m/${\PATTERN}/g und m/@{[PATTERN]}/g verstehe ich sogar,
da wird eine Referenz erstellt und direkt wieder dereferenziert.

Das Konstrukt (?{}) in einer RegExp war mir neu, das ist ja quasi das
Gegenstück zu ///e für die Pattern-Seite.

Ich hoffe, ich kriege jetzt keine Haue: Das ist alles toll, aber in
einem Jahr kann ich das nicht mehr entziffern, ich bin bei der
Variante mit $pattern verblieben…

Vielen Dank!
Christian
--
.....Christian.Garbs....................................https://www.cgarbs.de
gsenedt vno miemen iPghone !


rocksolid / de.comp.lang.perl / Re: use constant, qr// und Pattern-Matching

1
server_pubkey.txt

rocksolid light 0.9.136
clearnet tor