Skip to content

Instantly share code, notes, and snippets.

@Geczy
Created November 18, 2015 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Geczy/18f04ed1bf746a9803ec to your computer and use it in GitHub Desktop.
Save Geczy/18f04ed1bf746a9803ec to your computer and use it in GitHub Desktop.
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