CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Using \? in a variable

 

First page Previous page 1 2 Next page Last page  View All


s660117
User

Oct 13, 2012, 2:15 PM

Post #1 of 31 (5550 views)
Using \? in a variable Can't Post

Hi.
As an exercise, I have written a little script that reads through a directory looking for a string in each file.
Because I use a regex to do the find, I need to escape those characters that have a special meaning in a regular expression. I am attempting to do this in
the code, but am not having much luck.
The code I'm working with is --

$switch = shift;
$dir = pop;
$str = join(' ',@ARGV);
$str =~ s/^"// && $str =~ s/"$//;

my @characters = map {
if ($_ eq "?") {$_ = '\?';}
if ($_ eq ".") {$_ = '\.';}
if ($_ eq "*") {$_ = '\*';}
if ($_ eq "+") {$_ = '\+';}
$_;
} split(//,$str);
$str = join(/ /,@characters);

-d $dir || die "Directory not found. Terminating $!";
print "Now scanning $dir for \"$str\"\n";

The first thing I discovered is that it is impossible to code
if ($input eq "?") {$input = "\$input";}
presumably because the leading backslash is interpreted as an attempt to create a reference to $input.
And while this code works fine with search arguments like "Chapter" or "Chapter 6", for example, when I enter an argument of "???" the print Now scanning... command prints out each file in the directory whose name is 3 characters long. Searching for "??" prints those files whose name is two characters long, and so on.
If, when I invoke the script, I enter \?\?\?, the script works as expected, when, of course, I would have thought I would get a final argument of \\?\\?\\?.
If I change the code to read
if ($_ eq "A") {$_ = '\A';}
and enter the script with a search argument of "AAA", it correctly searches for \A\A\A. Thus, it seems that the problem has to do with the fact that the search is for a question mark. Except that, if I code
my $input = "?";
if ($input eq "?") {$input = '\?';}
print "INPUT = $input\n";
print INPUT prints "INPUT = \?".
Thanks in advance,
s660117


FishMonger
Veteran / Moderator

Oct 13, 2012, 3:45 PM

Post #2 of 31 (5543 views)
Re: [s660117] Using \? in a variable [In reply to] Can't Post

You need to use the quotemeta() function or use \Q...\E in the regex.

http://perldoc.perl.org/functions/quotemeta.html


MDTech.us_MAN
Novice

Oct 13, 2012, 8:25 PM

Post #3 of 31 (5530 views)
Re: [s660117] Using \? in a variable [In reply to] Can't Post


In Reply To
...

The first thing I discovered is that it is impossible to code
if ($input eq "?") {$input = "\$input";}
presumably because the leading backslash is interpreted as an attempt to create a reference to $input.

...




Use:

Code
if ($input eq '?') {$input = '\$input';}


When you use ' I don't think variables get parsed at all.

Maxwell.

Owner and Webmaster of http://MDTech.us


(This post was edited by MDTech.us_MAN on Oct 13, 2012, 8:27 PM)


s660117
User

Oct 14, 2012, 7:15 AM

Post #4 of 31 (5513 views)
Re: [FishMonger] Using \? in a variable [In reply to] Can't Post

Thanks for your reply, FishMonger.
OK... I commented out the code that checks for special characters and coded
$str = quotemeta($str);
instead.
In this case, the print "Now scanning..." line now prints
Now scanning . for "INC\ map\ ref\ use",
where INC, map, etc are the names of three character members in the directory and the spaces are escaped.
So, I think two problems remain: that an argument of "???" still results in the member names and that I don't need to escape a blank.
s660117


FishMonger
Veteran / Moderator

Oct 14, 2012, 8:48 AM

Post #5 of 31 (5508 views)
Re: [s660117] Using \? in a variable [In reply to] Can't Post

Please post a short but complete script that demonstrates your problem and explain how the results differ from what you expect.


s660117
User

Oct 14, 2012, 12:51 PM

Post #6 of 31 (5502 views)
Re: [FishMonger] Using \? in a variable [In reply to] Can't Post

FishMonger,
Thanks again for your reply.
The code I'm working with is --
----------------------------------------------------------------------------
$switch = shift;
$dir = pop;
$str = join(' ',@ARGV);
$str =~ s/^"// && $str =~ s/"$//;

my @characters = map {
if ($_ eq "?") {$_ = '\?';}
if ($_ eq ".") {$_ = '\.';}
if ($_ eq "*") {$_ = '\*';}
if ($_ eq "+") {$_ = '\+';}
$_;
} split(//,$str);
$str = join(/ /,@characters);

-d $dir || die "Directory not found. Terminating $!";
print "Now scanning $dir for \"$str\"\n";
--------------------------------------------------------------------------
This code is the start of a larger script, but is self-contained and is meant to be executed with three arguments: a switch (which is only relevant later on), a search argument, and a directory to be searched. The rest of the script reads through each file in the directory, looking for the search string.
It uses a regex to find the string, hence the need to escape the special characters listed in the if clauses.

This code works if I enter a character string for the search arguments, even when the string contains a space. Thus, "perl myscript -s AAA . " causes the 'print "Now scanning..."' line to display "Now scanning . for AAA" in the current directory, and "perl myscript -s AAA BBB ." causes the line to print "Now scanning . for AAA BBB". If I enter "perl my_script -s +++ .", I get "Now scanning for \+\+\+"; if I enter "perl my_script -s ... .", I get "Now scanning for \.\.\.".

The problem occurs when I enter either a question mark or an asterisk as the search argument. Entering "perl my_script -s ??? ." causes the print line to write that it is now scanning for each of the files in the current directory whose name is 3 characters long: "Now scanning for INC, map, ref, use."; entering "perl my_script -s ?? ." causes the print line to list those files whose name is two characters in length. Entering "perl my_script -s * ." cause the now scanning line to list all files in the current directory.

If I comment out the code that looks for the special characters and insert "$str = quotemeta($str)", so that the code looks like -
---------------------------------------------------------------------------------
$switch = shift;
$dir = pop;
$str = join(' ',@ARGV);
$str =~ s/^"// && $str =~ s/"$//;
$str = quotemeta($str);

# my @characters = map {
# if ($_ eq '?') {$_ = '\?';}
# if ($_ eq ".") {$_ = '\.';}
# if ($_ eq "*") {$_ = '\*';}
# if ($_ eq "+") {$_ = '\+';}
# print "DOLLAR UNDERSCORE = $_\n";
# $_;
# } split(//,$str);
# $str = join(/ /,@characters);

-d $dir || die "Directory not found. Terminating $!";
print "Now scanning $dir for \"$str\"\n";
------------------------------------------------------------------------------------
the 'print "Now scanning..." line prints "Now scanning . for "INC\ map\ ref\ use".
I realize that somehow the question mark and the asterisk are being interpreted as special characters, but I don't know why or how.
Thanks again,
s660117


FishMonger
Veteran / Moderator

Oct 14, 2012, 2:11 PM

Post #7 of 31 (5471 views)
Re: [s660117] Using \? in a variable [In reply to] Can't Post

That's not a complete script as I asked for, which I need to properly test your problem.

I could write my one test script but what's the point since I wouldn't make the same mistakes that you're doing.


s660117
User

Oct 14, 2012, 2:29 PM

Post #8 of 31 (5461 views)
Re: [FishMonger] Using \? in a variable [In reply to] Can't Post

FishMonger,
Thanks for your reply.
Actually, the code I gave you functions as a complete script. Input is accepted from the command line, is parsed, and the second argument is processed. In my script, I have an exit statement immediately following the "print Now Scanning" statement.
Try it and see.
s660117


s660117
User

Oct 14, 2012, 2:32 PM

Post #9 of 31 (5459 views)
Re: [s660117] Using \? in a variable [In reply to] Can't Post

In case you're interested, the entire script is as follows -

my $switch;
my $dir;
my $str;
my $file;

if (@ARGV) {
$switch = shift;
$dir = pop;
$str = join(' ',@ARGV);
$str =~ s/^"// && $str =~ s/"$//;
}
else {
print "Do you want to search for a file or a string?\n";
chomp($switch = (<STDIN>));
if ($switch eq '-s' || $switch eq "string") {$switch = "-s";}
else {$switch = "-f";}
print "Enter the string or file you want to search for: ";
chomp($str = (<STDIN>));
print "Enter the directory you want to search : ";
chomp($dir = (<STDIN>));
}
$str = join(' ',@ARGV);
$str =~ s/^"// && $str =~ s/$"//;
# $str = quotemeta($str);

my @characters = map {
if ($_ eq '?') {$_ = '\?';}
if ($_ eq ".") {$_ = '\.';}
if ($_ eq "*") {$_ = '\*';}
if ($_ eq "+") {$_ = '\+';}
print "DOLLAR UNDERSCORE = $_\n";
$_;
} split(//,$str);
$str = join(/ /,@characters);

-d $dir || die "Directory not found. Terminating $!";
print "Now scanning $dir for \"$str\"\n";

sub data_for_path {
my $entry = shift;
# print "ENTRY = $entry\n";

if (( -f $entry || -l $entry ) && ( $switch eq '-f' )) {
$entry =~ /(^.*)\/(.*$)/;
$dir = $1;
$file = $2;
print "Found file $file in $dir\n" if ( $2 =~ /$str/ );
return;
}

if (( -f $entry || -l $entry ) && ( $switch eq '-s' )) {
open(FILE,$entry) || die "Failed to open $entry $!";
while (<FILE>) {(/$str/i) && print "$entry - $_"};
close(FILE);
return;
}

if ( -d $entry ) {
# print "Now processing directory $entry\n";
opendir PATH, $entry || die "Failed to open $entry $!";
my @files = readdir PATH;
closedir PATH;
for my $file (@files) {
next if $file eq '.' || $file eq '..';
# print " > Entry = $entry\n";
# print " > File = $file\n";
data_for_path("$entry/$file");
}
}
}
data_for_path($dir);


s660117
User

Oct 15, 2012, 2:31 PM

Post #10 of 31 (5387 views)
Re: [s660117] Using \? in a variable [In reply to] Can't Post

My previous copy of the script erroneously duplicated the code that processes the search string when it is input from the command line. The corrected code is--
my $switch;
my $dir;
my $str;
my $file;

if (@ARGV) {
$switch = shift;
$dir = pop;
$str = join(' ',@ARGV);
$str =~ s/^"// && $str =~ s/"$//;
}
else {
print "Do you want to search for a file or a string?\n";
chomp($switch = (<STDIN>));
if ($switch eq '-s' || $switch eq "string") {$switch = "-s";}
else {$switch = "-f";}
print "Enter the string or file you want to search for: ";
chomp($str = (<STDIN>));
print "Enter the directory you want to search : ";
chomp($dir = (<STDIN>));
}
# $str = quotemeta($str);

my @characters = map {
if ($_ eq '?') {$_ = '\?';}
if ($_ eq ".") {$_ = '\.';}
if ($_ eq "*") {$_ = '\*';}
if ($_ eq "+") {$_ = '\+';}
print "DOLLAR UNDERSCORE = $_\n";
$_;
} split(//,$str);
$str = join(/ /,@characters);

-d $dir || die "Directory not found. Terminating $!";
print "Now scanning $dir for \"$str\"\n";

sub data_for_path {
my $entry = shift;
# print "ENTRY = $entry\n";

if (( -f $entry || -l $entry ) && ( $switch eq '-f' )) {
$entry =~ /(^.*)\/(.*$)/;
$dir = $1;
$file = $2;
print "Found file $file in $dir\n" if ( $2 =~ /$str/ );
return;
}

if (( -f $entry || -l $entry ) && ( $switch eq '-s' )) {
open(FILE,$entry) || die "Failed to open $entry $!";
while (<FILE>) {(/$str/i) && print "$entry - $_"};
close(FILE);
return;
}

if ( -d $entry ) {
# print "Now processing directory $entry\n";
opendir PATH, $entry || die "Failed to open $entry $!";
my @files = readdir PATH;
closedir PATH;
for my $file (@files) {
next if $file eq '.' || $file eq '..';
# print " > Entry = $entry\n";
# print " > File = $file\n";
data_for_path("$entry/$file");
}
}
}
data_for_path($dir);


FishMonger
Veteran / Moderator

Oct 17, 2012, 9:53 AM

Post #11 of 31 (5323 views)
Re: [s660117] Using \? in a variable [In reply to] Can't Post

Please use the code tags around all code you post so that its formatting (indentation) is retained, which makes it easier to read and follow.

Obviously you didn't attempt to use \Q...\E in the regex (as I suggested in my first post) to escape the metacharacters.


Code
#!/usr/bin/perl 

use strict;
use warnings;

my $str = 'The * cat scratched + people. Did it not?';
my @pattern = qw( . * + ? );

foreach my $pattern (@pattern) {
$pattern =~ s/\./\./;
if ( $str =~ /\Q$pattern\E/ ) {
print "We matched $pattern at position: " . index($str, $pattern) . "\n";
}
}


Outputs:

Quote
We matched . at position: 28
We matched * at position: 4
We matched + at position: 20
We matched ? at position: 41



s660117
User

Oct 17, 2012, 1:31 PM

Post #12 of 31 (5318 views)
Re: [FishMonger] Using \? in a variable [In reply to] Can't Post

Thanks for the reply, Fishmonger.
I have a couple of questions and concerns.
1) Did you have the same experience as me with the code as written?
2) Why does the question mark elicit the names of members in the directory instead of being taken as a simple chacter?
3) What does \E do? I can't find it in Programming Perl and taking it out appears to have no effect.
4) Why code $pattern =~ s/\./\./; if all this does is substitute a character for the same character? Again, taking it out has no effect.
5) I am having trouble integrating your code into my program since at no time does the later code know whether metacharacters might be in the search string, and, if so, which ones are present.
Thanks,
s660117


