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 20, 2012, 2:30 PM

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

One thing at a time.
Just wanted to make sure I had the GetOptions right.


s660117
User

Oct 20, 2012, 2:36 PM

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

One thing at a time...
I was just trying to get the GetOptions right.


FishMonger
Veteran / Moderator

Oct 20, 2012, 2:44 PM

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


Code
#!/usr/bin/perl 

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

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


unless ($dir and $pattern and ($str or $file)) {
croak usage();
}

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

-d $dir or croak "Directory not found. Terminating $!";
print "Now scanning $dir for '$pattern'\n";


if ($file) {
find_files($dir, $pattern);
}
else {
parse_files($dir, $pattern);
}

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

sub find_files {

return;
}

sub parse_files {

return;
}

sub usage {
print "Usage: $0 -d <dir> -o <option> -s -f\n\n",
"Example: $0 -d . -o 'some *? option' -f\n\n";

return;
}



s660117
User

Oct 21, 2012, 12:49 PM

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

FishMonger,
Even though the whole point of the exercise was to write a recursive script, I have rewritten it using File::Find --

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

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

GetOptions (
'd=s' => \$dir,
'p=s' => \$pattern,
's' => \$str,
'f' => \$file,
);
die usage() unless ($dir and $pattern and ($str or $file));

$pattern = quotemeta($pattern);

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

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

if ($file) {
find_files($dir, $pattern);
}
else {
parse_files($dir, $pattern);
}

#----------------------------------------------------------------------------------

sub find_files {
find(sub {
if (/$pattern/) {
print "$File::Find::name found\n";
}
return;
} , $dir)
}

sub parse_files {
find(sub {
my $file_name = $_;
open(FILE,$file_name) || die "Failed to open $file_name $!";
while (<FILE>) {(/$pattern/i) && print "$file_name => $_"};
close(FILE);
return
} , $dir)
}

sub usage {
print "Usage: $0 -d <dir> -o <option> -s -f\n\n",
"Example: $0 -d . -o 'some *? option' -f\n\n";

return;

I remain mystified by the unexpected results I got when I entered a pattern of unquoted question marks.
Thanks for all your help,
s660117


FishMonger
Veteran / Moderator

Oct 21, 2012, 1:50 PM

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

What part of the results were unexpected?
What part of of the results mystified you?

Did you forget that the question marks have special meaning to the shell (i.e., cmd.exe) and that the shell processes those prior to passing anything to the script? That is why they need to be quoted.


(This post was edited by FishMonger on Oct 21, 2012, 1:53 PM)


s660117
User

Oct 23, 2012, 2:50 PM

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

FishMonger,
Just continue to be curious about the fact that when I enter unquoted question marks, I end up with the names of all files in the directory whose length is equal to the number of question marks entered.
But now I realize that certain characters must be quoted.
It makes me think of an old colleague of mine. If someone came to him with something counter-intuitive he would say "Don't do that!".
Thanks again,
s660117

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