blahblah has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w use strict; use CGI; { # case 1 - a file my $file = "csvfile.csv"; my %parseddata = csvparse($file); } { # case 2 - a filehandle my $uploadedfile = param('uploadfile'); my %parseddata = csvparse($uploadedfile); } { # case 3 - a scalar my $csvdata = 'first,middle,last,phone,email'; my %parseddata = csvparse($csvdata); } # so how can I avoid having three separate csv parsing subs? # Once it can get at the data, the parsing is the same for all 3 cases +! sub csvparse { my $data = $_[0]; if (ref($data) eq 'SCALAR') { for (split(/\n/,$data)) { # parse... } } elsif (-f "$data") { open(DATA, "$data") or die("Noooo!\n"); while (<DATA>) { # parse... } close(DATA); } elsif () { # detect filehandle??? } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Handling different data types of the same data
by davido (Cardinal) on May 24, 2004 at 06:47 UTC | |
|
Re: Handling different data types of the same data
by Zaxo (Archbishop) on May 24, 2004 at 06:48 UTC | |
|
Re: Handling different data types of the same data
by adrianh (Chancellor) on May 24, 2004 at 07:35 UTC |