FishMonger
Veteran / Moderator

Oct 17, 2012, 1:59 PM

Post #13 of 31 (5316 views)
Re: [s660117] Using \? in a variable [In reply to] Can't Post

I did not test your code.

If you want me to test or review your code, you first must post it within code tags which retains the formatting (indentation) and makes your code readable (assuming you actually formatted it correctly).

Programming Perl is a great resource book, but not the best choice for learning about regular expressions. If you want a book for that purpose, you should get "Mastering Regular Expressions, 3rd Edition".
http://shop.oreilly.com/product/9780596528126.do

You can also learn about regex's by reading the documentation that comes with perl. The program that is used to access the docs is 'perldoc'. If you issue the command 'perldoc perl', you'll get an index of the documentation. There are several related to regex's, such as 'perldoc perlretut', and 'perldoc perlre'.


(This post was edited by FishMonger on Oct 17, 2012, 1:59 PM)


s660117
User

Oct 17, 2012, 2:23 PM

Post #14 of 31 (5312 views)
Re: [FishMonger] Using \? in a variable [In reply to] Can't Post

Ok. here's the formatted code --


Code
 
my $switch;
my $dir;
my $str;
my $file;

if (@ARGV) {
$switch = shift;
$dir = pop;
$str = join(' ',@ARGV);
}
else {
print "Do you want to search for a file or a string?\n";
chomp($switch = (<STDIN>));
if ($switch eq '-s' || $switch eq "string") {$switch = "-s";}
else {$switch = "-f";}
print "Enter the string or file you want to search for: ";
chomp($str = (<STDIN>));
print "Enter the directory you want to search : ";
chomp($dir = (<STDIN>));
}

