Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Need advice on how to break foreach parsing loop into functions

by Prof Vince (Friar)
on Sep 16, 2007 at 08:36 UTC ( #639236=note: print w/replies, xml ) Need Help??


in reply to Re: Need advice on how to break foreach parsing loop into functions
in thread Need advice on how to break foreach parsing loop into functions

For expanding Anno's solution, you can use a dispatch table to apply the corresponding policy :
my %parsers = ( 'Ethernet' => sub { my $chunk = shift; $chunk =~ tr/\n/ /; return "ETHERNET: $chunk"; }, 'Gigabit' => sub { my $chunk = shift; $chunk =~ tr/\n/ /; return "GIGABIT: $chunk"; } ); $/ = "!\n"; $\ = "\n"; while ( <DATA> ) { chomp; if ( /^interface (.*)/ && $1 && exists $parsers{$1} ) { print $parsers{$1}( $_ ); } elsif ( /^system (.*)/ ) { # blah... } }
Edit: As anonymous said, ( ) instead of { }. Copypasta from/to Firefox is treacherous.

Replies are listed 'Best First'.
Re^3: Need advice on how to break foreach parsing loop into functions
by Anonymous Monk on Sep 16, 2007 at 17:22 UTC
    the statement

    my %parsers = { ... };

    seems wrong. should it not be

    my %parsers = ( ... );

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://639236]
help
Chatterbox?
[Corion]: Meh. There is no nice way to have "old" CGI (3) forward compatible with "new" CGI (4) with regards to ->param() in list context, is there? I could (and have) monkeypatch CGI to have *CGI::multi_param ||= \&CGI::param;, but maybe there is ...
[Corion]: ... a better approach?

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2018-06-24 18:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (126 votes). Check out past polls.

    Notices?