Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number


by Anonymous Monk
on Oct 11, 2005 at 09:06 UTC ( #499070=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I need to set up a few pattern regexs. I want the script to continue if it matches a pattern but die if it matches a specific pattern:
$file =<stdin>; chomp($file); if (m/(\d+)\|\Q$file\E$/x) { do something} but if match (/^aaa\[/) {die};
what is the best way to approach the second line? Thanks

Replies are listed 'Best First'.
Re: unless/if
by pajout (Curate) on Oct 11, 2005 at 09:28 UTC
    I am not sure if I understand you exactly, but
    die if /match_to_die/; if (/match_if_something/) { #do something }
    is, probably, what you want...
Re: unless/if
by blazar (Canon) on Oct 11, 2005 at 09:34 UTC
    Well, the options seem to be mutually exclusive, so
    chomp($file=<stdin>); die "horribly\n" if /^aaa\[/; do something if /(\d+)\|\Q$file\E$/;
    or did you mean something different?

    Incidentally which is the $_ against which you're testing? I have a vague suspect you want to match against $file instead (even though the fact that it is interpolated in one of the regexen would tend to suggest the contrary). And if so, then even in pseudocode you're doing something utterly wrong...

Re: unless/if
by ysth (Canon) on Oct 11, 2005 at 16:18 UTC
    Your syntax is almost right already:
    $file =<stdin>; chomp($file); if (m/(\d+)\|\Q$file\E$/x) { do something} elsif (m/^aaa\[/) {die};
Re: unless/if
by Anonymous Monk on Oct 11, 2005 at 10:58 UTC
    you can also do :
    die "Oops" unless /matchsomething/
Re: unless/if
by Moron (Curate) on Oct 11, 2005 at 16:05 UTC
    To question assumptions as I usually do these days... die is normally for programming errors, whereas this looks more like a fatal data issue, which should be handled in a different way, e.g.:
    <>; if ( /^aaa/ ) { DataFatal( "problem-description, for $_" ); } chomp; my $file = $_; if ( /okregexp/ ) { #do something } else { #matches neither! # this case wasn't mentioned, by the way! } sub DataFatal { # fatal messaging. print STDERR "*** DATA FATAL ***\n+++ " . shift . " +++\n"; exit 1; }


    Free your mind

      Unless I am missing something, printing to STDERR and then exiting is not gaining anything over die "blah\n", which is going to exit with a non-zero status. Is this to avoid a possible $SIG{DIE}?

        die produces debugging information suitable for programmers but annoying for users. Programmers want to know about line numbers. Users want information that will indicate a functional solution and don't like to see even partial stack traces - it makes them believe, quite reasonably, that the program wasn't actually ready for release and that someone didn't do their job and maybe should even be fired - in fact it's hard to argue against that.


        Free your mind

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://499070]
Approved by jbrugger
Front-paged by monkfan
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2018-02-24 00:40 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (310 votes). Check out past polls.