Ask a Question related to PERL Beginners, Design and Development.
-
Jerry Rocteur #1
Use of uninitialized value in pattern match (m//) at ./getopt.pl line 14.
Hi,
I'm trying to be a good boy and use strict and warnings ..
The more I do, the more I feel I'm wasting so much time and should
become productive, my code looks full of 'my', I could understand the
requirement inside a sub routing but in the main code it gives me the
willies. My DBI Perl is even more of a headache ;-((
Anyway, enough whining, now I've going through code that is working and
thought I'd see the big difference if I change it to use warnings and
strict and this little routing gives me 'Use of uninitialized value in
pattern match (m//) at ./getopt.pl line 14.'
Line 14 is the while line..
I've tried all sorts of stuff with defined but keep getting syntax
errors. I've tried perldoc warnings and perldoc perllexwarn .. In any
case, unless I'm really missing something, I can't see what could be
problematic with the while statement below, of course this shows my
ignorance but as I've decided to use warnings and strict, I want to do
it right..
How would a pedant code the following to avoid this warning.. Your
answers will help me in the future.
#if ($#ARGV >= 0) { # What I've tried
while ($_ = $ARGV[0], /^-/) {
shift;
last if /^--$/;
if (/^-d(.*)/) { $debug++ }
elsif (/^-v(.*)/) { $verbose++ }
else { print "$_ unknown option\n";
Usage;
}
}
#}
Thanks in advance, I really want to put, Perl with warnings and strict
in my CV ;-))))))))))))
Jerry
Jerry Rocteur Guest
-
pattern match
Where can I find infi or doc on "pattern match" used within WHERE clause (mysql). As I need to matche with PHP variables I'd prfer something... -
please help !! pattern match
Hi , I need some help me to extract a pattern. The delimiters is a pair of "abcd" and "efgh". Can some one help me with an efficient use of Greedy... -
Return second line after a pattern match?
Ritchie wrote: is this what you want? find the "----------" line and skip it fine the 1st line after it and skip it find the 2nd line after it... -
uninitialized value in pattern match
#!/usr/bin/perl use warnings; use strict "refs"; use strict "subs"; use strict "vars"; our $netscape; $netscape = ($ENV{HTTP_USER_AGENT}... -
Pattern match with 2 conditions
Stephan Bour <sbour@niaid.nih.gov> writes: use strict; # is your friend what's the point of this when you just set it back to "" below? ... -
Jerry Rocteur #2
Use of uninitialized value in pattern match (m//) at ./getopt.pl line 14.
Hi,
I'm trying to be a good boy and use strict and warnings ..
The more I do, the more I feel I'm wasting so much time and should
become productive, my code looks full of 'my', I could understand the
requirement inside a sub routing but in the main code it gives me the
willies. My DBI Perl is even more of a headache ;-((
Anyway, enough whining, now I've going through code that is working and
thought I'd see the big difference if I change it to use warnings and
strict and this little routing gives me 'Use of uninitialized value in
pattern match (m//) at ./getopt.pl line 14.'
Line 14 is the while line..
I've tried all sorts of stuff with defined but keep getting syntax
errors. I've tried perldoc warnings and perldoc perllexwarn .. In any
case, unless I'm really missing something, I can't see what could be
problematic with the while statement below, of course this shows my
ignorance but as I've decided to use warnings and strict, I want to do
it right..
How would a pedant code the following to avoid this warning.. Your
answers will help me in the future.
#if ($#ARGV >= 0) { # What I've tried
while ($_ = $ARGV[0], /^-/) {
shift;
last if /^--$/;
if (/^-d(.*)/) { $debug++ }
elsif (/^-v(.*)/) { $verbose++ }
else { print "$_ unknown option\n";
Usage;
}
}
#}
Thanks in advance, I really want to put, Perl with warnings and strict
in my CV ;-))))))))))))
Jerry
Jerry Rocteur Guest
-
Wiggins D'Anconia #3
Re: Use of uninitialized value in pattern match (m//) at ./getopt.pl line 14.
Jerry Rocteur wrote:
Right now you probably are. In the future the loads of time it saves> Hi,
>
> I'm trying to be a good boy and use strict and warnings ..
>
> The more I do, the more I feel I'm wasting so much time and should
> become productive, my code looks full of 'my', I could understand the
> requirement inside a sub routing but in the main code it gives me the
> willies. My DBI Perl is even more of a headache ;-((
>
will repay you greatly. Stick with it, it will help... this comes from
someone that switched "cold turkey" from not using them to using them
after coding in Perl for 3+ years...
The above is just a warning so your code is running, granted it doesn't> Anyway, enough whining, now I've going through code that is working and
> thought I'd see the big difference if I change it to use warnings and
> strict and this little routing gives me 'Use of uninitialized value in
> pattern match (m//) at ./getopt.pl line 14.'
>
do much...
They wouldn't. They would use Getopt::Std or Getopt::Long...> Line 14 is the while line..
>
> I've tried all sorts of stuff with defined but keep getting syntax
> errors. I've tried perldoc warnings and perldoc perllexwarn .. In any
> case, unless I'm really missing something, I can't see what could be
> problematic with the while statement below, of course this shows my
> ignorance but as I've decided to use warnings and strict, I want to do
> it right..
>
> How would a pedant code the following to avoid this warning.. Your
> answers will help me in the future.
>
perldoc Getopt::Std
perldoc Getopt::Long
Now....
To test if an array has length simply take it in scalar context,> #if ($#ARGV >= 0) { # What I've tried
if (@ARGV) {
I believe the issue is that the $_ is not set by the time you hit the> while ($_ = $ARGV[0], /^-/) {
pattern match, there is probably a way to make it, but to me it is
simpler to just add a line after the initial test such as:
next unless (/^-/);
Inside the loop and drop the test inside the conditional.
I assume that $debug and $verbose have been pre-declared and that you> shift;
> last if /^--$/;
> if (/^-d(.*)/) { $debug++ }
> elsif (/^-v(.*)/) { $verbose++ }
have Usage prototyped or something, as it is a bare word...
More code showing specifically why I have been rambling about $_ in the> else { print "$_ unknown option\n";
> Usage;
> }
> }
last couple of days. If you are going to shift off the argument list
why not just do it in the while loop when you are setting $_? And Perl
is very nice and lets you leave the semi-colons off in the above, to me
it is a bad style to get into and will likely lead to errors later when
you (or someone else) decides to add additional statements to those
blocks. While you are a beginner code *exactly* what you mean, use lots
of names, avoid the shortcuts, later when you don't need to ask these
kinds of questions, then sign up for a golf tournament...
I have wondered about this for a while, putting that you use warnings> #}
>
> Thanks in advance, I really want to put, Perl with warnings and strict
> in my CV ;-))))))))))))
>
and strict into a CV is a tough choice, to me I wouldn't likely talk to
a candidate unless they used them, but I am not sure I would put them on
a CV because to an advanced person it would seem overally obvious.
Though I think it is something good to bring up in an interview, and I
have had several interviewers ask about them... what does the group
think (about including it in a CV)? (Obviously I know what the thoughts
are about including them in the code)....And naturally code samples
would reflect their use.
I would also check out:
perldoc Pod::Usage
[url]http://danconia.org[/url]
A decent template, any gurus have improvements?
UNTESTED---
#!/usr/local/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
pod2usage("No options specified\n") unless (@ARGV);
# configure GetOptions with some standard gnu options
my $opt_parser = new Getopt::Long::Parser ('config' =>
['gnu_compat','bundling','permute','no_getopt_compa t'] );
# init configuration
my $cmdopts = {};
# get configuration
$opt_parser->getoptions($cmdopts,
'help|h',
'debug|d',
'verbose|v',
) or pod2usage(2);
# print the help message if they asked just for it
pod2usage(1) if (defined($cmdopts->{'help'}));
if ($cmdopts->{'verbose'}) {
print "Verbose Mode\n";
}
if ($cmdopts->{'debug'}) {
print "Debugging Mode\n";
}
################################################## ###########################
__END__
=head1 NAME
getopt - A program for parsing command line arguments
=head1 SYNOPSIS
getopt [options]
Options:
--debug turn on debugging
--verbose turn on verbosity
--help print this message
=head1 OPTIONS
=over 8
=item B<--debug>
Mode of operation resulting in debugging statements
=item B<--verbose>
Mode of operation resulting in lots of messages
=item B<--help>
Print a brief help message and exit.
=back
=head1 DESCRIPTION
My first program
=cut
Wiggins D'Anconia Guest
-
Rob Dixon #4
Re: Use of uninitialized value in pattern match (m//) at ./getopt.pl line 14.
Jerry Rocteur wrote:
Well done!>
> I'm trying to be a good boy and use strict and warnings ..
Your problem is more than likely because you're expecting Perl to be> The more I do, the more I feel I'm wasting so much time and should
> become productive, my code looks full of 'my', I could understand the
> requirement inside a sub routing but in the main code it gives me the
> willies. My DBI Perl is even more of a headache ;-((
like another language. The usual ones are C and shell script. Perl's
syntax is often quite like C's, and the built-in functions are a fair
match, but unless you're used to declaring variables locally to their
scope of use as in C++ you will end up with a long preamble declaring
everything, which can be less than useful.
'use warnings' will moan at you if a variable's value is 'undefined'.> Anyway, enough whining, now I've going through code that is working and
> thought I'd see the big difference if I change it to use warnings and
> strict and this little routing gives me 'Use of uninitialized value in
> pattern match (m//) at ./getopt.pl line 14.'
>
> Line 14 is the while line..
It's left this way straight after either
my $var
or
$var = undef
If you define stuff like:
my $var = 0
then you won't get this problem. But don't go around initialising
all variables in their declarations. It's just a warning and is useful
as such, and anyway Perl will let you concatenate or increment an
undefined value. i.e.
my $var;
followed by
$var++;
or
$var .= '>>'
will work fine without any warnings.
You need to get to understand Perl idioms. I've started by reducing your> I've tried all sorts of stuff with defined but keep getting syntax
> errors. I've tried perldoc warnings and perldoc perllexwarn .. In any
> case, unless I'm really missing something, I can't see what could be
> problematic with the while statement below, of course this shows my
> ignorance but as I've decided to use warnings and strict, I want to do
> it right..
>
> How would a pedant code the following to avoid this warning.. Your
> answers will help me in the future.
indents from 8 chars to 2 because that makes it more visible for me.
This says 'if the index of the last array element is zero or more'.> if ($#ARGV >= 0) {
You don't mean that, because an array with a single element will
have a last element with an index of zero. Try
if (@ARGV) {
:
}
OK, these lines together pull the next argument from @ARGV and> while ($_ = $ARGV[0], /^-/) {
>
> shift;
quit the loop unless it begins with a hyphen. The reason you're
getting a warning is that once your 'shift' statement below has
finally emptied the array, $_ = $ARGV[0] will set $_ to 'undef'
so the pattern match is comparing against an undefined value.
This does the same:
foreach (@ARGV) {
last unless /^-/;
except that the parameters are left in @ARGV instead of being
shifted off, and the loop is executed just once for each
element of @ARGV.
Break out if the element is just two hyphens. That's fine.> last if /^--$/;
Again, that'll do. But unless you need to capture whatever follows> if (/^-d(.*)/) {
> $debug++
> }
the '-d' then you want just
if (/^-d/) {
$debug++
}
Same here:> elsif (/^-v(.*)/) {
> $verbose++
> }
elsif (/^-v/) {
$verbose++
}
And the rest's OK. But I'd write it something like this. (Actually I would> else {
> print "$_ unknown option\n";
> Usage;
> }
> }
> }
probably never write this, but my code depends on the context of the
code and what exactly you need to do with the results :)
foreach (@ARGV) {
next unless /^-/;
last if $_ eq '--';
if ( /^-(d|v)/ ) {
$debug++ if $1 eq 'd';
$verbose++ if $1 eq 'v';
}
else {
print "$_ unknown option\n";
Usage;
}
}
HTH,
Rob
Rob Dixon Guest
-
R. Joseph Newton #5
Re: Use of uninitialized value in pattern match (m//) at ./getopt.pl line 14.
Wiggins d'Anconia wrote:
Thanks, Wiggins, and keep on rambling. The point needs reiteration. Aside from> More code showing specifically why I have been rambling about $_ in the
> last couple of days. If you are going to shift off the argument list
> why not just do it in the while loop when you are setting $_?
the needle-in-a-haystack problems, I'd say that it's a good rule of thumb:
If you have to specifically assign $_, don't bother. Perl already assigns it in
the contexts in which it is appropriate
Yes and yes.> And Perl
> is very nice and lets you leave the semi-colons off in the above, to me
> it is a bad style to get into and will likely lead to errors later when
> you (or someone else) decides to add additional statements to those
> blocks. While you are a beginner code *exactly* what you mean, use lots
> of names, avoid the shortcuts, later when you don't need to ask these
> kinds of questions, then sign up for a golf tournament...
I think about the only place it would be appropriate is in notes describing>
>>> > #}
> >
> > Thanks in advance, I really want to put, Perl with warnings and strict
> > in my CV ;-))))))))))))
> >
> I have wondered about this for a while, putting that you use warnings
> and strict into a CV is a tough choice, to me I wouldn't likely talk to
> a candidate unless they used them, but I am not sure I would put them on
> a CV because to an advanced person it would seem overally obvious.
> Though I think it is something good to bring up in an interview, and I
> have had several interviewers ask about them... what does the group
> think (about including it in a CV)? (Obviously I know what the thoughts
> are about including them in the code)....And naturally code samples
> would reflect their use.
>
coding samples available for interview or included. If sufficiently understated,
it may reassure an interviewer or screener that they are not wasting time:
Codes samples included [all compiled using strict compilation option and
pre-tested]
Which at least indicates to the reader that they are looking at something which
has run successfully. Definitely not something to wave a big flag over,
though--it would be like listing:
Have mastered ABCs
....at the top of a resume for an English professorship.
Joseph
R. Joseph Newton Guest
-
Drieux #6
Re: Use of uninitialized value in pattern match (m//) at ./getopt.pl line 14.
On Dec 13, 2003, at 10:01 AM, Wiggins d'Anconia wrote:
[..[..]> A decent template, any gurus have improvements?
my stock gag, looks like:
my $opt = parse_cmd_line_options();
....
#---------------------------------------------------------------
# These are the Subs for the parse_cmd_line_options.
#---------------------------------------------------------------
# The Usage Command so that we can use a common interface to
# handle both the cases where we want to show some basic detailed
# information with a '--help' query, as well as to exit out of
# the parse_cmd_line_options wrapper for Getopt::Long if we have
# problems with user generated commands.
#---------------------------------------------------------------
sub Usage
{
my($msg, $retval, @list) = @_;
print "$msg\n" if ($msg);
print "Usage: $0 [-i|-x|-c|--kadb] [-f <alt_file>] [-d
<sysname>]\n";
if ( @list ) {
print " the full optins list is:\n";
print "\t$_\n" foreach (@list);
}
exit($retval);
}
#------------------------
# where we wrap the Getopt::Long and resolve which arguments
# work and play well with other command line arguments.
#
# <editorialize on the what this version does or does not do
#
sub parse_cmd_line_options
{
my $ref = {};
$ref->{'delete'} = [];
#
# more editorializing
#
$ref->{'add'} = [] ;
#
# this also doubles as the flash guide. in Usage
#
my @list = qw/
file|f=s
import|init|i
installdb
export|x|e
compact|c
delete|d=s
add|a=s
fsck
nup
kadb
help|h|?/;
my @config_args = qw/bundling/;
Getopt::Long::Configure(@config_args);
my $results = GetOptions($ref, @list) ;
Usage('Problem with Input Arguments',1,@list) unless ($results);
Usage('The General Help',0,@list) if ($ref->{'help'});
#
# now to create the action station section
#
#sort out other gory details about the which arguments
#should or should not be dealt with
$ref ;
} # end of parse_cmd_line_options
This dog has soldiered on in one variant or another...
vladimir: 41:] ./db_tool.plx --kadb
option: help
we have the following options:
add [sysname config_host] -> add sysname and config_host to db
delete [sysname ] -> delete sysname from db
show_sysnames -> list current sysnames in db
config_host -> get config_host for sysname in db
export -> export db to file
import -> import file to db
q|exit|quit -> exit kadb
option: q
exiting
vladimir: 42:]
the '--kadb' started out as a bad JOKE, I whined at
myMostGloriousLeader that just for him, I would offer
up a 'kryptic command line interface with short and
arcane syntax, you know, just like kadb....' IT
HURTS when one stops laughing and has to implement
it in a rush because, well, one just needs it down and dirty,
but it has to work...
I went with the 'bundling' approach so that the
'oldGuy' *nix freaks would be 'happy' while the
gnuSters would be able to wander the plains with
their --help and the like...
ciao
drieux
---
Drieux Guest
-
Jerry Rocteur #7
Re: Use of uninitialized value in pattern match (m//) at ./getopt.pl line 14.
On Saturday, Dec 13, 2003, at 18:12 Europe/Brussels, Jerry Rocteur
wrote:
> Hi,
>
> I'm trying to be a good boy and use strict and warnings ..
>
> The more I do, the more I feel I'm wasting so much time and should
> become productive, my code looks full of 'my', I could understand the
> requirement inside a sub routing but in the main code it gives me the
> willies. My DBI Perl is even more of a headache ;-((
>
> Anyway, enough whining, now I've going through code that is working
> and thought I'd see the big difference if I change it to use warnings
> and strict and this little routing gives me 'Use of uninitialized
> value in pattern match (m//) at ./getopt.pl line 14.'
>
> Line 14 is the while line..
>
> I've tried all sorts of stuff with defined but keep getting syntax
> errors. I've tried perldoc warnings and perldoc perllexwarn .. In any
> case, unless I'm really missing something, I can't see what could be
> problematic with the while statement below, of course this shows my
> ignorance but as I've decided to use warnings and strict, I want to do
> it right..
>
> How would a pedant code the following to avoid this warning.. Your
> answers will help me in the future.
>
> #if ($#ARGV >= 0) { # What I've tried
> while ($_ = $ARGV[0], /^-/) {
> shift;
> last if /^--$/;
> if (/^-d(.*)/) { $debug++ }
> elsif (/^-v(.*)/) { $verbose++ }
> else { print "$_ unknown option\n";
> Usage;
> }
> }
> #}
>
> Thanks in advance, I really want to put, Perl with warnings and strict
> in my CV ;-))))))))))))
>
> Jerry
>
>
Sorry for the double post, my first post I had done with an email
address that is not subscribed to the list, thinking it would not go
through I reposted. I didn't realize you accepted posts from non
subscribed addresses.
I thank all those that replied on and off list, great replies. Of
course my question was more than answered. Thanks to Wigins d'Anconia,
Rob Dixon, R. Joseph Newton and drieux! Food for thought. I'm still
reviewing your replies..
Best Regards,
Jerry Rocteur
Jerry Rocteur Guest
-
Luke Bakken #8
RE: Use of uninitialized value in pattern match (m//) at ./getopt.pl line 14.
> uninitialized value in
Use the standard Getopt::Std module to process options. Don't do it> pattern match (m//) at ./getopt.pl line 14.'
yourself.
> Line 14 is the while line..
>
> I've tried all sorts of stuff with defined but keep getting syntax
> errors. I've tried perldoc warnings and perldoc perllexwarn .. In any
> case, unless I'm really missing something, I can't see what could be
> problematic with the while statement below, of course this shows my
> ignorance but as I've decided to use warnings and strict, I
> want to do
> it right..
>
> How would a pedant code the following to avoid this warning.. Your
> answers will help me in the future.
>
> #if ($#ARGV >= 0) { # What I've tried
> while ($_ = $ARGV[0], /^-/) {
> shift;
> last if /^--$/;
> if (/^-d(.*)/) { $debug++ }
> elsif (/^-v(.*)/) { $verbose++ }
> else { print "$_ unknown option\n";
> Usage;
> }
> }
> #}Luke Bakken Guest
-
Owen #9
Re: Use of uninitialized value in pattern match (m//) at ./getopt.pl line 14.
On Sat, 13 Dec 2003 18:12:17 +0100
Jerry Rocteur <rocteur@mac.com> wrote:
> I'm trying to be a good boy and use strict and warnings ..
>
> The more I do, the more I feel I'm wasting so much time and should
> become productive, my code looks full of 'my'
Because so many people in c.l.p.m said "use strict;" I decided to take the plunge and use it.
Until I worked out what was going on, I, like you, thought it was a waste of time.
That and warnings are a great pair in getting programs to run and I suppose "Use of unitialized..." is the most common warning. There is always a bit of pleasure in tracking down the reason for that one.
See [url]http://perl.abigail.nl/Musings/coding_guidelines.html[/url] suggested perl coding practices
Hang on in there, it's worth it.
--
Owen
Owen Guest



Reply With Quote

