switches to stop errant one-liner at first warning 
Author Message
 switches to stop errant one-liner at first warning

For my use of one liners, I cannot find a switch on the perlrun man
page to kill the script at the first
"Use of uninitialized value in substitution (s///) at -e line 1, <> line 1."
And perl -nwe 'use strict;...' would get executed for each line, and
doesn't stop it anyway.

Anyway, what switches can I use to stop this mistake
$ perl -nwe 'if($a=~s/a/b/){print $a}' file
at the first warning, but at the same time not loading it up to spoil
the one-linerness?  Sort of like sh -e.
--
http://www.*-*-*.com/ (04)25854780



Tue, 22 Nov 2005 00:29:06 GMT  
 switches to stop errant one-liner at first warning

: For my use of one liners, I cannot find a switch on the perlrun man
: page to kill the script at the first
: "Use of uninitialized value in substitution (s///) at -e line 1, <> line 1."

There isn't one.  Do it in the code.

: And perl -nwe 'use strict;...' would get executed for each line,

use() influences compilation only.  It does not get executed on each
iteration.  

: and doesn't stop it anyway.

Nor is it meant to.  "use strict 'vars';" would force you to declare
variables, but you never have to define them.

: Anyway, what switches can I use to stop this mistake
: $ perl -nwe 'if($a=~s/a/b/){print $a}' file
: at the first warning, but at the same time not loading it up to spoil
: the one-linerness?  Sort of like sh -e.

Maybe (untested)...

    perl -ne 'use warnings FATAL => qw(uninitialized); print $a if
$a=~s/a/b/' file

or how about (untested again)...

    perl -nwe 'defined $a||die; print $a if $a=~s/a/b/' file

Not a great example, though.  $a never gets assigned anything, so is
always undef.



Tue, 22 Nov 2005 02:23:55 GMT  
 switches to stop errant one-liner at first warning

Quote:

> For my use of one liners, I cannot find a switch on the perlrun man
> page to kill the script at the first
> "Use of uninitialized value in substitution (s///) at -e line 1, <> line 1."
> And perl -nwe 'use strict;...' would get executed for each line, and
> doesn't stop it anyway.

> Anyway, what switches can I use to stop this mistake
> $ perl -nwe 'if($a=~s/a/b/){print $a}' file
> at the first warning, but at the same time not loading it up to spoil
> the one-linerness?  Sort of like sh -e.

There's no such switch.  The 'perlrun' documentation does mention
handling of warnings as fatal errors via '__WARN__' hooks.  Try:

$ perl -nwe 'BEGIN{ $SIG{__WARN__} = sub { die $_[0];}} if($a=~s/a/b/){print $a}' file

The program stops after the first warning.

--
Hope this helps,
Steven



Tue, 22 Nov 2005 02:27:15 GMT  
 switches to stop errant one-liner at first warning

Quote:


> : For my use of one liners, I cannot find a switch on the perlrun man
> : page to kill the script at the first
> : "Use of uninitialized value in substitution (s///) at -e line 1, <> line 1."

(snipped)

Quote:
> Maybe (untested)...

>     perl -ne 'use warnings FATAL => qw(uninitialized); print $a if
> $a=~s/a/b/' file

(snipped)

Interesting.  I've not been using 'warnings' to its full potential.
After looking at Jay's example and reading 'perllexwarn' for the
first time, I tested the following:

$ perl -mwarnings=FATAL,uninitialized -nwe 'if($a=~s/a/b/){print $a}' file
Use of uninitialized value in substitution (s///) at -e line 1, <> line 1.

Yea, it works.

Does that count as using a 'perlrun' switch?  :)

--
Regards,
Steven



Tue, 22 Nov 2005 02:41:14 GMT  
 switches to stop errant one-liner at first warning

Quote:

> For my use of one liners, I cannot find a switch on the perlrun man
> page to kill the script at the first

[warning]

Why do you want to kill the script at the first warning?

Quote:
> And perl -nwe 'use strict;...' would get executed for each line, and
> doesn't stop it anyway.

Warnings never affect how your program executes, they are not fatal.

Quote:
> Anyway, what switches can I use to stop this mistake
> $ perl -nwe 'if($a=~s/a/b/){print $a}' file
> at the first warning,

There isn't one.

Quote:
> but at the same time not loading it up to spoil
> the one-linerness?

I only know how to do a spoiled one:

   perl -nwe '$SIG{__WARN__} = sub { die $_[0] }; ...real code here' file

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Tue, 22 Nov 2005 02:06:22 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. gave a2p, s2p a one-liner, want one-liner back

2. Splitting challenge (looking for an O(one)-liner)

3. arbitrary one-liner generator

4. one liner issues

5. Command line One liner challenge

6. One-liner enhancement needed

7. one-liner for zapping header of postscript files in mail

8. Need one-liner

9. One liner to remove duplicate records

10. One-Liner Mania

11. windows one liner to output unix line feed

 

 
Powered by phpBB® Forum Software