# $str = quotemeta($str);

# my @characters = map {
# if ($_ eq '?') {$_ = '\?';}
# if ($_ eq ".") {$_ = '\.';}
# if ($_ eq "*") {$_ = '\*';}
# if ($_ eq "+") {$_ = '\+';}
# print "DOLLAR UNDERSCORE = $_\n";
# $_;
# } split(//,$str);
# $str = join(/ /,@characters);

-d $dir || die "Directory not found. Terminating $!";

$str =~ s/^"// && $str =~ s/"$//;

print "Now scanning $dir for \"$str\"\n";

sub data_for_path {
my $entry = shift;
# print "ENTRY = $entry\n";

if (( -f $entry || -l $entry ) && ( $switch eq '-f' )) {
$entry =~ /(^.*)\/(.*$)/;
$dir = $1;
$file = $2;
print "Found file $file in $dir\n" if ( $2 =~ /$str/ );
return;
}

if (( -f $entry || -l $entry ) && ( $switch eq '-s' )) {
open(FILE,$entry) || die "Failed to open $entry $!";
while (<FILE>) {(/$str/i) && print "$entry - $_"};
close(FILE);
return;
}

if ( -d $entry ) {
# print "Now processing directory $entry\n";
opendir PATH, $entry || die "Failed to open $entry $!";
my @files = readdir PATH;
closedir PATH;
for my $file (@files) {
next if $file eq '.' || $file eq '..';
# print " > Entry = $entry\n";
# print " > File = $file\n";
data_for_path("$entry/$file");
}
}
}
data_for_path($dir);



Code



      
    


FishMonger
Veteran / Moderator

Oct 17, 2012, 6:17 PM

Post #15 of 31 (5306 views)
Re: [s660117] Using \? in a variable [In reply to] Can't Post

Your code and question(s) are telling me that this is your class homework assignment. With that in mind, I will critique portions of your code making a few suggestions, but I won't provide a complete solution.

No where in this thread do you indicate that you're using the strict and warnings pragmas. Those pragmas should be in every Perl script you write so, if you're not using them, please add them to your script.

You really should not be parsing @ARGV in than manner for this task. Instead, it would be much better to use the Getopt::Long module. http://search.cpan.org/~jv/Getopt-Long-2.38/lib/Getopt/Long.pm

Here's an example rewrite of that parsing.

Code
#!/usr/bin/perl 

use strict;
use warnings;
use Getopt::Long;

my ($pattern, $dir, $file, $string);
GetOptions(
'd=s' => \$dir,
'p=s' => \$pattern,
'f=s' => \$file,
's=s' => \$string,
);

die usage() unless ($dir and $pattern and ($file or $string));
-d $dir or die "Directory not found. Terminating $!";


Your data_for_path() sub should be split up into 2 subs. One for handing the -f option and one for handing the -s option.

You should also look into using the File::Basename or the File::Spec module when working with (and splitting) path specifications.
http://search.cpan.org/~dom/perl-5.14.3/lib/File/Basename.pm
http://search.cpan.org/~smueller/PathTools-3.33/lib/File/Spec.pm


