Created
November 18, 2015 19:03
-
-
Save Geczy/18f04ed1bf746a9803ec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use JSON; | |
my @arr = ( | |
{ id => 'property_foo', | |
label => 'Some property' }, | |
{ id => 'property_bar', | |
label => 'Important field' }, | |
{ id => 'property_baz', | |
label => 'Something else' }, | |
); | |
my $json_str = encode_json(\@arr); # This will work now | |
print "$json_str"; | |
## @ARG is a special Perl array that contains the script's command line arguments | |
my $input = $ARGV[0]; | |
## Our input lines contain two columns, so we should define the variables that correspond to each column | |
my ($key, $value); | |
## Open the file, plus some sensible error checking | |
open(DATAFILE, "$input") || die("Can't open $input:!\n"); | |
## Loop through the file one line at a time | |
while (<DATAFILE>) { | |
## $_ is a special Perl variable containing the current line (in this case) | |
chomp $_; | |
## Use split() to get the fields. "\t" is the tab character. | |
($key, $value) = split(":", $_); | |
print "$key\t$value\n"; | |
} | |
$text_filename = $repo->id . '.txt'; | |
if (!Storage::exists($text_filename)) { | |
continue; | |
} | |
$fullPath = storage_path() . '/app/' . $text_filename; | |
$output = shell_exec("perl " . __DIR__ . "/script.pl " . $fullPath); | |
var_dump($output); | |
exit; | |
start_measure('fopen', 'Package via fgets'); | |
$fp = fopen($fullPath, "r"); | |
$package = []; | |
while (FALSE !== ($line = fgets($fp))) { | |
if ($line == "\n") { | |
foreach ($package as $key => $pack) { | |
$vals = explode(':', $pack, 2); | |
if (in_array(strtolower($vals[0]), $correctKeys)) { | |
$done[$vals[0]] = trim($vals[1]); | |
} | |
} | |
$all[$repo->id][] = $done; | |
$done = []; | |
$package = []; | |
continue; | |
} | |
if (0 === strpos($line, " ") || 0 === strpos($line, "\t") || strpos($line, ':') < 0) { | |
end($package); | |
$last = key($package); | |
$package[$last] = $package[$last] . "\n" . trim($line); | |
continue; | |
} | |
$package[] = rtrim($line); | |
} | |
fclose($fp); | |
stop_measure('fopen'); | |
return view('welcome'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment