Use \Q...\E to escape regular expression special characters, like the any character '.'
Also use word boundaries \b so that you don't match a substring like 1.1.1.100
use strict;
use warnings;
my $str = "group=1,101,102";
my $ip = "1.1.1.1";
if ($str =~ /\b\Q$ip\E\b/) {
print "The string...
The easiest solution is to download it to a subdirectory of cgi-bin, and then remove access from that subdirectory using .htaccess.
Alternatively, you can use relative paths to access a directory outside of cgi-bin, but you'll of course have to set permissions right, which can be a challenge...
While it may not be helpful to point out, I must say that this code is very sloppy. It doesn't even appear to have use strict and use warnings turned on as none of the variables are declared with my. This does not make me optimistic toward the effectiveness and bug-free nature of this code...
As of Perl 5.18, smart match is experimental: "It is clear that smartmatch is almost certainly either going to change or go away in the future. Relying on its current behavior is not recommended."
- Miller
As feherke has already stated, you should simplify your logic by taking advantage of the fact that you can execute code in the right side of a regex to selectively replace things. This is pretty close to what he's provided with just a couple small differences:
use strict;
use warnings;
my...
This should give you what you desire.
my @defined_vars = grep {defined $_} ($x, $y, $z);
if (@defined_vars && ! grep {$_ ne $defined_vars[0]} @defined_vars) {
print "All Vars equal each other if they are defined\n";
}
The only special case that you didn't specify is what should happen...
Quick answer: Yes, that is normal.
Slightly longer answer: Just because your code includes "use strict;", does not mean that the external modules enforce that pragma as well. They can very easily do an explicit "no strict;" for the own scope.
I've attempted to get rid of all of those...
Looks like you just need to be introduced to arrays and the foreach loop:
#!/usr/bin/perl
use strict;
use warnings;
# Grocery store inventory:
my $lines = <<'END_OF_REPORT';
0.95 300 White Peaches
1.45 120 California Avacados
5.50 10 Durian
0.40 700 Spartan Apples
END_OF_REPORT
my...
On a side note, I would advise you to remove the 'g' modifier, as global search and replace does not make sense in this situation.
Also, if you really want to be hardcore, add a boundary condition since you want this to be removed from the front of the line only.
$sentence =~...
The most reliable way to solve this type of problem is to break it up. Yes, you can write the solution to this in a single regular expression. However, the more complicated the regex, the more likely you are to overlook cases or introduce a bug.
Therefore, use non-greedy matching to capture...
The following will get you part of the way there. You'll still have to do the parsing and potentially install the DateTime module on your system:
use DateTime;
use strict;
use warnings;
my $dt = DateTime->new(
year => 2011,
month => 1,
day => 1,
);
$dt->add(days => 266);
print...
You don't need a regex for this, just a queue that saves the lines with the current minimum value for the current group. If you can assume the lines are ordered, you could simplify the below logic, but the following will work.
#!/usr/bin/perl -w
use strict;
use warnings;
my @lines;
my $min...
You need to either install them using strawberry perl's cpan.
cpan -i Text::CSV
Or you can just download the .pm's yourself and put them in your source tree.
Also, I have to mention that perl 5.8 is legacy now so I have to encourage you to consider upgrading to perl 5.12.
- Miller
I need to second the recommendation for Text::CSV
my $csv = Text::CSV->new({
binary => 1,
eol => "\n",
quote_char => q{"},
}) or die "Cannot use CSV: ".Text::CSV->error_diag();
Even if you're worried about being able to include it on thousands of machines, just rely on the pure...
Why do arrays and hashes need to be mutually exclusive. Use both!
# In loop for loading the files
push @files, {
id => $id,
name => $name,
size => $size,
};
Assuming this isn't an assignment, I'd agree that rsync would work just fine.
Good luck,
- Miller
I second the recommendation if not insistence that you include use strict; and use warnings; at the beginning of every script. It will require adjustment of your programming style, but it will save you so much headache in the end and make it more likely that you'll get assistance in forums when...
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.