s660117
User

Oct 18, 2012, 11:21 AM

Post #16 of 31 (5279 views)
Re: [FishMonger] Using \? in a variable [In reply to] Can't Post

Fishmonger,
I am not in a class. I am teaching myself Perl and coded my routine as an exercise.
I cut and pasted your code, but could not get it to work. To begin with, it chokes on the usage subroutine. As first I thought I had to include Pod::Usage, but that doesn't help.
If I comment out the "die" line and execute the code, it refuses to recognize a directory of '.', telling me instead that the variable is undefined.
Finally, your updates do nothing to help me with the need/desire to backslash the metacharacters within the script, which was the reason for my original post.
Thanks again,
s660117


FishMonger
Veteran / Moderator

Oct 18, 2012, 12:49 PM

Post #17 of 31 (5273 views)
Re: [s660117] Using \? in a variable [In reply to] Can't Post

The usage() sub, which should demonstrate how to execute the script, was intended to be written by you. Typically when using the Getopt::Long module I also use the Pod::Usage module and write the POD documentation in the script. In this case I chose not to do that because I didn't want to cover how to write POD documentation.

I did make an error in that example. The -f and -s options should be booleans, not string options.

Here's an updated version of that example, which only handles the parsing of the script arguments.

Code
#!/usr/bin/perl 

