Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^3: regex basics

by CountZero (Bishop)
on Feb 02, 2015 at 13:12 UTC ( [id://1115287]=note: print w/replies, xml ) Need Help??


in reply to Re^2: regex basics
in thread regex basics

Modern::Perl is indeed for more recent versions of Perl. I'm running Perl 5.18. You can safely delete it in this script and add use strict; use warnings; in its place.

Those blank lines you see are mysterious. There shouldn't be any such lines as nothing is printed when a comment is found.

!$_ /;\s+/ is invalid syntax and confused the Perl-interpreter hence the rather unhelpful error message.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

Replies are listed 'Best First'.
Re^4: regex basics
by jott19 (Initiate) on Feb 02, 2015 at 14:01 UTC

    CountZero, did you say !$_ /;\s+/ is invalid syntax?

    And here I was all ready to use this on other config files. If fact, I already have:

    #!/usr/bin/env perl use strict; use warnings; my $conforig = 'httpd.conf-orig'; my $confnocm = 'httpd.conf-nocomments'; open my $IN, '<', $conforig or die "Could not open $conforig for reading: $!"; my $text; while (<$IN>) { if (!/\#\s+/) { $text .= $_; } } close $IN or die "Error closing $conforig: $!"; # remove blank lines $text =~ s{\n{2,}}{\n}gm; open my $OUT, '>', $confnocm or die "Could not open $confnocm for writing: $!"; print $OUT $text; close $OUT or die "Error closing $confnocm: $!";
    It seems to work... but please show me the correct negation syntax before the code police smash in my door, drag me off to a detention camp and force me to write JavaScript.
      These are not the same:
      !$_ /;\s+/ !/\#\s+/

      In fact, when you want to mention the thing to be matched, you must use the binding operator:

      !( $_ =~ /;\s+/); # or $_ !~ /;\s+/;

      Update: Precedence fixed. Thanks AnomalousMonk.

      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        ! $_ =~ /;\s+/;

        Because of the high precedence of the  ! (logical-not) operator versus  =~ (see perlop), the regex actually matches against either  '' (the empty string) or '1'.

        c:\@Work\Perl>perl -wMstrict -MO=Deparse,-p -le "! $_ =~ /;\s+/; " BEGIN { $^W = 1; } BEGIN { $/ = "\n"; $\ = "\n"; } use strict 'refs'; ((!$_) =~ /;\s+/); -e syntax OK
        These are equivalent:
        ! ($_ =~ /;\s+/); # or $_ !~ /;\s+/;


        Give a man a fish:  <%-(-(-(-<

        Understood.

        =~ means match and !~ means no match.

        This should work:

        #!/usr/bin/env perl use strict; use warnings; my $iniprod = 'php.ini-production'; my $ininocm = 'php.ini-nocomments'; open my $IN, '<', $iniprod or die "Could not open $iniprod for reading: $!"; my $text; while (<$IN>) { if ($_ !~ /;\s+/) { $text .= $_; } } close $IN or die "Error closing $iniprod: $!"; # remove blank lines $text =~ s{\n{2,}}{\n}gm; open my $OUT, '>', $ininocm or die "Could not open $ininocm for writing: $!"; print $OUT $text; close $OUT or die "Error closing $ininocm: $!";

Log In?
Username:
Password:

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

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

    No recent polls found