Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help my program interpret /? as an argument 3

Status
Not open for further replies.

darkreaper00

Technical User
Aug 5, 2002
23
US
I've got myself a program that parses another program's output and formats it in a way that is useful to our lab. The biggest hangup I have with the program is trying to get it to print my usage message when /? is used for an argument. Below is the first way I tried to do that, before I realized that / was not a metacharacter that gets escaped with a backslash (which, incidentally, will behave the way i want it to if i feed it '\?', which is something I don't understand but is irrelevant at the moment):

Code:
while ($ARGV[0] ne "") {
                        # code for arguments it does 
                        # know how to handle
     }
     elsif ($ARGV[0] =~ /^\/\?/) {
	     die($usage);
     }

since then I've tried:

Code:
($ARGV[0] =~ /^'/'\?/)
($ARGV[0] =~ /(^'/'\?)/)
($ARGV[0] =~ /^/+\?/)
($ARGV[0] =~ /^$e/)
, with $e being the string '/?'

and many others... I understand why putting / between // could be problematic, and I think my next route would be to try unicode names, but I'm inclined to think there must be an easier way to do this. Any thoughts?

Thanks,
Tony
 
Code:
($ARGV[0] =~ m:^/\?:)

You can use any character instead of / to surround your regular expression. You just have to add the 'm' (for match) operator which is implicit if you use / as delimitor.
 
Tony,

this bit of code

foreach $arg (@ARGV){
print "$arg "
print "gotcha" if $arg =~ /\//;
print "\n";
}

seems to work ok

you have to "escape" the \ within the search pattern so that Perl understands that you're not terminating the search pattern

you could also use different search pattern delimiters, like this:

print "gotcha" if $arg =~ m#/#;
Mike
________________________________________________________________

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Thanks for the prompt replies, but there're still some sticking points... namely, the problem of the phantom slash that you both solved creates a problem we'll call the furtive question mark. In both cases -- using the match operator as dchoulette wrote it, and with MikeLacey's standalone 'bit of code' -- perl gets very upset with /? as an argument, though they're all equally happy with anything else I've tried following the slash. Both programs behave almost normally when I feed them any of the following arguments:
/ /abc /;; /// /& /!

The first four all produce the correct output, while /& precedes the correct output with:

Code:
     [1] 225137
     >

(the number following [1] changes each time), and /! gives the correct output followed by

Code:
[1]  + Exit 255                      perl process-hits.pl /

from my program (named process-hits) and

Code:
[1]  + Done                          perl test /

from MikeLacey's.

When I feed /? to either one, however, the error message is the same:

Code:
perl: No match.

Again, any thoughts? (and again, thanks in advance..)
 
Maybe Perl itself "steals" those arguments and only sends parts of them to your script? That's sure what it seems like. //Daniel
 
more interesting complications:

in the case of dchoulette's suggestion, the output is not a result of a successful match with
Code:
($ARGV[0] =~ m:^/\?:)
-- I also have the program die($usage) when it gets an argument that's not expected. (Funnily enough, /? is the one argument that fails before it gets to that point in the loop). Anyway, the /[.]* inputs match on MikeLacey's code (naturally, since all we're searching for is a /) but not dchoulette's, but /? does produce an error message from both.

Moreover, the "\?" which I thought was the correct input for what I coded initially is not -- it too dies on the unexpected argument premise.

I was thinking that I might just as well be rid of the line which solicits /? and have the program treat /? just as any other unexpected argument and display $usage by default (even though I still wanted to know how to fix it). But perl is making sure I pursue the problem, because removing that line doesn't fix the problem -- even when I'm not looking for a /?, perl gives me:

Code:
perl: No match.

It seems, then, perl is unhappy taking this as an argument... How, then, do I work around this? Surely someone sometime wanted to provide /? argument support to display a helpfile! Is there perhaps a defined way to do this -- maybe a variable which, when defined will respond to /? as an argument?

so much for a simple question, eh?
 
The problem is not with Perl but with the shell that you are using. I assume that you are using csh or tcsh. The problem is that '?' is a special character to csh so you have to escape it on the command line like

process-hits.pl /\?

it then gets passed into @ARGV as '/?'. ('?' is a single-character glob similar to '.' in perl regex). The '&' is also a special character, which is why you get strange behavior with it as well.

Perhaps /h would be a better choice for a help flag.

jaa

 
I agree with justice41.
The characters &!|;*? are interpreted by the shell before perl is even launch.

For example, with '/&' you run your perl script with last argument equal to '/' and in a background job. The
Code:
[1] 225137
means that your first (
Code:
[1]
) background job has process id 225137.

The shell tries to replace /? by the names of all the files in the root directory (/) whose name has only one letter (?).

When you want to use any 'strange' character (?;*! even $ if you do not want to expand a variable) in a shell command argument put the whole argument in single quotes.

Code:
process-hits.pl '/?'
 
yep, this is all shell stuff

try the same commands on a DOS machine Mike
________________________________________________________________

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top