use strict;
use warnings;
use Getopt::Long;
use File::Find;
use Data::Dumper;

my ($pattern, $dir, $file, $string);
GetOptions(
'd=s' => \$dir,
'p=s' => \$pattern,
'f' => \$file,
's' => \$string,
);

die usage() unless ($dir and $pattern and ($file or $string));
-d $dir or die "Directory not found. Terminating $!";

print Dumper ($pattern, $dir, $file, $string);


################################################################################

sub usage {
print "Usage: $0 -d <dir> -p <pattern> [-f -s]\n\n",
"Example: $0 -d . -p 'some *? pattern' -s\n\n"
}


Here's its output.

Quote
[root@099-91-RKB01 ~]# ./test.pl -d . -p 'some *? pattern' -f
$VAR1 = 'some *? pattern';
$VAR2 = '.';
$VAR3 = 1;
$VAR4 = undef;


My prior post showed how to use \Q...\E to escape the metacharacters in a regex and I referred you to the perl documentation for additional explanations. Do you need more info in that area?

I noticed that you're making recursive calls to your data_for_path() sub to process a directory tree. It would be much better/cleaner to use the File::Find module for that purpose.

As I previously mentioned, I'd split your sub into 2 separate subs, one to handle the -f option and one to handle the -s option. Then it would be a simple if conditional to decide which one of those gets executed as the "wanted" function for File::Find.

File::Find - Traverse a directory tree. - http://search.cpan.org/~dom/perl-5.14.3/lib/File/Find.pm
File::Find::Rule - Alternative interface to File::Find - http://search.cpan.org/~rclamp/File-Find-Rule-0.33/lib/File/Find/Rule.pm


s660117
User

Oct 20, 2012, 11:48 AM

Post #18 of 31 (5221 views)
Re: [FishMonger] Using \? in a variable [In reply to] Can't Post

Thanks for all your help, FishMonger, but I still have a question or two.

I have amended my opening code as follows --

Code
#! /usr/bin/perl -w 
use strict;
use warnings;
use Getopt::Long;
use File::Find;
use Data::Dumper;

my ($option, $dir, $str, $file);

GetOptions (
'd=s' => \$dir,
'o=s' => \$option,
's' => \$str,
'f' => \$file,
);
die usage() unless ($dir and $option and ($str or $file));
sub usage {
print "Usage: $0 -d <dir> -o <option> -s -f\n\n",
"Example: $0 -d . -o 'some *? option' -s -f\n\n"
}

$str = quotemeta($str);

print Dumper ($option, $dir, $file, $str);

-d $dir || die "Directory not found. Terminating $!";
print "Now scanning $dir for \"$str\"\n";

exit;


Code
 
1) Now when I execute perl my_script -p -s -d '.' -s "ZZZ" with $str and $file defined as booleans, the Print Dumper line prints --
$VAR1 = '-s';
$VAR2 = '.';
$VAR3 = undef;
$VAR4 = '1';
Now scanning . for "1",
where the value of $str is '1'.
If I change those GetOptions to strings, I get --
$VAR1 = '-s';
$VAR2 = '.';
$VAR3 = undef;
$VAR4 = 'ZZZ';
Now scanning . for "ZZZ",
and the value of $str is available for use.

2) When I execute perl mygrep_pg -o -s -d '.' -s ???, without quotes around the string, I still see the first member in the directory whose name is three characters long --
$VAR1 = '-s';
$VAR2 = '.';
$VAR3 = undef;
$VAR4 = 'INC';
Now scanning . for "INC".
I would love to understand what is happening here.

3) When I execute perl mygrep_pg -o -s -d '.' -s "???", with quotes, I see --
$VAR1 = '-s';
$VAR2 = '.';
$VAR3 = undef;
$VAR4 = '\\?\\?\\?';
Now scanning . for "\?\?\?".
So, the final option works, in spite of the double backslashes.

Once again, thanks for your help, although I remain clueless as to how @ARGV is handling an unquoted question mark.
s660117

(And sorry for the bold type; I am having a problem turning it off.)


FishMonger
Veteran / Moderator

Oct 20, 2012, 12:33 PM

Post #19 of 31 (5217 views)
Re: [s660117] Using \? in a variable [In reply to] Can't Post

First, you need to understand that anything passed to the script which is not configured as an option (i.e., is the value of that option) needs to be quoted.

You seem to have altered the meaning of the -s option and added a new -o option, which I'm not sure what you intend that to do. We need to keep these things consistent and clear so that we know what they're used for.

The meaning of the options I used are:
-d = the top level directory to start the search
-p = the regex pattern to be used when matching the filename or file contents
-f = boolean to indicate that the pattern is to match filenames
-s = boolean to indicate that the pattern is used in searching the file contents

The -d and -p options require a quoted string for their value. The -f and -s options are booleans which do not get values passed to them on the command line.

If what you need differs from those definitions, then please provide the list of options you want to use and a clear definition for each.

For now, lets not be concerned with quoting the meta characters. Lets get the options figured out.


s660117
User

Oct 20, 2012, 1:37 PM

Post #20 of 31 (5212 views)
Re: [FishMonger] Using \? in a variable [In reply to] Can't Post

FishMonger,
Maybe I'm being dense.
The behavior of the script depends on an option that is equal to either -s or -f. When the option is equal to -s, the script expects as input the name of the directory to be searched and the string that it will attempt to find in each of the directory's files. When the option is -f, the script function like File::Find: it will look to see if the specified string is the name of a file in the specified directory.
Given this, I don't see how the input string and input directory can be coded as boolean values.
s660117


s660117
User

Oct 20, 2012, 1:47 PM

Post #21 of 31 (5209 views)
Re: [s660117] Using \? in a variable [In reply to] Can't Post

FishMonger,
I was being dense.
I see now how you setup the GetOptions.
I am amending my script now.
s660117


FishMonger
Veteran / Moderator

Oct 20, 2012, 1:57 PM

Post #22 of 31 (5208 views)
Re: [s660117] Using \? in a variable [In reply to] Can't Post

You can approach it in 2 different ways. You could use a single -o option where its value is 's' or 'f'. Or, IMO, the better approach would be to separate those as two boolean flags -s ($str) and -f ($file). The script could then use a simple conditional test to see which one is set to 1 (i.e., true) and then execute the corresponding subroutine.

example: (I'm assuming both cases require the directory and pattern to use in the search.

Code
if ($file) { 
find_files($dir, $pattern);
}
elsif ($str) {
parse_files($dir, $pattern);
}



s660117
User

Oct 20, 2012, 2:12 PM

Post #23 of 31 (5204 views)
Re: [s660117] Using \? in a variable [In reply to] Can't Post

Ok... got it.
Now the only problem is that I check both $str and $file in the course of the script and GetOptions will set one of them to undef.
I suppose the only way around this is to set $file equal to '0' when $str is equal to '1' and vice versa.


s660117
User

Oct 20, 2012, 2:16 PM

Post #24 of 31 (5203 views)
Re: [s660117] Using \? in a variable [In reply to] Can't Post

Scratch that last post, please.
I now have coded --
if (( -f $entry || -l $entry ) && $file ) {
and
if (( -f $entry || -l $entry ) && $str ) {
s660117


FishMonger
Veteran / Moderator

Oct 20, 2012, 2:19 PM

Post #25 of 31 (5200 views)
Re: [s660117] Using \? in a variable [In reply to] Can't Post

Why are you doing the -f and -l test?

Are you still manually doing the recursion? If so, stop doing that and instead use the File::Find module.


(This post was edited by FishMonger on Oct 20, 2012, 2:20 PM)

First page Previous page 1 2 Next page Last page  View All
 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives